/*
Forum: https://forum.arduino.cc/t/goto-loop-gets-ignored-sometimes/1357468/6
Wokwi: https://wokwi.com/projects/423791454389927937
*/
#include <FastLED.h>
#define NUM_LEDS 112
#define DATA_PIN 13
const int buttonPin = 2;
int buttonState = 0; // Current state of the button
bool lastButtonState = false; // Previous state of the button
bool loopStarted = false; // Whether the loop has started
int halfWay;
enum Loops {FIRST,SECOND,THIRD, FORTH, IDLE};
Loops myLoop = FIRST;
CRGB leds[NUM_LEDS];
void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with internal pull-up
// Turn all LEDs off
FastLED.clear();
FastLED.show();
halfWay = NUM_LEDS / 2; // cuts strip in half
}
void fadeall() {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].nscale8(200); // nscale is comet length (Small number - shorter tail)
}
}
void loop() {
switch(myLoop){
case FIRST:
break;
case SECOND:
break;
case THIRD:
break;
case FORTH:
break;
case IDLE:
break;
}
}
void firstLoop(){
// First loop: LED pattern fills outward from end
for (int i = halfWay - 1; i >= 0; i--) {
leds[halfWay + i] = CRGB(70, 254, 0);
leds[halfWay - i] = CRGB(70, 254, 0);
FastLED.setBrightness(120);
FastLED.show();
fadeall(); //remove for fill sweep. Also change speed of fill to 30
delay(20); // Speed of fill (50)
}
myLoop = SECOND;
}
void secondLoop(){
// Second loop: LED comet spreads out
for (int i = 0; i <= halfWay; i++) {
leds[halfWay + i] = CRGB(90, 254, 0);
leds[halfWay - i] = CRGB(90, 254, 0);
FastLED.setBrightness(120);
FastLED.show();
fadeall();
delay(20); // Speed of fill (50)
}
myLoop = THIRD;
}
void thirdLoop(){
// Third loop: LED comet returns to the center
for (int i = halfWay - 1; i >= 0; i--) {
leds[halfWay + i] = CRGB(70, 254, 0);
leds[halfWay - i] = CRGB(70, 254, 0);
FastLED.show();
fadeall();
delay(20); // Speed of fill (50)
}
// Turn off all LEDs
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = CRGB(0, 0, 0);
}
FastLED.show();
delay(100);
myLoop = SECOND;
};
void waitWhileButtonPressed(){
while (digitalRead(buttonPin) == LOW) {
delay(500); // Debounce delay to avoid multiple triggers
// Do nothing
}
}
/*
// Read the current state of the button -------------------------------------
buttonState = digitalRead(buttonPin); // Button pressed when HIGH
// Check if the button was just pressed (edge detection)
if (buttonState && !lastButtonState) {
loopStarted = !loopStarted; // Toggle the state of the loop
delay(100); // Debounce delay to avoid multiple triggers
}
// Store the current button state for the next loop iteration
lastButtonState = buttonState;
// If the loop is started, do something
if (loopStarted) {
// Clear all LEDs
FastLED.clear();
FastLED.show();
delay(100);
// Set LED 16 through 21, 52 through 58, 92 through 97 at brightness 255 (4 LEDS ON)
for (int i = 14; i <= 19; i++) {
leds[i] = CRGB(70, 254, 0);
}
for (int i = 52; i <= 58; i++) {
leds[i] = CRGB(70, 254, 0);
}
for (int i = 92; i <= 97; i++) {
leds[i] = CRGB(70, 254, 0);
}
FastLED.setBrightness(255);
FastLED.show();
delay(500);
// Hold until power removed
while (digitalRead(buttonPin) == HIGH) {
// do nothing
// Read the current state of the button -------------------------------------
buttonState = digitalRead(buttonPin); // Button pressed when HIGH
if (digitalRead(buttonPin) == LOW) {
FastLED.clear(); // Clear the LEDs
FastLED.show(); // Update the LED state
delay(1000); // Debounce delay
goto checkAgain;
}
}
}
}
*/