Go Down

Topic: Very confused about timing. (Read 3737 times) previous topic - next topic

Roddiy

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

any help is much appreciated!

AWOL

Eight hours in milliseconds:
8L * 60L * 60L * 1000L
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Roddiy


AWOL

Quote
I'm... Lost

Post your code, and we'll draw you a map.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Roddiy

As i have it right now theres only the function code:
Code: [Select]

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

AWOL

#5
Jan 14, 2011, 09:08 pm Last Edit: Jan 14, 2011, 09:09 pm by AWOL Reason: 1
And what do you want to do?

(Why hasn't poor pin 8 got a nice name?)
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

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.

Roddiy

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

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 :) cheers!

Roddiy

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

PaulS

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.

Roddiy

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

Roddiy

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

im sorry for my noobness :/

u0421793

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

AWOL

#13
Jan 14, 2011, 10:07 pm Last Edit: Jan 14, 2011, 10:09 pm by AWOL Reason: 1
Quote
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.

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

Now, just you wait a minute.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Quote
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?

Go Up