Very confused about timing.

Okay guys, say i want Arduino to run a function for about 8 hours, ( from 8am to about 3pm, every day.

how would i do that?

Ive been reading about the millis() function, but i cant seem to get a grasp of it, not a single bit. i dont even have any sample code to show :confused:

any help is much appreciated!

Eight hours in milliseconds:
8L * 60L * 60L * 1000L

I'm... Lost. >_<

:confused:

I'm... Lost

Post your code, and we'll draw you a map.

As i have it right now theres only the function code:

int readPin = A0;
int Value;
int Strobe = 6;

void setup() {
  pinMode(readPin, INPUT);
  pinMode(8, OUTPUT);
  pinMode(Strobe, OUTPUT);
  
  attachInterrupt(0, light, LOW);
  
}

void loop() {
  Value = analogRead(readPin);

     if ( Value >= 1) {
       digitalWrite(8, HIGH);
       delay(3000);
     }
     else {
       digitalWrite(8, LOW);
     }
  } 
  
  void light() {
    digitalWrite(Strobe, HIGH);
  }

And what do you want to do?

(Why hasn't poor pin 8 got a nice name?)

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.

hahah, code was drawn up in a hurry, as i need the prototype working :stuck_out_tongue:

i need my Arduino to run the function inside loop() from 8:30 am to 5pm exactly, then shut down until the next day, and start again.

thanks a lot guys, for the attention and help :slight_smile: cheers!

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.

thats what gets me complicated >_<

If the stop watch starts at the on time for a day, then record the first on time:

unsigned long onTime = millis();

Then, in loop, see if the current time minus the onTime is greater than 8/2 hours. If not, do the ON thing. If so, do the OFF thing.

Also, in loop, see if the current time minus the onTime is greater than 24 hours. If so, reset onTime to the current time.

Sounds great. ill try and get it working, thank yoU!

k guys, i still cant figure out the millis() function, does anyone have any code that could help? :S

im sorry for my noobness :confused:

You'd think there'd be a halfhour() function, wouldn't you?

i still cant figure out the millis() function

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?

Now, just you wait a minute.

You'd think there'd be a halfhour() function, wouldn't you?

What would this mythical function do? You're not thinking in terms of delayHalfHour() are you?

Aimed at how to simulate multitasking with millis(), but provides an extended explanation on how it works:

http://www.cmiyc.com/blog/2011/01/06/millis-tutorial/

millis() will work in this application, but you would be able to write much simpler code if you wired up a RTC (such as the ds1307).

Aimed at how to simulate multitasking with millis(), but provides an extended explanation on how it works:
http://www.cmiyc.com/blog/2011/01/06/millis-tutorial/

The code does not work correctly as millis approaches the wrap-around.

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!