0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« on: January 14, 2011, 02:55:51 pm » |
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!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: January 14, 2011, 02:57:27 pm » |
Eight hours in milliseconds: 8L * 60L * 60L * 1000L
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« Reply #2 on: January 14, 2011, 03:02:50 pm » |
I'm... Lost. >_<
:/
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: January 14, 2011, 03:04:00 pm » |
I'm... Lost Post your code, and we'll draw you a map.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« Reply #4 on: January 14, 2011, 03:05:12 pm » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #5 on: January 14, 2011, 03:08:54 pm » |
And what do you want to do?
(Why hasn't poor pin 8 got a nice name?)
|
|
|
|
« Last Edit: January 14, 2011, 03:09:33 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #6 on: January 14, 2011, 03:09:34 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« Reply #7 on: January 14, 2011, 03:10:06 pm » |
hahah, code was drawn up in a hurry, as i need the prototype working  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!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« Reply #8 on: January 14, 2011, 03:11:50 pm » |
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 >_<
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #9 on: January 14, 2011, 03:19:26 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« Reply #10 on: January 14, 2011, 03:21:33 pm » |
Sounds great. ill try and get it working, thank yoU!
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 28
Arduino rocks
|
 |
« Reply #11 on: January 14, 2011, 03:38:46 pm » |
k guys, i still cant figure out the millis() function, does anyone have any code that could help? :S
im sorry for my noobness :/
|
|
|
|
|
Logged
|
|
|
|
|
London, GB
Offline
Sr. Member
Karma: 7
Posts: 332
Nothing works.
|
 |
« Reply #12 on: January 14, 2011, 04:03:14 pm » |
You'd think there'd be a halfhour() function, wouldn't you?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #13 on: January 14, 2011, 04:07:35 pm » |
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.
|
|
|
|
« Last Edit: January 14, 2011, 04:09:49 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #14 on: January 14, 2011, 04:41:47 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
|