Pages: [1]   Go Down
Author Topic: [newbie] timer countdown...basics  (Read 1346 times)
0 Members and 1 Guest are viewing this topic.
Argentina
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
EPIC n00b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, im tring to do a miltiple timer... its has to do an action after some intervals...
there is also a button read involved and i have no idea on how to do something like:
if millis() == 14000
print 14 secconds...

any examples out there?
Logged

--

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (millis() % 1000 == 0) {
  Serial.print(millis()/1000);
  Serial.println(" seconds");
}

This will print time since startup, in seconds (at least I think so. snippet NOT TESTED).
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49338
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In AlphaBeta's example the two calls to millis() may return different values. If that's OK, use his code.

If not, create a local variable:
Code:
unsigned long now = millis();
Then, use the local variable, now, in place of both calls to millis.
Code:
if (now % 1000 == 0) {
  Serial.print(now/1000);
  Serial.println(" seconds");
}

« Last Edit: September 16, 2010, 08:33:55 pm by PaulS » Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 310
Posts: 40382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you didn't know already, the % in their code is called modulus.  It's the remainder from division.  5 % 3 would equal 2.  7 % 3 equals 1.  The modulus is how much is left over.  
You could use if (now==14000) to print it just one time.
Logged

Norway@Oslo
Offline Offline
Edison Member
*
Karma: 13
Posts: 2033
loveArduino(true);
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually... when I think about it, I think using % 1000 == 0 is a bad idea because then you need to run that code EXACTLY when the millis return 1000 2000 3000 ...
if ( (now % 1000) < 50 ) {/*second has passed*/ }
This is more likely to work, but then again, you might get more than one printout. You could store a previousSecond variable to check that you only print a second once.
Logged

Argentina
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
EPIC n00b
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for your replies, this seems to bit a bit more harder than i first tought
so im considering on trying this: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1217881285

i dont need a high precision, but since the last time i played with electronics the most complex thing i learned was about the 555 this a totally different concept

my idea is to have an array or something of 3 times and 3 steps for a servo, so after pressing start those actions should occur
Logged

--

Pages: [1]   Go Up
Jump to: