//variables
const unsigned int LEDstart = 6;
const unsigned int pause = 400;
const unsigned int LEDcount = 7;
const unsigned int LEDend = LEDcount+LEDstart-1;
const int buttonPIN = 4;
int LEDactive = LEDstart;
int step = 1;
int i;
void setup() {
pinMode(4, INPUT); //set pin 4 as INPUT (button)
Serial.begin(9600); //serial monitor setup
//assign pins
for(int pin = LEDstart; pin < LEDcount; pin++)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
}
void loop() {
for( i = 0; i <= 2; i++) //activate LED area(because 0,1,2 are 3 LEDs)
{
if(LEDstart <= LEDactive-i*step && LEDactive-i*step <= LEDend) //LEDactive-i*step = LEDactive-0*step(erste LED die an ist) + LEDactive-1*step (zweite LED die an ist) + LEDactive-2*step (dritte LED die an ist)
{
digitalWrite(LEDactive-i*step, HIGH);
}
}
delay(pause);
int buttonPushed = digitalRead(buttonPIN);
if(buttonPushed == HIGH) //check button press
{
for(i = 0; i <= 2; i++) //deactivate LED area
{
if(LEDstart <= LEDactive-i*step && LEDactive-i*step <= LEDend)
{
digitalWrite(LEDactive-i*step, LOW);
}
}
//setup for changing the direction
LEDactive += step;
if(LEDactive == LEDstart-3 || LEDactive == LEDend+3)
{
step *= -1;
LEDactive += 2*step;
}
}
}