Pages: 1 [2]   Go Down
Author Topic: Simple counting/increment per iteration  (Read 638 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
loops - 1;


What was wrong with my suggestion?

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

Instead of:

Code:
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:
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

Logged

Pages: 1 [2]   Go Up
Jump to: