/ 3D_Printed_NeoPixel_Gas_Mask / 3D_Printed_NeoPixel_Gas_Mask.ino
3D_Printed_NeoPixel_Gas_Mask.ino
 1  // SPDX-FileCopyrightText: 2014 Phil Burgess for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  //
 5  
 6  #include <Adafruit_NeoPixel.h>
 7  
 8  #define PIN       0
 9  #define NUM_LEDS 24
10  
11  Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN);
12  
13  uint8_t  mode   = 0,        // Current animation effect
14           offset = 0;        // Position of spinner animation
15  uint32_t color  = 0xFF0000; // Starting color = red
16  uint32_t prevTime;          // Time of last animation mode switch
17  
18  void setup() {
19    pixels.begin();
20    pixels.setBrightness(85); // 1/3 brightness
21    prevTime = millis();      // Starting time
22  }
23  
24  void loop() {
25    uint8_t  i;
26    uint32_t t;
27  
28    switch(mode) {
29  
30     case 0: // Random sparkles - just one LED on at a time!
31      i = random(NUM_LEDS);           // Choose a random pixel
32      pixels.setPixelColor(i, color); // Set it to current color
33      pixels.show();                  // Refresh LED states
34      // Set same pixel to "off" color now but DON'T refresh...
35      // it stays on for now...both this and the next random
36      // pixel will be refreshed on the next pass.
37      pixels.setPixelColor(i, 0);
38      delay(10);                      // 10 millisecond delay
39      break;
40   
41     case 1: // Spinny wheel
42      // A little trick here: pixels are processed in groups of 8
43      // (with 2 of 8 on at a time), NeoPixel rings are 24 pixels
44      // (8*3) and 16 pixels (8*2), so we can issue the same data
45      // to both rings and it appears correct and contiguous
46      // (also, the pixel order is different between the two ring
47      // types, so we get the reversed motion on #2 for free).
48      for(i=0; i<NUM_LEDS; i++) {    // For each LED...
49        uint32_t c = 0;              // Assume pixel will be "off" color
50        if(((offset + i) & 7) < 2) { // For each 8 pixels, 2 will be...
51          c = color;                 // ...assigned the current color
52        }
53        pixels.setPixelColor(i, c);  // Set color of pixel 'i'
54      }
55      pixels.show();                 // Refresh LED states
56      delay(50);                     // 50 millisecond delay
57      offset++;                      // Shift animation by 1 pixel on next frame
58      if(offset >= 8) offset = 0;    // Reset offset every 8 pixels
59      break;
60  
61      // More animation modes could be added here!
62    }
63  
64    t = millis();                    // Current time in milliseconds
65    if((t - prevTime) > 8000) {      // Every 8 seconds...
66      mode++;                        // Advance to next animation mode
67      if(mode > 1) {                 // End of modes?
68        mode = 0;                    // Start over from beginning
69        color >>= 8;                 // Next color R->G->B
70        if(!color) color = 0xFF0000; // Preiodically reset to red
71      }
72      pixels.clear();                // Set all pixels to 'off' state
73      prevTime = t;                  // Record the time of the last mode change
74    }
75  }