Hello all, i have my code below. in this case my code switches state only after the for loop in each case is finished, how can i go about instantly stopping for loop and moving on to the next given state?
thanks!
#include <FastLED.h>
#define NUM_LEDS 30
#define LED_PIN 3
CRGB leds[NUM_LEDS];
const int buttonPin = 2;
int buttonState = 0;
int prevbuttonState = 0;
int State = 1;
void setup() {
FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness (96);
Serial.begin(9600);
}
void loop() {
checkbuttonState();
switch (State)
{
case 1:
RedcrossingLED();
break;
case 2:
brainMelter();
break;
case 3:
//placeholder
break;
case 4:
//placeholder
break;
case 5:
//placeholder
break;
}
}
////////////////////////////////////////////////////////////////////////
void checkbuttonState()
{
buttonState = digitalRead(buttonPin);
if (buttonState != prevbuttonState) {
if (buttonState == LOW) {
State++;
Serial.println(State);
}
if (State > 5){
State = 1;
}
}
prevbuttonState = buttonState;
}
////////////////////////////////////////////////////////////////////////
void RedcrossingLED()
{
for (int i = 0; i < NUM_LEDS; i++) {
checkbuttonState();
leds[i] = CRGB::Red;
FastLED.show();
delay(100);
leds[i] = CRGB::Black;
}
for (int i = NUM_LEDS - 1; i >=- 0; i--){
checkbuttonState();
leds[i] = CRGB::Red;
FastLED.show();
delay(100);
leds[i] = CRGB::Black;
}
}
////////////////////////////////////////////////////////////////
void brainMelter()
{
static uint8_t hue = 0;
Serial.print("x");
// First slide the led in one direction
for(int i = 0; i < NUM_LEDS; i++) {
checkbuttonState();
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
Serial.print("x");
// Now go in the other direction.
for(int i = (NUM_LEDS)-1; i >= 0; i--) {
checkbuttonState();
// Set the i'th led to red
leds[i] = CHSV(hue++, 255, 255);
// Show the leds
FastLED.show();
// now that we've shown the leds, reset the i'th led to black
// leds[i] = CRGB::Black;
fadeall();
// Wait a little bit before we loop around and do it again
delay(10);
}
}
////////////////////////////////////////////////////////////////
void fadeall() { for(int i = 0; i < NUM_LEDS; i++) { leds[i].nscale8(250); } }
////////////////////////////////////////////////////////////////
The delay(100) will be the main cause for latency.
A lag of 1/10th of a second is considered in many studies (since 1993) as the upper limit for a human interface before it starts feeling laggy (nowadays gamers wants low single digit ms lag ).
So I would also replace the call to delay by a custom millis() based function where you also check the button.
The better design would be to use state machine based non blocking functions for the led animations that would be called repetitively from the loop which would also check the button. That’s more work
speech coders generate new phonemes every 20 ms. in other words, the vocal chords can generate a new phoneme 50 times/sec. this applies to other muscles as well. athletes have faster reaction times.
not obvious to me how quickly someone can perceive seeing something change. American TVs refresh the screen at 60Hz which is in line with above
Younger ppl are more sensitive, percussionists of any age…
So whenever, wherever you can make something instant or nearly so, and doing is easy, you should because there will inevitably be other sources of lag.