Timer

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

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!

garagelab:
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 :stuck_out_tongue:

garagelab:
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:

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:

onTime = millis();
relayState = true;

When the Arduino turns the relay off, accumulate the total time and again capture the state:

accumTime += millis() - onTime;
relayState = false;

Calculate total time on as:

totalTime = accumTime + relayState ? millis() - onTime : 0;

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

(wikipedia links dont want to play nice tonight)

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

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

instead of the more verbose

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?