Pages: [1]   Go Down
Author Topic: more loops  (Read 87 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Everybody,  I have written a small sketch in the most basic form  and I cant get my head around something.

The sketch is a simple watering system and so far I have managed to get it all working, then I decided I wanted to add a routine that only kicked in every 20 cycles.

so what I need to know is how do I get the main part of the program to run 20 times then run another part of the program once, then start again.

so it goes round part one of the program 20 times   then goes into part 2,  then starts all over again, part one of the program 20 times   then goes into part 2
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void loop ()
{
  for (int x = 0; x < 20; x++)
  {
    doOneThing ();
  }
  doAnotherThing ();
}
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

An alternative:
Code:
int counter = 0;

void loop()
{
   // do the every time stuff

   counter++;
   if(counter == 20)
   {
       // do the every 20th time stuff

      counter = 0;
   }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Guys, works a treat
Logged

Pages: [1]   Go Up
Jump to: