5

For my application I need to simulate the sunlight including the rising and setting of the sun.

I have a program that will light up some LEDs slowly over a period of time but I need to know what to type in the (delay on) to keep the lights on for 7 hours?

Will it work if I just replace delay(1000) with delay(25200000UL) ? I don't want to run anything else at the same time and don't want to use or understand millis(). Thanks for any help.

sa_leinad
3,2182 gold badges23 silver badges51 bronze badges
asked Aug 10, 2017 at 12:48
6
  • Do you want "light up slowly over time" and then wait for 7 hours? Commented Aug 10, 2017 at 12:52
  • Hi, I already have it lighting up slowly over time just need to stay on for 7 hours then slowly turn off. Commented Aug 10, 2017 at 13:09
  • 1
    This is the kind of problem that you can resolve with a single test. Make a sketch, put the delay and see what happen. Commented Aug 10, 2017 at 13:12
  • It does upload but with an error but still compiles, tried again and no error...seems to be working. Commented Aug 10, 2017 at 13:15
  • Very new to this, it seems to be working now. Thankyou very much for your responce. Commented Aug 10, 2017 at 13:19

5 Answers 5

6

Yes, absolutely, it should work. But your code will be easier to read if you use some constants, like:

const unsigned long SECOND = 1000;
const unsigned long HOUR = 3600*SECOND;
delay(7*HOUR);
answered Aug 10, 2017 at 15:00
3

Yes you can write delay(25200000UL) and it will delay for 7 hours.

How it works
delay(x) will delay for x number of milliseconds.
1 second = 1000 milliseconds
1 minute = 60 seconds
1 hour = 60 minutes
7 hours = 1000 * 60 * 60 * 7 = 25,200,000

This number is quite large but is well within the scope of an unsigned long: 32 bits = (2^32)-1 = 4,294,967,295. Doing the sums, this will last for 49 days and 17 hours.

Delaying for>49 days
If for some reason you needed to go longer than 49 days, you can do so by placing the delay inside a for loop.

const unsigned long ONE_DAY = 24UL*3600*1000;
const unsigned int numOfDays = 51;
int i;
int j;
for( i=0; i<numOfDays; i++ )
{
 delay(ONE_DAY);
}
answered Aug 11, 2017 at 1:22
1
  • You wrote: 1000*3600*24UL. This will overflow. You mean 1000UL*3600*24: multiplication is left-to-right associative in C++. You also wrote i<7 while you probably meant i<numOfDays. Commented Aug 11, 2017 at 6:59
1

It's an old one but I give an answer. I think someone needs help like you.

I trying to make a fish meal machine with the Arduino Uno.

In the first. I counted seconds over a minute. It works well. But when I try over an hour it does not work.

After all. I got to know another way to count a long time

In the second. I used the SimpleTimer.h. The link is here. Arduino SimpleTimer

It's an easy way to makes a circular moving but it has the same problem. SimpleTime can't make a count over time. I think it's 30 seconds.

Now I referenced StackExchange Arduino. Here is my code.

#include <SimpleTimer.h>
#include <Servo.h>
#define FIVE_SEC 5000UL
#define ONE_MIN 60000UL
#define FIVE_MIN 300000UL
#define TEN_MIN 3000000UL
#define ONE_HR 3600000UL
#define EIGHT_HRS 28800000UL
#define TWELVE_HRS 43200000UL
SimpleTimer timer;
Servo servo;
int servoPin = 9;
int servoReadyAngle = 180;
int servoMoveAngle = 170;
int servoMoveCnt = 0;
int servoJobCycle = 35000;
unsigned long startTime;
void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 servo.attach(servoPin);
 startTime = millis(); 
}
void loop() {
 if(millis()- startTime > TEN_MIN) {
 startTime = millis();
 giveAFishMeal();
 } else {
 Serial.print("ping ");
 Serial.println(millis());
 }
}
void giveAFishMeal() {
 int servoAngle;
 if(servoReadyAngle > servoMoveAngle) {
 servoAngle = servoReadyAngle - servoMoveAngle;
 } else {
 servoAngle = servoReadyAngle + servoMoveAngle;
 }
 Serial.println("I give a meal to your fish");
 delay(500);
 servo.write(servoReadyAngle);
 delay(500);
 servo.write(servoAngle);
 delay(500);
}
answered Jul 9, 2019 at 14:57
0

delay(1000) is a delay in ms. So for a 7 hour delay you need 7 hours * 60 minutes * 60 seconds * 1000 = 52,200,000

Adding UL is good, to show it's long and unsigned.

Also it fits in an unsigned long which has a max value of 2^32-1 = 4,294,967,295.

If you really want to test it you can wait 7 hours, or use a smaller number (like dividing by 60 * 60, thus 7,000 which should be 7 seconds, or make it 70.000 and check it is 70 seconds.

If you would want to make it explicitly how 52,200,000 is calculated or want to make your program more flexible you can always create some defines (or constants):

static const uint32_t DELAY_1_S = 1000UL;
static const uint32_t DELAY_1_MINUTE = DELAY_1S * 60UL;
static const uint32_t DELAY_1_HOUR = DELAY_1MINUTE * 60UL;

than you can use

delay(7UL * DELAY_1_HOUR); // Delay 7 hours
answered Aug 10, 2017 at 14:01
0

For my application I need to simulate the sunlight including the rising and setting of the sun.

if you have to simulate that long of a delay, you are doing something seriously wrong.

but if you do want to go down that path, something like this may work

#define delay_1hr() delay(1000ul * 60 * 60) //delay a hour
//delay a day
void delay_hr(uint8_t hr) {
 while (hr--) delay_1hr();
}
answered Aug 11, 2017 at 13:12

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.