I want to have an integer that keeps track of the number of times the loop statement is looped for exactly one second. How can I do this? I figure there has got to be either a way to end the loop after one second or a function that is called after one second. Kind of like delay(1000). But instead of delaying the loop for one second, I want to print my integer at that exact moment. Thanks!
Before we tell you how to do this, we would need to know why you want to do this. Basing any action on the time that it takes one (or n) iteration(s) of loop to complete is not a good idea.
You can use millis() to see what time it is, relative to when the Arduino restarted.
You just want something simple like this?
starttime = millis();
endtime = starttime;
While ((endtime - starttime) <=1000) // do this loop for up to 1000mS
{
// code here
loopcount = loopcount+1;
endtime = millis();
}
serial.print (loopcount,DEC);
http://arduino.cc/en/Reference/While
endtime, starttime are unsigned long variable types.
You could try it with micros() (microseconds), see if the results change;
Change the compare to 1000000.
I think I understand the question and I also understand Paul's. It would be as simple as:
ilong lCnt = 0;
unsigned long lStart;
void loop
{
if( lCnt == 0)
{
lStart = millis();
}
lCnt++;
if( (millis() - lStart) >= 1000)
{
Serial.Print(lCnt);
while(true) ;
}
}
But Paul's question of why is very relevant. Basing timing on loop counts is a bad idea. The more you do in the loop, the longer they take and the less frequent they are. Instead use elapsed time. When I get a command to move my USV, I start it moving and record the time (using millis()) of the command. In my loop, I check to see if it has been past a certain threshold and stop if I haven't received another command. Otherwise a problem with the base unit could send the remote unit off over the horizon.
Ok. What I am doing is using a photocell and button to record the amount of time that goes by after the photocell detects light and the button is pressed. The purpose is to determine how far away a lightning strike is. I imagine it would be better if I could use some form of microphone instead of button to eliminate human response time, but I don't have one. The millis() function should be fine. If I had the microphone, the micros() would be great, but due to the before mentioned human response time, I don't think it would be necessary. Thanks for your responses!
I just noticed I forgot to explain that my example showed how to get the number of times loop was called in one second starting with the very first call to loop.
electrosheep,
Look at the stopwatch code in posts 82/83 here
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1292540097/75
This code was set up to write out to 7 segment displays via shift register, but it will also display on screen.
Thanks guys. It was kind of a retarded thing to ask. I didn't realize that some basic functions are outlined in the reference tab of the home page. It's all about the timing, so the millis() and micros() functions are more useful to me than the number of times loop is called in a second. Once I get this thing figured out, I'm going to have to write some tutorials for people who are new to this stuff like I am. It's wasn't as easy as I would've liked to find tutorials on the proper usage of photocells, buzzers, thermistors, etc. I got a starter kit that came with no documentation of any sort, and I didn't see anything on this site that demonstrated how to use them. :-/ I can't wait to get out of this learning curve and understand what I'm doing.