Pages: [1]   Go Down
Author Topic: Stop after # of loops  (Read 343 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I modified the LED BLINK code to run 2 solenoids(followed the tutorial on solenoids) and was wondering how do I stop after a specified number of loops say 20,000 actuations? I was also thinking of using a selector with resistors so I can select the number of loops before it stops, say 10K, 20K and 30K, but thats still way ahead of me as Im a real newbie with this stuff. Just stopping the loop will be ok for me now.

Using the USB version.

Thanks

Roy
« Last Edit: May 06, 2007, 06:05:35 pm by royco » Logged

Panucatt Devices

0
Offline Offline
Full Member
***
Karma: 0
Posts: 203
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There isn't a concept of stopping. A possible solution would be to keep a count of how many loops you had executed and then not blinking after that many have passed. Something like...
Code:
void loop()
{
  static int loops = 0; // this static variable holds its value between calls to loop()

  if ( loops > 10000) { // if we have looped too long, then turn off
    digitalWrite(LED,0);
    return; // don't do the rest of the loop function
  } else loops++;  // don't increment after we hit 10000 or we wrap eventually

  // blah blah, the rest of your loop function goes down here
}

Your loop() function will still be called continuously, but it won't be blinking anymore.

(Note: If battery life were an issue, there are ways to halt the processor, but that isn't an issue when you are plugged in.)
« Last Edit: May 06, 2007, 07:49:57 pm by jims » Logged

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

Thanks jims it worked!!  smiley-wink

Roy
Logged

Panucatt Devices

Pages: [1]   Go Up
Jump to: