Hello! I'm working on some lights for a costume, I was able to get it to light up and change the colors when I press a button. But I used NeoPixel LED's and I really want to add in some nice effects to add to it. I was thinking maybe a simple fading/glowing effect but I'm having troubles getting it right. I also cant get the animation to loop, if I do I can't use the button to change the color.
I'm starting to run out of time, so any help would be amazing!
Here's a post of what I currently have if your curious.
https://www.instagram.com/p/B37Y5N9Dh8X/
The code is a modified sample of the NeoPixel buttoncycler
// Simple demonstration on using an input device to trigger changes on your
// NeoPixels. Wire a momentary push button to connect from ground to a
// digital IO pin. When the button is pressed it will change to a new pixel
// animation. Initial state has all pixels off -- press the button once to
// start the first animation. As written, the button does not interrupt an
// animation in-progress, it works only when idle.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket
#endif
// Digital IO pin connected to the button. This will be driven with a
// pull-up resistor so the switch pulls the pin to ground momentarily.
// On a high -> low transition the button press logic will execute.
#define BUTTON_PIN1 3
#define BUTTON_PIN2 2
#define PIXEL_PIN1 0 // Digital IO pin connected to the NeoPixels.
#define PIXEL_PIN2 1 // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 4 // Number of NeoPixels
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip1(PIXEL_COUNT, PIXEL_PIN1, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2(PIXEL_COUNT, PIXEL_PIN2, NEO_GRB + NEO_KHZ800);
// Argument 1 = Number of pixels in NeoPixel strip
// Argument 2 = Arduino pin number (most are valid)
// Argument 3 = Pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
boolean oldState1 = HIGH;
boolean oldState2 = HIGH;
int mode1 = 0; // Currently-active animation mode, 0-9
int mode2 = 0; // Currently-active animation mode, 0-9
void setup() {
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
pinMode(BUTTON_PIN1, INPUT_PULLUP);
pinMode(BUTTON_PIN2, INPUT_PULLUP);
strip1.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip1.show(); // Initialize all pixels to 'off'
strip2.begin(); // Initialize NeoPixel strip object (REQUIRED)
strip2.show(); // Initialize all pixels to 'off'
}
void loop() {
// Get current button state.
boolean newState1 = digitalRead(BUTTON_PIN1);
boolean newState2 = digitalRead(BUTTON_PIN2);
// Check if state changed from high to low (button press).
if((newState1 == LOW) && (oldState1 == HIGH)) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState1 = digitalRead(BUTTON_PIN1);
if(newState1 == LOW) { // Yes, still low
if(++mode1 > 6) mode1 = 0; // Advance to next mode, wrap around after #8
switch(mode1) { // Start the new animation...
case 0:
colorWipe1(strip1.Color( 0, 0, 0), 50); // Black/off
break;
case 1:
colorWipe1(strip1.Color( 0, 255, 0), 50); // Green Magic
break;
case 2:
colorWipe1(strip1.Color(255, 0, 0), 50); // Red Summon
break;
case 3:
colorWipe1(strip1.Color(255, 255, 255), 50); // White Holy
break;
case 4:
colorWipe1(strip1.Color( 0, 0, 255), 50); // Blue Support
break;
case 5:
colorWipe1(strip1.Color(230, 194, 20), 10); // Yellow Compound
break;
case 6:
colorWipe1(strip1.Color(131, 4, 205), 50); // pruple Independent
break;
case 7:
theaterChase1(strip1.Color( 0, 0, 127), 50); // Blue
break;
}
}
}
// Check if state changed from high to low (button press).
if((newState2 == LOW) && (oldState2 == HIGH)) {
// Short delay to debounce button.
delay(20);
// Check if button is still low after debounce.
newState2 = digitalRead(BUTTON_PIN2);
if(newState2 == LOW) { // Yes, still low
if(++mode2 > 6) mode2 = 0; // Advance to next mode, wrap around after #8
switch(mode2) { // Start the new animation...
case 0:
colorWipe2(strip2.Color(0, 0, 0), 50); // Black/off
break;
case 1:
colorWipe2(strip2.Color( 0, 255, 0), 50); // Green Magic
break;
case 2:
colorWipe2(strip2.Color(255, 0, 0), 50); // Red Summon
break;
case 3:
colorWipe2(strip2.Color(255, 255, 255), 50); // White Holy
break;
case 4:
colorWipe2(strip2.Color( 0, 0, 255), 50); // Blue Support
break;
case 5:
colorWipe2(strip2.Color(230, 194, 20), 10); // Yellow Compound
break;
case 6:
colorWipe2(strip2.Color(131, 4, 205), 50); // pruple Independent
break;
}
}
}
// Set the last-read button state to the old state.
oldState1 = newState1;
oldState2 = newState2;
}
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe1(uint32_t color, int wait) {
for(int i=0; i<strip1.numPixels(); i++) { // For each pixel in strip...
strip1.setPixelColor(i, color); // Set pixel's color (in RAM)
strip1.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void colorWipe2(uint32_t color, int wait) {
for(int i=0; i<strip2.numPixels(); i++) { // For each pixel in strip...
strip2.setPixelColor(i, color); // Set pixel's color (in RAM)
strip2.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
// Theater-marquee-style chasing lights. Pass in a color (32-bit value,
// a la strip.Color(r,g,b) as mentioned above), and a delay time (in ms)
// between frames.
void theaterChase1(uint32_t color, int wait) {
for(int a=0; a<10; a++) { // Repeat 10 times...
for(int b=0; b<3; b++) { // 'b' counts from 0 to 2...
strip1.clear(); // Set all pixels in RAM to 0 (off)
// 'c' counts up from 'b' to end of strip in steps of 3...
for(int c=b; c<strip1.numPixels(); c += 3) {
strip1.setPixelColor(c, color); // Set pixel 'c' to value 'color'
}
strip1.show(); // Update strip with new contents
delay(wait); // Pause for a moment
}
}
}