// LED PINS
int led_pins[5] = {13,12,11,10,9};
int led_pins_length = sizeof(led_pins)/sizeof(led_pins[0]);
int led_anim_delay = 100;
int led_index = 0;
// Button setup
int btn_pin = 6;
int btn_value = 0;
int btn_prev_value = 0;
int btn_switch = false;
// Slowdown Animation
int anim_init_slowdown = 300;
int anim_total_slowdowns_max = 4;
int anim_total_slowdowns_count = 999;
bool anim_switch = false;
void setup() {
// Set led pin modes
for (int i = 0; i < led_pins_length; i++){
pinMode(led_pins[i], OUTPUT);
}
// set button
pinMode(btn_pin, INPUT);
}
void loop() {
btn_value = digitalRead(btn_pin);
// Detect button press transition
if (btn_value == HIGH && btn_prev_value == 0) {
btn_switch = !btn_switch; // Toggle the LED animation state
btn_prev_value = 1; // Remember current state
anim_total_slowdowns_count = 0;
delay(50); // Debouncing delay
} else if (btn_value == LOW && btn_prev_value == 1) {
btn_prev_value = 0; // Remember current state
led_index = 0 + rand() % led_pins_length; // randomise position
}
// LED Cycle Animation
if (btn_switch){
digitalWrite(led_pins[led_index], HIGH);
delay(led_anim_delay);
digitalWrite(led_pins[led_index], LOW);
// Play LED animation cycle
led_animation_cycle();
} else {
// Make LED animation slowdown before stopping
if (anim_total_slowdowns_count <= anim_total_slowdowns_max) {
for (int i = 0; i < anim_total_slowdowns_max; i++){
digitalWrite(led_pins[led_index], HIGH);
delay(anim_init_slowdown + 50);
digitalWrite(led_pins[led_index], LOW);
// Reuse led animation cycle for this slowdown
led_animation_cycle();
// Increment total slowdowns
anim_total_slowdowns_count++;
}
}
else {
// Stop and light up specific LED
digitalWrite(led_pins[led_index], HIGH);
}
}
}
// Function for making LED animation switch between left and right
void led_animation_cycle(){
if (anim_switch){
// Make LED animation go left
if (led_index <= 0){
anim_switch = !anim_switch;
} else {
led_index--;
}
} else {
// Make LED animation go right
if (led_index >= led_pins_length-1){
anim_switch = !anim_switch;
} else {
led_index++;
}
}
}