| 1 | #include <Adafruit_NeoPixel.h> |
| 2 | #ifdef __AVR__ |
| 3 | #include <avr/power.h> |
| 4 | #endif |
| 5 | |
| 6 | #define PIN 6 |
| 7 | |
| 8 | // Parameter 1 = number of pixels in strip |
| 9 | // Parameter 2 = Arduino pin number (most are valid) |
| 10 | // Parameter 3 = pixel type flags, add together as needed: |
| 11 | // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) |
| 12 | // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) |
| 13 | // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) |
| 14 | // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) |
| 15 | // NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products) |
| 16 | Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800); |
| 17 | |
| 18 | // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across |
| 19 | // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input |
| 20 | // and minimize distance between Arduino and first pixel. Avoid connecting |
| 21 | // on a live circuit...if you must, connect GND first. |
| 22 | |
| 23 | void setup() { |
| 24 | // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket |
| 25 | #if defined (__AVR_ATtiny85__) |
| 26 | if (F_CPU == 16000000) clock_prescale_set(clock_div_1); |
| 27 | #endif |
| 28 | // End of trinket special code |
| 29 | |
| 30 | strip.begin(); |
| 31 | strip.setBrightness(50); |
| 32 | strip.show(); // Initialize all pixels to 'off' |
| 33 | } |
| 34 | |
| 35 | void loop() { |
| 36 | // Some example procedures showing how to display to the pixels: |
| 37 | colorWipe(strip.Color(255, 0, 0), 50); // Red |
| 38 | colorWipe(strip.Color(0, 255, 0), 50); // Green |
| 39 | colorWipe(strip.Color(0, 0, 255), 50); // Blue |
| 40 | //colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW |
| 41 | // Send a theater pixel chase in... |
| 42 | theaterChase(strip.Color(127, 127, 127), 50); // White |
| 43 | theaterChase(strip.Color(127, 0, 0), 50); // Red |
| 44 | theaterChase(strip.Color(0, 0, 127), 50); // Blue |
| 45 | |
| 46 | rainbow(20); |
| 47 | rainbowCycle(20); |
| 48 | theaterChaseRainbow(50); |
| 49 | } |
| 50 | |
| 51 | // Fill the dots one after the other with a color |
| 52 | void colorWipe(uint32_t c, uint8_t wait) { |
| 53 | for(uint16_t i=0; i<strip.numPixels(); i++) { |
| 54 | strip.setPixelColor(i, c); |
| 55 | strip.show(); |
| 56 | delay(wait); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void rainbow(uint8_t wait) { |
| 61 | uint16_t i, j; |
| 62 | |
| 63 | for(j=0; j<256; j++) { |
| 64 | for(i=0; i<strip.numPixels(); i++) { |
| 65 | strip.setPixelColor(i, Wheel((i+j) & 255)); |
| 66 | } |
| 67 | strip.show(); |
| 68 | delay(wait); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Slightly different, this makes the rainbow equally distributed throughout |
| 73 | void rainbowCycle(uint8_t wait) { |
| 74 | uint16_t i, j; |
| 75 | |
| 76 | for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel |
| 77 | for(i=0; i< strip.numPixels(); i++) { |
| 78 | strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); |
| 79 | } |
| 80 | strip.show(); |
| 81 | delay(wait); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | //Theatre-style crawling lights. |
| 86 | void theaterChase(uint32_t c, uint8_t wait) { |
| 87 | for (int j=0; j<10; j++) { //do 10 cycles of chasing |
| 88 | for (int q=0; q < 3; q++) { |
| 89 | for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 90 | strip.setPixelColor(i+q, c); //turn every third pixel on |
| 91 | } |
| 92 | strip.show(); |
| 93 | |
| 94 | delay(wait); |
| 95 | |
| 96 | for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 97 | strip.setPixelColor(i+q, 0); //turn every third pixel off |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | //Theatre-style crawling lights with rainbow effect |
| 104 | void theaterChaseRainbow(uint8_t wait) { |
| 105 | for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel |
| 106 | for (int q=0; q < 3; q++) { |
| 107 | for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 108 | strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on |
| 109 | } |
| 110 | strip.show(); |
| 111 | |
| 112 | delay(wait); |
| 113 | |
| 114 | for (uint16_t i=0; i < strip.numPixels(); i=i+3) { |
| 115 | strip.setPixelColor(i+q, 0); //turn every third pixel off |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Input a value 0 to 255 to get a color value. |
| 122 | // The colours are a transition r - g - b - back to r. |
| 123 | uint32_t Wheel(byte WheelPos) { |
| 124 | WheelPos = 255 - WheelPos; |
| 125 | if(WheelPos < 85) { |
| 126 | return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); |
| 127 | } |
| 128 | if(WheelPos < 170) { |
| 129 | WheelPos -= 85; |
| 130 | return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); |
| 131 | } |
| 132 | WheelPos -= 170; |
| 133 | return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); |
| 134 | } |