France
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: April 26, 2011, 12:35:01 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Massachusetts, USA
Offline
Tesla Member
Karma: 96
Posts: 6339
|
 |
« Reply #1 on: April 26, 2011, 01:08:12 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 240
Posts: 16433
Available for Design & Build services
|
 |
« Reply #2 on: April 26, 2011, 01:25:03 pm » |
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 } } }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 240
Posts: 16433
Available for Design & Build services
|
 |
« Reply #3 on: April 26, 2011, 01:31:35 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 240
Posts: 16433
Available for Design & Build services
|
 |
« Reply #4 on: April 26, 2011, 01:34:00 pm » |
Looks like I forgot to set previous_millis = current_millis; after the interval passed, you'll need to add that in.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #5 on: April 26, 2011, 02:22:03 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 240
Posts: 16433
Available for Design & Build services
|
 |
« Reply #6 on: April 26, 2011, 10:04:07 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #7 on: April 28, 2011, 04:56:20 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #8 on: April 28, 2011, 05:03:43 am » |
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 ?
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Sr. Member
Karma: 2
Posts: 378
|
 |
« Reply #9 on: April 28, 2011, 05:35:12 am » |
|
|
|
|
|
Logged
|
|
|
|
|
France
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #10 on: April 28, 2011, 07:05:49 am » |
Great thanks for your kind and fast reply !
|
|
|
|
|
Logged
|
|
|
|
|
|