djsfantasi
- Joined Apr 11, 2010
- 9,237
Using delay() would require that the delay interval be the least common denominator of the two intervals. Otherwise, using delay wouldn’t work."using it to blink two LEDs at different intervals would only work if one rate is a multiple of the other."
Consider flashing at 30 and 40 seconds. If you flashed at 30 seconds, then 40 seconds, with delays, one cycle would take 70 seconds. You’d miss flashing at 60 seconds if you delayed for a total of 70 seconds. If the times were 20 seconds and 60 seconds, work out this example and see for yourself that you wouldn’t miss any times.
Using the millis() function, you could define the start and end times for the two LEDs, regardless of how the two times are related. Start one. Continue checking and end at 30 seconds later. Start the second at n seconds. Continue checking and end at 40 seconds later, regardless of the first delay times.
I don’t see how the device would be powered all day if you are sleeping for 20 second intervals. It sounds like at most it might be powered on at most an extra 20 seconds?Which I am pretty sure at this point the battery operated device would continue to run unnecessarily all day until it "woke up again" and resumed it's "on" "off" program.
When scheduling, once the second LDR says it’s time to go to sleep, your program must do what it has to. That is, turn off the device and flash the LED (I think that’s what you said).
To clarify my suggestion, you have six events…
1) Turn the first LED on and calculate the time in milliseconds when you need to shut it off. Turn the device on
2) When it’s time, turn the first LED off
3) When it’s time, turn the second LED on and calculate when to turn it off.
4) When it’s time (based on the LDR input?) , turn the second LED off. Turn the device off.
As far as sleeping, at the beginning of these tests, go to sleep (5).
When you wake, check to see if it’s time to flash the LEDs and check the LDR. At the end of these tests, go to sleep for a time (20 seconds?). When the program awakes, check to see if you need to change the states of the LEDs, turn the device on or check the LDR.
The link I gave you shows how to check for one set of states. You can add more if statements for the other conditions.
Think in terms of discrete states not sequential operations. You know what you want to do when better than I. Adjust the states to match your requirements.
Last edited: