Search code examples
arduinosequencerepeatled

Arduino: moving light repeating sequence


during shutdown of school my son got some homework to do.

One is programming a moving light sequence, 5 led, which should run:

1, 1-2, 1-2-3, 1-2-3-4, 1-2-3-4-5, 1, 1-2, ...

He is only allowed to use a limited choice of orders, no 'if' or 'while'

The result at the moment is:

1, 1-2, 1-2-3, 1-2-3-4, 1-2-3-4-5, 1-2-3-4-5, 1, 1-2, ...

Here's the code:

void setup() { 

 for (int a=1; a<6; a++) {  
  pinMode(a, OUTPUT);     
 } 
} 
void loop() {
 for (int a=1; a<6; a++) {
  digitalWrite(a, HIGH);
  delay(300);
  digitalWrite(a,LOW);
  delay(300);
 for (int b=1; b<=a; b++) {
   digitalWrite(b, HIGH);
   delay(300); 
   digitalWrite(b,LOW);
   delay(300);
  }
 }
}

Thanks

Markus

Edit: Strange, seems like 'Hi' and 'hallo' aren't allowed to start the post with :-(


Solution

  • Got it, read through the instruction booklet from school...

    void setup() { 
    for (int a=1; a<6; a++) {  
    pinMode(a, OUTPUT);     
    } 
    } 
    void loop() {
    for (int a=1; a<6; a++) {
    for (int b=1; b<=a; b++) {
    digitalWrite(b, HIGH);
    delay(300); 
    digitalWrite(b,LOW);
    delay(300);
    }
    }
    }
    

    Don't know how to name it correctly in English, but it was the double "digital write" (digitalwrite(a,..); digitalwrite(b,..)) in the loop sequence