Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit NeoPixelexamplesbuttoncyclerbuttoncycler.ino

164 lines · 6.9 KB

1// Simple demonstration on using an input device to trigger changes on your
2// NeoPixels. Wire a momentary push button to connect from ground to a
3// digital IO pin. When the button is pressed it will change to a new pixel
4// animation. Initial state has all pixels off -- press the button once to
5// start the first animation. As written, the button does not interrupt an
6// animation in-progress, it works only when idle.
7
8#include <Adafruit_NeoPixel.h>
9#ifdef __AVR__
10 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
11#endif
12
13// Digital IO pin connected to the button. This will be driven with a
14// pull-up resistor so the switch pulls the pin to ground momentarily.
15// On a high -> low transition the button press logic will execute.
16#define BUTTON_PIN 2
17
18#define PIXEL_PIN 6 // Digital IO pin connected to the NeoPixels.
19
20#define PIXEL_COUNT 16 // Number of NeoPixels
21
22// Declare our NeoPixel strip object:
23Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);
24// Argument 1 = Number of pixels in NeoPixel strip
25// Argument 2 = Arduino pin number (most are valid)
26// Argument 3 = Pixel type flags, add together as needed:
27// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
28// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
29// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
30// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
31// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
32
33boolean oldState = HIGH;
34int mode = 0; // Currently-active animation mode, 0-9
35
36void setup() {
37 pinMode(BUTTON_PIN, INPUT_PULLUP);
38 strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
39 strip.show(); // Initialize all pixels to 'off'
40}
41
42void loop() {
43 // Get current button state.
44 boolean newState = digitalRead(BUTTON_PIN);
45
46 // Check if state changed from high to low (button press).
47 if((newState == LOW) && (oldState == HIGH)) {
48 // Short delay to debounce button.
49 delay(20);
50 // Check if button is still low after debounce.
51 newState = digitalRead(BUTTON_PIN);
52 if(newState == LOW) { // Yes, still low
53 if(++mode > 8) mode = 0; // Advance to next mode, wrap around after #8
54 switch(mode) { // Start the new animation...
55 case 0:
56 colorWipe(strip.Color( 0, 0, 0), 50); // Black/off
57 break;
58 case 1:
59 colorWipe(strip.Color(255, 0, 0), 50); // Red
60 break;
61 case 2:
62 colorWipe(strip.Color( 0, 255, 0), 50); // Green
63 break;
64 case 3:
65 colorWipe(strip.Color( 0, 0, 255), 50); // Blue
66 break;
67 case 4:
68 theaterChase(strip.Color(127, 127, 127), 50); // White
69 break;
70 case 5:
71 theaterChase(strip.Color(127, 0, 0), 50); // Red
72 break;
73 case 6:
74 theaterChase(strip.Color( 0, 0, 127), 50); // Blue
75 break;
76 case 7:
77 rainbow(10);
78 break;
79 case 8:
80 theaterChaseRainbow(50);
81 break;
82 }
83 }
84 }
85
86 // Set the last-read button state to the old state.
87 oldState = newState;
88}
89
90// Fill strip pixels one after another with a color. Strip is NOT cleared
91// first; anything there will be covered pixel by pixel. Pass in color
92// (as a single 'packed' 32-bit value, which you can get by calling
93// strip.Color(red, green, blue) as shown in the loop() function above),
94// and a delay time (in milliseconds) between pixels.
95void colorWipe(uint32_t color, int wait) {
96 for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
97 strip.setPixelColor(i, color); // Set pixel's color (in RAM)
98 strip.show(); // Update strip to match
99 delay(wait); // Pause for a moment
100 }
101}
102
103// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
104// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
105// between frames.
106void theaterChase(uint32_t color, int wait) {
107 for(int a=0; a<10; a++) { // Repeat 10 times...
108 for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
109 strip.clear(); // Set all pixels in RAM to 0 (off)
110 // 'c' counts up from 'b' to end of strip in steps of 3...
111 for(int c=b; c<strip.numPixels(); c += 3) {
112 strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
113 }
114 strip.show(); // Update strip with new contents
115 delay(wait); // Pause for a moment
116 }
117 }
118}
119
120// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
121void rainbow(int wait) {
122 // Hue of first pixel runs 3 complete loops through the color wheel.
123 // Color wheel has a range of 65536 but it's OK if we roll over, so
124 // just count from 0 to 3*65536. Adding 256 to firstPixelHue each time
125 // means we'll make 3*65536/256 = 768 passes through this outer loop:
126 for(long firstPixelHue = 0; firstPixelHue < 3*65536; firstPixelHue += 256) {
127 for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
128 // Offset pixel hue by an amount to make one full revolution of the
129 // color wheel (range of 65536) along the length of the strip
130 // (strip.numPixels() steps):
131 int pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
132 // strip.ColorHSV() can take 1 or 3 arguments: a hue (0 to 65535) or
133 // optionally add saturation and value (brightness) (each 0 to 255).
134 // Here we're using just the single-argument hue variant. The result
135 // is passed through strip.gamma32() to provide 'truer' colors
136 // before assigning to each pixel:
137 strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
138 }
139 strip.show(); // Update strip with new contents
140 delay(wait); // Pause for a moment
141 }
142}
143
144// Rainbow-enhanced theater marquee. Pass delay time (in ms) between frames.
145void theaterChaseRainbow(int wait) {
146 int firstPixelHue = 0; // First pixel starts at red (hue 0)
147 for(int a=0; a<30; a++) { // Repeat 30 times...
148 for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
149 strip.clear(); // Set all pixels in RAM to 0 (off)
150 // 'c' counts up from 'b' to end of strip in increments of 3...
151 for(int c=b; c<strip.numPixels(); c += 3) {
152 // hue of pixel 'c' is offset by an amount to make one full
153 // revolution of the color wheel (range 65536) along the length
154 // of the strip (strip.numPixels() steps):
155 int hue = firstPixelHue + c * 65536L / strip.numPixels();
156 uint32_t color = strip.gamma32(strip.ColorHSV(hue)); // hue -> RGB
157 strip.setPixelColor(c, color); // Set pixel 'c' to value 'color'
158 }
159 strip.show(); // Update strip with new contents
160 delay(wait); // Pause for a moment
161 firstPixelHue += 65536 / 90; // One cycle of color wheel over 90 frames
162 }
163 }
164}