Question. Loop principle. 5 diodes

Hi, i have a problem with understanding a loop in couple with incremental function.
Here is a link that simmulates work of this one. LINK
I don’t understand why every new loop of blinks of diodes starts from n+1 diode (output pin).

int i;
int n;
void setup() {
  for(i=2;i<7;i++)
  {
    pinMode(i,OUTPUT);
  }
}
void loop() 
{
 for(n=1;n<6;n++)
 {
  switch(n)
  {
    case 1:
    digitalWrite(2,HIGH);
    delay(500);
    digitalWrite(2,LOW);
    delay(500);
     
     case 2:
    digitalWrite(3,HIGH);
    delay(500);
    digitalWrite(3,LOW);
    delay(500);
     
     case 3:
    digitalWrite(4,HIGH);
    delay(500);
    digitalWrite(4,LOW);
    delay(500);
     
     case 4:
    digitalWrite(5,HIGH);
    delay(500);
    digitalWrite(5,LOW);
    delay(500);
     
     case 5:
    digitalWrite(6,HIGH);
    delay(500);
    digitalWrite(6,LOW);
    delay(500);
  }
 }
}

Thank you.

_5diodes.ino (650 Bytes)

Your ‘case’ needs a ‘break’

.

Before you post your code, try placing your cursor in the IDE's source code window and pressing Ctrl-T. It will reformat your code to a common C style that's a little easier for all to read.

Or press

econjack: ... And pressing Ctrl-T....

Or cmd-t if you are on a Mac

:)

totallnoob:
Hi, i have a problem with understanding a loop in couple with incremental function.
Here is a link that simmulates work of this one. LINK
I don’t understand why every new loop of blinks of diodes starts from n+1 diode (output pin).

int i;

int n;
void setup() {
  for(i=2;i<7;i++)
  {
    pinMode(i,OUTPUT);
  }
}
void loop()
{
for(n=1;n<6;n++)
{
  switch(n)
  {
    case 1:
    digitalWrite(2,HIGH);
    delay(500);
    digitalWrite(2,LOW);
    delay(500);
   
    case 2:
    digitalWrite(3,HIGH);
    delay(500);
    digitalWrite(3,LOW);
    delay(500);
   
    case 3:
    digitalWrite(4,HIGH);
    delay(500);
    digitalWrite(4,LOW);
    delay(500);
   
    case 4:
    digitalWrite(5,HIGH);
    delay(500);
    digitalWrite(5,LOW);
    delay(500);
   
    case 5:
    digitalWrite(6,HIGH);
    delay(500);
    digitalWrite(6,LOW);
    delay(500);
  }
}
}




Thank you.

You’re kind of on the track, but the big switch shows you’re not :slight_smile:

void setup() {
  for(int i=2;i<7;i++) { // counts from 2 up to and including 6
    pinMode(i,OUTPUT);
  }
}
void loop()
{
  for (int i=2; i<7; i++) {
    digitalWrite(i,HIGH);
    delay(500);
    digitalWrite(i,LOW);
    delay(500);
  }
}

:slight_smile:

you can even further “simplify” and use the loop function to get rid of the for iteration there

int currentLed;

void setup() {
  for(int i=2;i<7;i++) { // counts from 2 up to and including 6
    pinMode(i,OUTPUT);
  }
 currentLed = 2;
}

void loop()
{
    digitalWrite(currentLed,HIGH);
    delay(500);
    digitalWrite(currentLed,LOW);
    delay(500);
    if (++currentLed >= 7) currentLed = 2;
}

and if you are bold and understand in which sequence parameters get evaluated you can even go one step further and have only 1 digital write in the body and have the sequence work on the pin you tick only increments every 2 loops run

    digitalWrite(do_some_smart_math(&currentLed),!digitalRead(do_some_smart_math(&currentLed)));
    delay(500);

J-M-L:
you can even further “simplify” and use the loop function to get rid of the for iteration there

int currentLed;

void setup() {
  for(int i=2;i<7;i++) { // counts from 2 up to and including 6
    pinMode(i,OUTPUT);
  }
currentLed = 2;
}

void loop()
{
    digitalWrite(currentLed,HIGH);
    delay(500);
    digitalWrite(currentLed,LOW);
    delay(500);
    if (++currentLed >= 7) currentLed = 2;
}




and if you are bold and understand in which sequence parameters get evaluated you can even go one step further and have only 1 digital write in the body and have the sequence work on the pin you tick only increments every 2 loops run 



digitalWrite(do_some_smart_math(&currentLed),!digitalRead(do_some_smart_math(&currentLed)));
    delay(500);

Yes that’s true, but for loops inside the loop() function, is more efficient, since they reduce the number of function calls. So there …

:smiley:

Rupert909: Yes that's true, but for loops inside the loop() function, is more efficient, since they reduce the number of function calls. So there ...

:-D

if you want to be really smart then in the loop you can play with PORTD and just shift a 1 around with bitwise operators (PORTD is for pins 0 to 7 so you'll need to mask the 2 lower bit to not touch them)