Pages: [1]   Go Down
Author Topic: Best solution to schedule sketch  (Read 1229 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Tesla Member
***
Karma: 212
Posts: 8954
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27353
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27353
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27353
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Looks like I forgot to set
previous_millis = current_millis;
after the interval passed, you'll need to add that in.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

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 Offline
Brattain Member
*****
Karma: 545
Posts: 27353
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

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 Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 2
Posts: 380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock

and a lot of links with : http://arduino.cc/playground/Main/InterfacingWithHardware

look in the "TIME" section.
Logged

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

Great thanks for your kind and fast reply !
Logged

Pages: [1]   Go Up
Jump to: