Go Down

### Topic: Simple counting/increment per iteration (Read 1 time)previous topic - next topic

#### yost87

#15
##### Nov 08, 2012, 11:00 pm
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

#16
##### Nov 09, 2012, 12:18 am

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;   //FAILwhile ( 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  }`

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`

Please post technical questions on the forum, not by personal message. Thanks!

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

Go Up

Please enter a valid email to subscribe