#include <Arduino.h>
#define LED_1_PIN 19 // most left
#define LED_2_PIN 18
#define LED_3_PIN 5
#define LED_4_PIN 17
#define LED_5_PIN 16 // most right
#define BTN_LEFT_PIN 22
#define BTN_RIGHT_PIN 23
#define LED_MODE_OFF 0
#define LED_MODE_LEFT_TO_RIGHT 1
#define LED_MODE_RIGHT_TO_LEFT 2
#define DELAY_TIME 100
// Globale Variablen
int led_array[5] = {LED_1_PIN, LED_2_PIN, LED_3_PIN, LED_4_PIN, LED_5_PIN};
int led_index = 0;
int direction = LED_MODE_OFF;
unsigned long last_led_update_time = 0;
// Funktionsprototyp
void run_led_chaser(int direction);
void setup()
{
// put your setup code here, to run once:
pinMode(BTN_LEFT_PIN, INPUT_PULLDOWN);
pinMode(BTN_RIGHT_PIN, INPUT_PULLDOWN);
// LEDS
for (int i = 0; i < 5; i++)
{
pinMode(led_array[i], OUTPUT);
}
}
void loop()
{
// put your main code here, to run repeatedly
if (HIGH == digitalRead(BTN_LEFT_PIN))
{
direction = LED_MODE_RIGHT_TO_LEFT;
}
else
{
direction = LED_MODE_LEFT_TO_RIGHT;
}
run_led_chaser(direction);
}
void run_led_chaser(int direction)
{
auto current_millis = millis();
if (current_millis - last_led_update_time > DELAY_TIME)
{
last_led_update_time = current_millis;
// time to do something.
digitalWrite(led_array[led_index], LOW);
switch (direction)
{
case LED_MODE_LEFT_TO_RIGHT:
led_index++;
if (led_index > 4)
{
led_index = 0;
}
break;
case LED_MODE_RIGHT_TO_LEFT:
led_index--;
if (led_index < 0)
{
led_index = 4;
}
break;
default:
//LED_MODE_OFF -> return early.
return;
break;
}
digitalWrite(led_array[led_index], HIGH);
}
}