So I'm using Adafruit randomcycle code, to animate some lights for a sword. I want to be able to make the animation stop whenever I released the button, but having trouble to do so. I realize it has to do with how delay works in Arduino and currently for me it seems impossible to do something about it.
I want to stop my animation from keep on going, whenever I release the button but it keeps on going for a little while before it stops.
// Katarina LED_ANIMATION Version 1.0
#include <Adafruit_NeoPixel.h>
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 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)
#define PIN 12
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
const int BTNpin = 11;
int BTNpinState = 0;
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(BTNpin, INPUT);
}
void loop() {
BTNpinState = digitalRead(BTNpin);
if(BTNpinState == HIGH) {
rainbowCycle(20);
} else {
colorWipe(strip.Color(0, 0, 0), 20); // Red
}
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*1; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, 0);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, 0, 0);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 0, 0);
}
I'm not a native english speaker and a rookie(copy paste & tinker a bit) in this community.