It'll be great if you could advice me on which arduino software solution (timer, interrupt, alarm, millis,...) and sketch structure to use to schedule the following (on Arduino Mega):
each day, starting at 7 o'clock in the morning and ending at 10 o'clock at night,
to sense every 5 minutes the status of 8 switches and display the result ("X" switch on 8 have the status "On").
Great thanks in advance for your kind help.
The Arduino clock (millis(), etc) will drift over time. If you don't mind resetting the time every week or so it might be a usable solution. Otherwise you will want an external clock.
Something simple like this should work. Let me know if you can't follow it. Needs some cleanup (;s) and flesh out the sensors you read, but the flow is pretty clear.
unsigned long current_millis = 0;
unsigned long previous_millis = 0;
unsigned long elapsed_millis = 0;
unsigned long interval = 300000UL; // 5 x 60 seconds x 1000mS
end_of_day = 0; // 180 = 15 hrs x 12 five minute intervals
morning = 0; // 108 = 9 hours x 12 five minute intervals
interval_type = 0; // 0 = day,1=night
void setup(){
define I/O pins
{ wait for button press to sync time to 7:00 for very first start, or after a restart}
current_millis = millis()
previous_millis = current_millis();
}
void loop(){
elapsed_millis = current_millis - previous_millis
if (elapsed_millis >= interval && interval_type = 0 ){
read the 8 switches
display the results
end_of_day = end_of_day + 1;
if (end_of_day == 180){
end_of_day = 0
interval_type = 1
}
}
if(elapsed_millis >=interval && interval_type == 1){
end_of_night = end_of_night +1
if (end_of_night == 108){
end_of_night = 0
interval_type = 0
}
}
}
You could do a search for "event manager" or "event scheduler" that was posted recently and see how hard it would be to schedule 180 events. I think the approach above is pretty straightforward tho.
Looks like I forgot to set
previous_millis = current_millis;
after the interval passed, you'll need to add that in.
Thanks a lot for your very fast reply.
I have to test your solution and enlarge search following your advices.
Is that a solution combining alarm and timer could be an effective alternative possible?
if yes, how to start function and stop it, containing timer ?
To be more precise it should be noted that the sketch will run all over the year and must be stable every days of the weeks.
You are certainly able to add some more buttons and a display to show the time, an indicator that a reading is taking place, etc.
Start with the basics, build up from there.
I beg your pardon, but as i'm novice, could you please give me more details about the following part and how to do that:
"{ wait for button press to sync time to 7:00 for very first start, or after a restart}"
Great thanks.
On other hand, what do you think of using an external real time clock hardware (like DS1307 that i've discovered) ? Is it easier to implement and use? Is it reliable? Do you have code examples of using that type of real time clock hardware to schedule events ?
http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
and a lot of links with : Arduino Playground - InterfacingWithHardware
look in the "TIME" section.
Great thanks for your kind and fast reply !