2

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.

  • Do you want "light up slowly over time" and then wait for 7 hours? – user31481 Aug 10 '17 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. – Steve Aug 10 '17 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. – user31481 Aug 10 '17 at 13:12
  • It does upload but with an error but still compiles, tried again and no error...seems to be working. – Steve Aug 10 '17 at 13:15
  • Very new to this, it seems to be working now. Thankyou very much for your responce. – Steve Aug 10 '17 at 13:19
4

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);
| improve this answer | |
2

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);
}
| improve this answer | |
  • 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. – Edgar Bonet Aug 11 '17 at 6:59
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
| improve this answer | |
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();
}
| improve this answer | |
0

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

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.