Go Down

Topic: Timer (Read 459 times) previous topic - next topic

Lentrave

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

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!

Lentrave


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

Jack Christensen


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: [Select]
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: [Select]
onTime = millis();
relayState = true;


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


Calculate total time on as:
Code: [Select]
totalTime = accumTime + relayState ? millis() - onTime : 0;
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

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.


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!

Osgeld

#5
Mar 22, 2012, 04:46 am Last Edit: Mar 22, 2012, 04:52 am by Osgeld Reason: 1
(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: [Select]
int opening_time = (day == WEEKEND) ? 9 : 12;

instead of the more verbose

Code: [Select]
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?
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Go Up