Stop after # of loops


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.



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...

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
    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.)

Thanks jims it worked!! :wink: