Advertisement
McMrARM

Adafruit Neopixel Goggles Multimode

May 16th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Multi-mode goggles put together by MrARM
  2. // Power cycle changes modes
  3.  
  4. #include <Adafruit_NeoPixel.h>
  5. #include <EEPROM.h>
  6. #ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
  7. #include <avr/power.h>
  8. #endif
  9.  
  10. #define PIN 0
  11.  
  12. byte eepromFlags = 0;
  13. byte nexteeprom = 0;
  14.  
  15. bool initFlag = false; // setup for a mode, Ex. Set brightness
  16. bool modeIndicatior = true; // Light up with a set number of LEDS to indicate mode
  17.  
  18. // Adafruit tutorial wheel vars
  19. uint8_t offset = 0;
  20. uint32_t color  = 0x00FF00;
  21.  
  22. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(32, PIN);
  23.  
  24. void setup() {
  25.   // Performance tweak
  26. #ifdef __AVR_ATtiny85__ // Trinket, Gemma, etc.
  27.   if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  28. #endif
  29.   // Initialize things
  30.   randomSeed(analogRead(0));
  31.   pixels.begin();
  32.  
  33.   //get current EEPROM value, then rotate for the next reboot
  34.   eepromFlags = EEPROM.read(0);
  35.  
  36.   nexteeprom = eepromFlags + 1;
  37.   if (nexteeprom > 2) {
  38.     // Only 2 modes, reset to mode 0
  39.     nexteeprom = 0;
  40.   }
  41.   EEPROM.write(0, nexteeprom);
  42.  
  43.   // If mode indication is on, show that.
  44.   if (modeIndicatior) {
  45.     pixels.setBrightness(5);
  46.     for (int i = 0; i < eepromFlags + 1; i++) {
  47.       pixels.setPixelColor(i, 255, 255, 255);
  48.     }
  49.     pixels.show();
  50.     delay(500);
  51.     pixels.clear();
  52.     pixels.show();
  53.   }
  54.  
  55. }
  56.  
  57. void white() {
  58.   if (!initFlag) {
  59.     pixels.setBrightness(100);
  60.     for (int i = 0; i < 31; i++) {
  61.       pixels.setPixelColor(i, 255, 255, 255);
  62.     }
  63.     pixels.show();
  64.     initFlag = true;
  65.   }
  66.   delay(1000);
  67. }
  68.  
  69. void rainbow(uint8_t wait) {
  70.   uint16_t i, j;
  71.  
  72.   if (!initFlag) {
  73.     pixels.setBrightness(40);
  74.     initFlag = true;
  75.   }
  76.  
  77.   for (j = 0; j < 256; j++) {
  78.     for (i = 0; i < pixels.numPixels(); i++) {
  79.       pixels.setPixelColor(i, Wheel((i * 1 + j) & 255));
  80.     }
  81.     pixels.show();
  82.     delay(wait);
  83.   }
  84. }
  85.  
  86. void adawheel() {
  87.   uint8_t  i;
  88.   if (!initFlag) {
  89.     pixels.setBrightness(40);
  90.     initFlag = true;
  91.   }
  92.  
  93.   for (i = 0; i < 16; i++) {
  94.     uint32_t c = 0;
  95.     if (((offset + i) & 7) < 2) c = color; // 4 pixels on...
  96.     pixels.setPixelColor(   i, c); // First eye
  97.     pixels.setPixelColor(31 - i, c); // Second eye (flipped)
  98.   }
  99.   pixels.show();
  100.   offset++;
  101.   delay(50);
  102. }
  103.  
  104. // Input a value 0 to 255 to get a color value.
  105. // The colours are a transition r - g - b - back to r.
  106. uint32_t Wheel(byte WheelPos) {
  107.   if (WheelPos < 85) {
  108.     return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  109.   }
  110.   else if (WheelPos < 170) {
  111.     WheelPos -= 85;
  112.     return pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  113.   }
  114.   else {
  115.     WheelPos -= 170;
  116.     return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  117.   }
  118. }
  119.  
  120.  
  121. void loop() {
  122.   // put your main code here, to run repeatedly:
  123.   switch (eepromFlags) {
  124.     case 0:
  125.       // Simple flashlight mode created by me
  126.       // "Dude turn off your brights"
  127.       white();
  128.       break;
  129.     case 1:
  130.       // Neopixel rainbow by mattnupen.
  131.       // Best way to put it is fading colors
  132.       rainbow(30);
  133.       break;
  134.     case 2:
  135.       // Spinning color Wheel created by adafruit.
  136.       // Color set in the color variable
  137.       adawheel();
  138.       break;
  139.     default:
  140.     // Spillover/EEPROM overflow code.
  141.     // Slow blinking white
  142.       white();
  143.       delay(200);
  144.       pixels.clear();
  145.       pixels.show();
  146.       initFlag = false;
  147.       break;
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement