Go Down

Topic: Best solution to schedule sketch (Read 1 time) previous topic - next topic

Jeffm

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.

johnwasser

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.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

CrossRoads

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: [Select]

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
}
}
}





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.

CrossRoads

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.
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.

CrossRoads

Looks like I forgot to set
previous_millis = current_millis;
after the interval passed, you'll need to add that in.
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.

Jeffm

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. 

CrossRoads

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.
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.

Jeffm

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.

Jeffm

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 ?


Jeffm

Great thanks for your kind and fast reply !

Go Up