Pages: [1]   Go Down
Author Topic: Timer  (Read 431 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i want to do something but i don't know how, maybe same one can help me. I have a relay controlled by an arduino. I want to display the on time of this relay. How many hours and minutes this relay is on since the arduino start.

Tanks
Logged

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

Lentrave,

What about using the ATmega328 timer. You can have an interruption each 10 seconds or 1 minute, just to check the Relay state, and make the math. It is important to use timers because they are precise. Functions like millis() are not ok for this application.

Here you may find a good tutorial bout timers: http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

Another good source of information, I a place to meet people and exchange ideas and projets is GarageLab.com

Good luck!
Logged

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

Lentrave,

What about using the ATmega328 timer. You can have an interruption each 10 seconds or 1 minute, just to check the Relay state, and make the math. It is important to use timers because they are precise. Functions like millis() are not ok for this application.

Here you may find a good tutorial bout timers: http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

Another good source of information, I a place to meet people and exchange ideas and projets is GarageLab.com

Good luck!

tanks ... i read this but it seem over my knowledge in arduino smiley-razz
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4093
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

What about using the ATmega328 timer. You can have an interruption each 10 seconds or 1 minute, just to check the Relay state, and make the math. It is important to use timers because they are precise. Functions like millis() are not ok for this application.

millis() is implemented with a timer. I don't see the difference. millis() and anything else done with the timers will be as accurate as the system clock.

If it were me, I'd try the following. An RTC could also be used, but most don't have millisecond resolution; whether that is an issue for this particular application I have no idea.

Declare some variables:
Code:
unsigned long onTime;    //the time the relay was turned on
unsigned long accumTime;    //accumulated time the relay has been on for previous cycles
unsigned long totalTime;  //total time the relay has been on, including the current cycle
boolean relayState;    //current relay state, on or off, i.e. true or false

When the Arduino turns the relay on, capture the time and state:
Code:
onTime = millis();
relayState = true;

When the Arduino turns the relay off, accumulate the total time and again capture the state:
Code:
accumTime += millis() - onTime;
relayState = false;

Calculate total time on as:
Code:
totalTime = accumTime + relayState ? millis() - onTime : 0;
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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

What about using the ATmega328 timer. You can have an interruption each 10 seconds or 1 minute, just to check the Relay state, and make the math. It is important to use timers because they are precise. Functions like millis() are not ok for this application.

millis() is implemented with a timer. I don't see the difference. millis() and anything else done with the timers will be as accurate as the system clock.

If it were me, I'd try the following. An RTC could also be used, but most don't have millisecond resolution; whether that is an issue for this particular application I have no idea.


Thanks for the help .... what the relayState change in calculation of total time?
What the "?" mean ? could you please explain
Thanks a lot!
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(wikipedia links dont want to play nice tonight)

Quote
The conditional operator's most common usage is to make a terse simple conditional assignment statement. For example, if we wish to implement some C code to change a shop's opening hours from 12 o'clock during weekdays to 9 o'clock on weekends, we may use

Code:
int opening_time = (day == WEEKEND) ? 9 : 12;

instead of the more verbose

Code:
int opening_time;
 
if (day == WEEKEND)
    opening_time = 9;
else
    opening_time = 12;

I wish people would stop using it without expatiation since it does not appear on the reference pages, some of us (me) are c-tarded don't ya know?
« Last Edit: March 21, 2012, 10:52:03 pm by Osgeld » Logged


Pages: [1]   Go Up
Jump to: