Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. //Qinxinrui Zhu. NeoPixel Assignment. Nov.12.2019.//
  2. #include <Adafruit_NeoPixel.h>
  3. #ifdef __AVR__
  4. #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
  5. #endif
  6.  
  7. #define LED_PIN 6 // connect to NeoPixels
  8. #define LED_COUNT 40 // How many Pixels
  9. #define BUTTON_PIN 2 // connect to button
  10.  
  11. boolean oldState = HIGH;
  12. int mode = 0;//animation mode, 0-9
  13.  
  14. Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
  15. // Argument 1 = Number of pixels in NeoPixel strip
  16. // Argument 2 = Arduino pin number (most are valid)
  17. // Argument 3 = Pixel type flags, add together as needed:
  18. // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  19. // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  20. // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
  21. // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  22. // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
  23.  
  24. void setup() {
  25. pinMode(BUTTON_PIN, INPUT_PULLUP);
  26. #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
  27. clock_prescale_set(clock_div_1);
  28. #endif
  29. strip.begin();//Initializing the neopxel strip object
  30. strip.show();// Initialize all pixels to 'off'
  31. }
  32.  
  33.  
  34.  
  35. void loop() {
  36. ////Gets the current button status.
  37. boolean newState = digitalRead(BUTTON_PIN);
  38. if ((newState == LOW) && (oldState == HIGH)) {
  39. // Short delay to debounce button.
  40. delay(30);
  41. // Check if the button is still low after debounce.
  42. newState = digitalRead(BUTTON_PIN);
  43. if (newState == LOW) {
  44. if (++mode > 5) mode = 0;
  45. switch (mode) {
  46. case 1:
  47. ////Fill with various colors along the strip
  48. colorWipe(strip.Color(30, 144, 255), 40);
  49. break;//DodgerBlue
  50. case 2 :
  51. colorWipe(strip.Color(255, 127, 80), 10);//Coral
  52. break;
  53. case 3:
  54. colorWipe(strip.Color(0, 255, 0), 50);//Lime
  55. break;
  56.  
  57. }
  58.  
  59. }
  60.  
  61. }
  62. //Set the last read button state to the old state.
  63. oldState = newState;
  64. }
  65. //Created animatied effect
  66. void colorWipe(uint32_t color, int wait) {
  67. for (int i = 0; i < strip.numPixels(); i++) {
  68. strip.setPixelColor(i, color);
  69. strip.show();
  70. delay(20);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement