Krolyxon
Commit 3120783

Branches

Tags

Refs →

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

200 lines · 7.5 KB

1// A non-blocking everyday NeoPixel strip test program.
2
3// NEOPIXEL BEST PRACTICES for most reliable operation:
4// - Add 1000 uF CAPACITOR between NeoPixel strip's + and - connections.
5// - MINIMIZE WIRING LENGTH between microcontroller board and first pixel.
6// - NeoPixel strip's DATA-IN should pass through a 300-500 OHM RESISTOR.
7// - AVOID connecting NeoPixels on a LIVE CIRCUIT. If you must, ALWAYS
8// connect GROUND (-) first, then +, then data.
9// - When using a 3.3V microcontroller with a 5V-powered NeoPixel strip,
10// a LOGIC-LEVEL CONVERTER on the data line is STRONGLY RECOMMENDED.
11// (Skipping these may work OK on your workbench but can fail in the field)
12
13#include <Adafruit_NeoPixel.h>
14#ifdef __AVR__
15 #include <avr/power.h> // Required for 16 MHz Adafruit Trinket
16#endif
17
18// Which pin on the Arduino is connected to the NeoPixels?
19// On a Trinket or Gemma we suggest changing this to 1:
20#ifdef ESP32
21// Cannot use 6 as output for ESP. Pins 6-11 are connected to SPI flash. Use 16 instead.
22#define LED_PIN 16
23#else
24#define LED_PIN 6
25#endif
26
27// How many NeoPixels are attached to the Arduino?
28#define LED_COUNT 60
29
30// Declare our NeoPixel strip object:
31Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
32// Argument 1 = Number of pixels in NeoPixel strip
33// Argument 2 = Arduino pin number (most are valid)
34// Argument 3 = Pixel type flags, add together as needed:
35// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
36// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
37// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
38// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
39// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
40
41unsigned long pixelPrevious = 0; // Previous Pixel Millis
42unsigned long patternPrevious = 0; // Previous Pattern Millis
43int patternCurrent = 0; // Current Pattern Number
44int patternInterval = 5000; // Pattern Interval (ms)
45bool patternComplete = false;
46
47int pixelInterval = 50; // Pixel Interval (ms)
48int pixelQueue = 0; // Pattern Pixel Queue
49int pixelCycle = 0; // Pattern Pixel Cycle
50uint16_t pixelNumber = LED_COUNT; // Total Number of Pixels
51
52// setup() function -- runs once at startup --------------------------------
53void setup() {
54 // These lines are specifically to support the Adafruit Trinket 5V 16 MHz.
55 // Any other board, you can remove this part (but no harm leaving it):
56#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
57 clock_prescale_set(clock_div_1);
58#endif
59 // END of Trinket-specific code.
60
61 strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
62 strip.show(); // Turn OFF all pixels ASAP
63 strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
64}
65
66// loop() function -- runs repeatedly as long as board is on ---------------
67void loop() {
68 unsigned long currentMillis = millis(); // Update current time
69 if( patternComplete || (currentMillis - patternPrevious) >= patternInterval) { // Check for expired time
70 patternComplete = false;
71 patternPrevious = currentMillis;
72 patternCurrent++; // Advance to next pattern
73 if(patternCurrent >= 7)
74 patternCurrent = 0;
75 }
76
77 if(currentMillis - pixelPrevious >= pixelInterval) { // Check for expired time
78 pixelPrevious = currentMillis; // Run current frame
79 switch (patternCurrent) {
80 case 7:
81 theaterChaseRainbow(50); // Rainbow-enhanced theaterChase variant
82 break;
83 case 6:
84 rainbow(10); // Flowing rainbow cycle along the whole strip
85 break;
86 case 5:
87 theaterChase(strip.Color(0, 0, 127), 50); // Blue
88 break;
89 case 4:
90 theaterChase(strip.Color(127, 0, 0), 50); // Red
91 break;
92 case 3:
93 theaterChase(strip.Color(127, 127, 127), 50); // White
94 break;
95 case 2:
96 colorWipe(strip.Color(0, 0, 255), 50); // Blue
97 break;
98 case 1:
99 colorWipe(strip.Color(0, 255, 0), 50); // Green
100 break;
101 default:
102 colorWipe(strip.Color(255, 0, 0), 50); // Red
103 break;
104 }
105 }
106}
107
108// Some functions of our own for creating animated effects -----------------
109
110// Fill strip pixels one after another with a color. Strip is NOT cleared
111// first; anything there will be covered pixel by pixel. Pass in color
112// (as a single 'packed' 32-bit value, which you can get by calling
113// strip.Color(red, green, blue) as shown in the loop() function above),
114// and a delay time (in milliseconds) between pixels.
115void colorWipe(uint32_t color, int wait) {
116 static uint16_t current_pixel = 0;
117 pixelInterval = wait; // Update delay time
118 strip.setPixelColor(current_pixel++, color); // Set pixel's color (in RAM)
119 strip.show(); // Update strip to match
120 if(current_pixel >= pixelNumber) { // Loop the pattern from the first LED
121 current_pixel = 0;
122 patternComplete = true;
123 }
124}
125
126// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
127// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
128// between frames.
129void theaterChase(uint32_t color, int wait) {
130 static uint32_t loop_count = 0;
131 static uint16_t current_pixel = 0;
132
133 pixelInterval = wait; // Update delay time
134
135 strip.clear();
136
137 for(int c=current_pixel; c < pixelNumber; c += 3) {
138 strip.setPixelColor(c, color);
139 }
140 strip.show();
141
142 current_pixel++;
143 if (current_pixel >= 3) {
144 current_pixel = 0;
145 loop_count++;
146 }
147
148 if (loop_count >= 10) {
149 current_pixel = 0;
150 loop_count = 0;
151 patternComplete = true;
152 }
153}
154
155// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
156void rainbow(uint8_t wait) {
157 if(pixelInterval != wait)
158 pixelInterval = wait;
159 for(uint16_t i=0; i < pixelNumber; i++) {
160 strip.setPixelColor(i, Wheel((i + pixelCycle) & 255)); // Update delay time
161 }
162 strip.show(); // Update strip to match
163 pixelCycle++; // Advance current cycle
164 if(pixelCycle >= 256)
165 pixelCycle = 0; // Loop the cycle back to the begining
166}
167
168//Theatre-style crawling lights with rainbow effect
169void theaterChaseRainbow(uint8_t wait) {
170 if(pixelInterval != wait)
171 pixelInterval = wait; // Update delay time
172 for(int i=0; i < pixelNumber; i+=3) {
173 strip.setPixelColor(i + pixelQueue, Wheel((i + pixelCycle) % 255)); // Update delay time
174 }
175 strip.show();
176 for(int i=0; i < pixelNumber; i+=3) {
177 strip.setPixelColor(i + pixelQueue, strip.Color(0, 0, 0)); // Update delay time
178 }
179 pixelQueue++; // Advance current queue
180 pixelCycle++; // Advance current cycle
181 if(pixelQueue >= 3)
182 pixelQueue = 0; // Loop
183 if(pixelCycle >= 256)
184 pixelCycle = 0; // Loop
185}
186
187// Input a value 0 to 255 to get a color value.
188// The colours are a transition r - g - b - back to r.
189uint32_t Wheel(byte WheelPos) {
190 WheelPos = 255 - WheelPos;
191 if(WheelPos < 85) {
192 return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
193 }
194 if(WheelPos < 170) {
195 WheelPos -= 85;
196 return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
197 }
198 WheelPos -= 170;
199 return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
200}