/ Gemma_3D_Printed_Tree_Topper / Gemma_3D_Printed_Tree_Topper.ino
Gemma_3D_Printed_Tree_Topper.ino
 1  // SPDX-FileCopyrightText: 2017 Phillip Burgess for Adafruit Industries
 2  //
 3  // SPDX-License-Identifier: MIT
 4  
 5  #include <Adafruit_NeoPixel.h>
 6  
 7  #define PIN       1
 8  #define NUM_LEDS 36
 9  
10  Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, PIN);
11  
12  uint8_t  mode   = 0,        // Current animation effect
13           offset = 0;        // Position of spinner animation
14  uint32_t color  = 0xA000A0; // Purple
15  uint32_t prevTime;          // Time of last animation mode switch
16  
17  void setup() {
18    pixels.begin();
19    pixels.setBrightness(255); // Full brightness
20    prevTime = millis();       // Starting time
21  }
22  
23  void loop() {
24    uint8_t  i;
25    uint32_t t;
26  
27    switch(mode) {
28  
29     case 0: // Random sparkles - just one LED on at a time!
30      i = random(NUM_LEDS);           // Choose a random pixel
31      pixels.setPixelColor(i, color); // Set it to current color
32      pixels.show();                  // Refresh LED states
33      // Set same pixel to "off" color now but DON'T refresh...
34      // it stays on for now...both this and the next random
35      // pixel will be refreshed on the next pass.
36      pixels.setPixelColor(i, 0);
37      delay(10);                      // 10 millisecond delay
38      break;
39   
40     case 1: // Spinny wheel
41      for(i=0; i<NUM_LEDS; i++) {    // For each LED...
42        uint32_t c = 0;              // Assume pixel will be "off" color
43        if(((offset + i) & 7) < 2) { // For each 8 pixels, 2 will be...
44          c = color;                 // ...assigned the current color
45        }
46        pixels.setPixelColor(i, c);  // Set color of pixel 'i'
47      }
48      pixels.show();                 // Refresh LED states
49      delay(90);                     // 90 millisecond delay
50      offset++;                      // Shift animation by 1 pixel on next frame
51      break;
52  
53      // More animation modes could be added here!
54    }
55  
56    t = millis();                    // Current time in milliseconds
57    if((t - prevTime) > 8000) {      // Every 8 seconds...
58      mode++;                        // Advance to next animation mode
59      if(mode > 1) {                 // End of modes?
60        mode = 0;                    // Start over from beginning
61      }
62      pixels.clear();                // Set all pixels to 'off' state
63      prevTime = t;                  // Record the time of the last mode change
64    }
65  }