I am really happy with my new Arduino stuff and playing around with all those pixels :).
I am new to programming and it's difficult to understand it, but with every success, I grow in knowledge.
My latest problem now is to restart my loop of incrementing the strip at any time with the push of a button.
I know that delay();
will stop the code completely and wait till the time passes, but I wrote some code without the use of delay();
and used millis();
instead.
So far my LEDs are incrementing through my strip, but I am not able to restart it until it passes though completely. I read that the while();
part in my code works kinda like a delay();
and that would prevent it from restarting until a full cycle. I used some millis();
code from a different sample so it's not like the ones from Blink Without Delay.
I searched so many sites and cannot write another code to increment without a delay, so hopefully you can help me with it.
#include <FastLED.h>
#define NUM_LEDS_PER_STRIP 16
CRGB ledStrip[NUM_LEDS_PER_STRIP];
const int signalButtonPin = 4;
const int ledStripPin = 8;
int buttonState = 0;
int lastButtonState = 0;
int delaySignalAnimation = 100;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
void setup() {
pinMode(signalButtonPin, INPUT_PULLUP);
pinMode(ledStripPin, OUTPUT);
FastLED.addLeds<NEOPIXEL, 8>(ledStrip, NUM_LEDS_PER_STRIP);
}
void loop() {
buttonState = digitalRead(signalButtonPin);
if (buttonState != lastButtonState) {
if (buttonState == LOW) {
fill_solid(ledStrip, NUM_LEDS_PER_STRIP, CRGB::Black);
for (int i = 0; i < 16; i++) {
ledStrip[i] = CRGB::Blue;
FastLED.setBrightness(100);
FastLED.show();
currentMillis = previousMillis = millis();
while (previousMillis + delaySignalAnimation >= currentMillis) {
currentMillis = millis();
}
}
} else {
lastButtonState = buttonState;
}
fill_solid(ledStrip, NUM_LEDS_PER_STRIP, CRGB::Black);
FastLED.setBrightness(0);
}
FastLED.show();
}
Thanks in advance for you help.