The Arduino is a bit like a stop watch. When it powers up, or is reset, it starts the stop watch going.
The millis() function returns how long it has been since the stop watch was started.
What time does that correspond to? The Arduino has no idea, unless you tell it when you start it up, or connect a real time clock that can keep track of the real time.
well im thinking about it like this, start the function ( when the arduino first powers up) run it for 8 1/2 hours, then stop for 15 1/2 hours, then run again, etc...
it doesnt need to know what time it is, just how long to run it for, and how long to stop running it for.
It just returns a number each time it is called.
That number is the number (roughly) of milliseconds since the processor was reset.
Say it returns 765, and you want to wait until another second has elapsed.
So, keep calling "millis" until the value returned, minus the number you first read (765) is greater than or equal to 1000.
Have a look at, and more importantly experiment with, the "blink without delay" example.
You'd think there'd be a halfhour() function, wouldn't you?
Something basic like this would work, flesh out the definitions, pin assignments, other stuff.
Basically, you push the start button at 8:30.
It reads the current value of millis, then sits in a loop reading millis and comparing to the initial read until 8.5 hrs have gone by for interval1.
It then sets some flags and sits in the loop again until 15.5 hrs have gone by for interval2.
Then it repeats.
unsigned long currentmillis = 0; // value from 0 to 4294967295 (hex FFFF FFFF, a 32 bit number)
unsigned long elapsedmillis = 0;
unsigned long previousmillis = 0;
//unsigned long interval1 = 3060000; // = 8.5hrs x 60 min/hr * 60 sec/min * 1000ms/sec
//unsigned long interval2 = 55800000; // = (24-8.5)hrs x 60 min/hr * 60 sec/min * 1000ms/sec
unsigned long interval1 = 5000; // for testing, replace with above
unsigned long interval2 = 5000; // for testing, replace with above
byte interval1_active = 0; // flag =1 to show interval1 in process
byte interval2_active = 0; // flag =1 to show interval2 in process
byte start_button = 2; // pin2 for start button
byte start_button_state = 1; // state of the active low (connect to ground) button
void setup(){
Serial.begin (9600);
pinMode (start_button, INPUT);
digitalWrite (start_button, HIGH); // internal pullup turned on
}
void loop()
{
start_button_state = digitalRead (start_button); // read the active low (internal pullup) start button
if (start_button_state == 0 && interval1_active == 0) // uses interval1 to ignore switch bounce once pushed
{ // once pressed,
Serial.println ("Started!"); // let the user know
interval1_active = 1; // and set a flag for interval1
currentmillis = millis(); // take a snapshot of current time
previousmillis = currentmillis; // sets difference to 0
}
if (interval1_active == 0 && interval2_active == 0) // nothing going on yet
{
Serial.println ("Waiting for 8:30"); // let user know waiting for start time
delay (1000); // but not too often
}
if (interval1_active == 1 && interval2_active == 0){
currentmillis = millis(); // read the time.
elapsedmillis = currentmillis - previousmillis; // compute how much has gone by
if (elapsedmillis >= interval1) // 8.5 hrs have gone by
{
Serial.println ("8.5 hrs elapsed");
interval1_active = 0; // turn off 1st flag
interval2_active = 1; // turn on 2nd
previousmillis = currentmillis; // reset the difference to 0
elapsedmillis = 0;
}
}
if (interval1_active == 0 & interval2_active == 1){
currentmillis = millis(); // read the time.
elapsedmillis = currentmillis - previousmillis; // compute how much has gone by
if (elapsedmillis >= interval2) // (24-8.5) hrs have gone by
{
Serial.println ("15.5 hrs elapsed");
interval1_active = 1;
interval2_active = 0;
previousmillis = currentmillis;
elapsedmillis = 0;
}
}
} // end void looop
Take a look at the Time library. I am looking at a led display matrix. it syncs the time once a week via a ntp server. Seems to keep to good time with or without the sync.
Well here's another fairly clean cut example. Might help.
#define ONEMINUTE 60000
unsigned long lastminute;
int minutes = 0; // either set these to starting values
int hours = 0; // or switch on at midnight!
bool running = false;
void setup(){
// whatever else you want here
lastminute = millis(); // should be last command in setup
}
void loop{
if (millis() - lastminute >= ONEMINUTE){
lastminute += ONEMINUTE;
minutes += 1;
if (minutes == 60){
minutes = 0;
hours += 1;
if (hours == 24){
hours == 0;
}
}
}
if (not running and hours == 8 and minutes == 30){ // just examples
running = true;
}
else if (running and hours == 17 and minutes == 0){
running = false;
}
if (running){
// some function here
}
// some other code here too
}
This will correctly handle millis() wrap around zero and also step over any other code functions provided the overall loop time is less than 1 minute!