// Forum: https://forum.arduino.cc/t/how-backwards-functions-in-code/1101167
// This Wokwi project: https://wokwi.com/projects/359119719077946369
// Previous sketch: https://wokwi.com/projects/359074849643556865
// The first sketch: https://wokwi.com/projects/359042982266722305
// This is the same as the previous sketch, but now the leds go in the other directions.
const int LED_COUNT = 6;
int LED_PINS[LED_COUNT] = {3, 5, 6, 9, 10, 11};
int currentPosition = 0;
const int delayTime = 400;
void setup()
{
for (int i = 0; i < LED_COUNT; i++)
{
pinMode(LED_PINS[i], OUTPUT); // OUTPUT and default also LOW
}
}
void loop()
{
// Intro sequence
// Roll in a led from the most right position to the left.
// Keep the left led on, and continue with the next led
for (int i=LED_COUNT-1; i>=0; i--) // start with the most left led
{
for (int j=0; j<=i; j++) // roll in leds from the right
{
digitalWrite(LED_PINS[j], HIGH);
delay(delayTime);
if (j!=i) // keep the current led on
{
digitalWrite(LED_PINS[j], LOW);
}
}
}
// All the leds are on now.
delay(2000);
// Outro sequence
// Leds are rolling out to the right, until all are off
for (int i=0; i<LED_COUNT; i++) // start with the most right led
{
for (int j=i; j>=0; j--) // roll out leds to the right
{
if (j!=i) // keep the current led off
{
digitalWrite(LED_PINS[j], HIGH);
}
delay(delayTime);
digitalWrite(LED_PINS[j], LOW);
}
}
// All the leds are off now.
delay(2000);
}