Go Down

Topic: Stop after # of loops (Read 486 times) previous topic - next topic

royco

May 07, 2007, 01:04 am Last Edit: May 07, 2007, 01:05 am by royco Reason: 1
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
Panucatt Devices

jims

#1
May 07, 2007, 02:48 am Last Edit: May 07, 2007, 02:49 am by jims Reason: 1
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: [Select]
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.)

royco

Thanks jims it worked!!  ;)

Roy
Panucatt Devices

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy