RobiSydney

Steampunk NeoPixel Goggle Code.ino

Jun 28th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.87 KB | None | 0 0
  1. /*
  2. Need the library in the arduino IDE for `Adafruit neopixel' and the attiny85 driver
  3.  
  4.  
  5. https://digistump.com/wiki/digispark/tutorials/connecting
  6.  
  7. */
  8.  
  9. #include <Adafruit_NeoPixel.h>
  10. #ifdef __AVR__
  11.   #include <avr/power.h>
  12. #endif
  13.  
  14. #define LED 1
  15.  
  16. #define PIN 0
  17.  
  18. #define LOOPS 10
  19.  
  20. // brightness of brightest 2 to 254 must be even
  21. #define BRIGHT 10
  22.  
  23. // Must be an even number
  24. #define PIXELS 24
  25.  
  26. // Parameter 1 = number of pixels in strip
  27. // Parameter 2 = Arduino pin number (most are valid)
  28. // Parameter 3 = pixel type flags, add together as needed:
  29. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  30. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  31. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  32. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  33. Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXELS, PIN, NEO_GRB + NEO_KHZ800);
  34.  
  35. // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
  36. // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
  37. // and minimize distance between Arduino and first pixel.  Avoid connecting
  38. // on a live circuit...if you must, connect GND first.
  39.  
  40. void setup() {
  41.   // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  42.   #if defined (__AVR_ATtiny85__)
  43.     if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  44.   #endif
  45.   // End of trinket special code
  46.  
  47.  
  48.   strip.begin();
  49.   strip.show(); // Initialize all pixels to 'off'
  50.   pinMode(LED,OUTPUT);
  51.   delay(5000);
  52. }
  53.  
  54. void loop() {
  55.   // Some example procedures showing how to display to the pixels:
  56.   for(int l=0; l<LOOPS; l++) {
  57.     digitalWrite(LED,HIGH);
  58.     colorWipe(strip.Color(BRIGHT, 0, 0), 50); // Red
  59.     digitalWrite(LED,LOW);
  60.     colorWipe(strip.Color(BRIGHT, 0, BRIGHT), 50); // Purple
  61.     digitalWrite(LED,HIGH);
  62.     colorWipe(strip.Color(BRIGHT, BRIGHT, BRIGHT), 50); // White
  63.     digitalWrite(LED,HIGH);
  64.     colorWipe(strip.Color(0, BRIGHT, BRIGHT), 50); // yellow
  65.     digitalWrite(LED,HIGH);
  66.     colorWipe(strip.Color(BRIGHT, BRIGHT, 0), 50); // Purple
  67.     digitalWrite(LED,HIGH);
  68.     colorWipe(strip.Color(0, 0, BRIGHT), 50); // blue
  69.     digitalWrite(LED,HIGH);
  70.     colorWipe(strip.Color(BRIGHT, 0, BRIGHT), 50); // Purple
  71.     digitalWrite(LED,HIGH);
  72.     colorWipe(strip.Color(BRIGHT, BRIGHT/2, BRIGHT), 50); // Blue
  73.     digitalWrite(LED,LOW);
  74.   }
  75.   // Send a theater pixel chase in...
  76.   theaterChase(strip.Color(BRIGHT/2, BRIGHT/2, BRIGHT/2), 50); // White
  77.   digitalWrite(LED,HIGH);
  78.   theaterChase(strip.Color(BRIGHT/2, 0, 0), 50); // Red
  79.   digitalWrite(LED,LOW);
  80.   theaterChase(strip.Color(0, 0, BRIGHT/2), 50); // Blue
  81.   digitalWrite(LED,HIGH);
  82.  
  83.   rainbow(20);
  84.   digitalWrite(LED,LOW);
  85.   rainbowCycle(20);
  86.   digitalWrite(LED,HIGH);
  87.   //theaterChaseRainbow(50);
  88.   //digitalWrite(LED,LOW );
  89. }
  90.  
  91. // Fill the dots one after the other with a color
  92. void colorWipe(uint32_t c, uint8_t wait) {
  93.   //for(int l=0; l<LOOPS; l++) {
  94.     for(uint16_t i=0; i<strip.numPixels()/2; i++) {
  95.       strip.setPixelColor(i, c);
  96.       strip.setPixelColor((strip.numPixels()-1)-i, c);
  97.       strip.show();
  98.       delay(wait);
  99.     }
  100.   //}
  101. }
  102.  
  103. void rainbow(uint8_t wait) {
  104.   uint16_t i, j;
  105.  
  106.   for(j=0; j<256; j++) {
  107.     for(i=0; i<(strip.numPixels())/2; i++) {
  108.       strip.setPixelColor((strip.numPixels()-1)-i, Wheel((i+j) & 255));
  109.       strip.setPixelColor(i, Wheel((i+j) & 255));
  110.     }
  111.     strip.show();
  112.     delay(wait);
  113.   }
  114. }
  115.  
  116. // Slightly different, this makes the rainbow equally distributed throughout
  117. void rainbowCycle(uint8_t wait) {
  118.   uint16_t i, j;
  119.  
  120.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  121.     for(i=0; i< strip.numPixels()/2; i++) {
  122.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  123.       strip.setPixelColor((strip.numPixels()-1)-i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  124.     }
  125.     strip.show();
  126.     delay(wait);
  127.   }
  128. }
  129.  
  130. //Theatre-style crawling lights.
  131. void theaterChase(uint32_t c, uint8_t wait) {
  132.   for (int j=0; j<(LOOPS*2); j++) {  //do LOOPS*2 cycles of chasing
  133.     for (int q=0; q < 3; q++) {
  134.       for (int i=0; i < strip.numPixels()/2; i=i+3) {
  135.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  136.         strip.setPixelColor((strip.numPixels()-1)-(i+q), c);    //turn every third pixel on
  137.       }
  138.       strip.show();
  139.  
  140.       delay(wait);
  141.  
  142.       for (int i=0; i < strip.numPixels()/2; i=i+3) {
  143.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  144.         strip.setPixelColor((strip.numPixels()-1)-(i+q), 0);        //turn every third pixel off
  145.       }
  146.     }
  147.   }
  148. }
  149.  
  150. //Theatre-style crawling lights with rainbow effect
  151. void theaterChaseRainbow(uint8_t wait) {
  152.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  153.     for (int q=0; q < 3; q++) {
  154.       for (int i=0; i < strip.numPixels()/2; i=i+3) {
  155.         strip.setPixelColor(i+q, Wheel( (i+j) % BRIGHT));    //turn every third pixel on
  156.         strip.setPixelColor((strip.numPixels()-1)-(i+q), Wheel( (i+j) % BRIGHT));    //turn every third pixel on
  157.       }
  158.       strip.show();
  159.  
  160.       delay(wait);
  161.  
  162.       for (int i=0; i < strip.numPixels()/2; i=i+3) {
  163.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  164.         strip.setPixelColor((strip.numPixels()-1)-(i+q), 0);        //turn every third pixel off
  165.       }
  166.     }
  167.   }
  168. }
  169.  
  170. // Input a value 0 to BRIGHT to get a color value.
  171. // The colours are a transition r - g - b - back to r.
  172. uint32_t Wheel(byte WheelPos) {
  173.   WheelPos = 255 - WheelPos;
  174.   if(WheelPos < 85) {
  175.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  176.   }
  177.   if(WheelPos < 170) {
  178.     WheelPos -= 85;
  179.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  180.   }
  181.   WheelPos -= 170;
  182.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  183. }
Advertisement
Add Comment
Please, Sign In to add comment