Go Down

Topic: Simple counting/increment per iteration (Read 817 times) previous topic - next topic

yost87

Ah!!! Rolling over... okay, I can account for that in the future.  It was not knowing why that was driving nuts (not the steering wheel in my lap).

Mucho grassyass! to all of you

Nick Gammon


Quote
Nick Gammon
Global Moderator

This will certainly fail:

Code:

int loops = 3;   //FAIL
while ( loops != 1 ){  //fAIL


Thanks for pointing that out, but why will it certainly fail?


I left out a line, sorry:

Code: [Select]

int loops = 3;   //FAIL
while ( loops != 1 ){  //fAIL
  // loops - 1;


First, you aren't subtracting one. Second, this is not how you subtract one:

Code: [Select]

loops - 1;



What was wrong with my suggestion?

Code: [Select]

for (int loops = 0; loops < 3; loops++)
  {
  // do something
  }


Instead of:

Code: [Select]

void loop() {
  static int loops = 6;   //FAIL - stops after the 3rd time
    if (loops > 0){  //fAIL
    loops =  loops - 1;
    for (int thisPin = 1; thisPin < pinCount; thisPin++) {
  // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                 
  // turn the pin off:
    digitalWrite(thisPin, LOW);   
}


Code: [Select]

void loop()
  {

  for (int loops = 0; loops < 6; loops ++)
    {

    for (int thisPin = 1; thisPin < pinCount; thisPin++)
      {
      digitalWrite(thisPin, HIGH);   
      delay(timer);                 
      digitalWrite(thisPin, LOW);   
      delay(timer);                 
      } // end of for each pin

    }  // end of for each loop

}  // end of loop


http://www.gammon.com.au/electronics

Go Up