Understanding milliseconds in a script from a friend.

Using a sketch someone else wrote for controlling an LED. They have some things labeled as hours and minutes but the values in milliseconds don’t match. Am I reading this wrong or missing something or is the value set wrong.

void loop (){
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
{analogWrite(ledPin11, fadeValue);
delay(7059);} //half hour sunrise
delay(39600000);// 11 hr day

//2min cloud cover//
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
{analogWrite(ledPin11, fadeValue);
delay(470);}

The half hour sunrise is set to 7059 milliseconds which is under 1 minute is it not?

Does this help you? I have adjusted the formatting.

void loop (){
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
  {
    analogWrite(ledPin11, fadeValue);
    delay(7059);
  } //half hour sunrise      
  delay(39600000);// 11 hr day

  //2min cloud cover//
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {
    analogWrite(ledPin11, fadeValue);
    delay(470);
  }

Look at the for loop. It loops through 256 times delaying 7059 milliseconds each time. That is 1807104ms = 1807.104s = 30.1184minutes

As for the 11 hour day, 39600000ms = 39600s = 660mins = 11 hours.

And another for loop gives 256*470 = 120320ms = 120.320s ~= 2mins of “cloud”

That is an atrocious coding style which obfuscates the control structure of the code. No wonder you’re having trouble figuring out how often each piece of code is called. Tom Carpenter’s version is a vast improvement. I recommend you follow Tom’s style in future but with one small change - don’t put the opening brace on the same line as the function signature. Use this:

void loop ()
{
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
  {
    ...

instead of this:

void loop (){
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
  {
    ...

Yeah thats what I would normally do as well (brace with the function name). In this case it was the IDE which did the formatting, and its autoformat seems to prefer it on another line.

Thanks for the replies guys. I’m understanding this a little better now. There is one issue I see in my sketch. This is to control the LED light on my salt water tank. I need 8 hours of light at 100%, 30min sunrise and 30min sunset. Now I bought an RTC thinking that I would use it to control the cycle. But the guy who wrote this script says he didn’t use one. So I figured he wrote a full 24 hour cycle but when I add up all the time in the script its way short of 24 hours.

Here is the full sketch if someone can explain or see what I’m missing here. And yes, it is ugly. If you see any way to improve the function or code please point it out. I was wondering if there is a way to handle the lightning storm lengths randomly.

// Keihi 24hr 1/2 sunrise/sunset + 10 minute storm
int ledPin11 = 11;

void setup (){}
void loop (){
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
  {analogWrite(ledPin11, fadeValue);
  delay(7059);} //half hour sunrise
  delay(39600000);// 11 hr day
  
  //2min cloud cover//
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {analogWrite(ledPin11, fadeValue);
  delay(470);}
  
  //lightning storm (10mins)//
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(40);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(50);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(40);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(50);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10);
  digitalWrite(11, HIGH);  
  delay(20);              
  digitalWrite(11, LOW);  
  delay(100000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(40);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(50);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(40);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(50);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10);
  digitalWrite(11, HIGH);  
  delay(20);              
  digitalWrite(11, LOW);  
  delay(100000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(40);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(50);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(40);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(50);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(200);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(1000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(70);              
  digitalWrite(11, LOW);  
  delay(10000);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(10);
  digitalWrite(11, HIGH);  
  delay(20);              
  digitalWrite(11, LOW);  
  delay(71000);
  digitalWrite(11, HIGH);  
  delay(20);              
  digitalWrite(11, LOW); 
  delay(70760);
  digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);digitalWrite(11, HIGH);  
  delay(10);              
  digitalWrite(11, LOW);  
  delay(100);
  
  //1 minute cloud clearing//
  {
  for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=1)
  {analogWrite(ledPin11, fadeValue);
  delay(235);}}
  
  //30 min sunset//
  for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=1)
  {analogWrite(ledPin11, fadeValue);
  delay(7059);}
  delay(500000);

}

Well, to start with, I would do

define ONESEC (1000)

define ONEMIN (60l*ONESEC)

define ONEHOUR (60l*ONESEC)

And make the long delays look like delay(12*ONEHOUR)

Much clearer.