Program stops working

Hi

I'm building a traffic light for my class, so when we have a lesson the light is red and it changes to yellow, then green when we have recess.

But it stops working at 12.40... It changes to yellow but then nothing happens.

If you have any idea why it isnt working, please help :smiley:

Regards Rumrobot

int cykel = 1;
int rod = 2;
int gul = 3;
int gron = 4;

long tid = 45580; //10:10

void setup() 
{
  pinMode(cykel, OUTPUT);
  pinMode(rod, OUTPUT);
  pinMode(gul, OUTPUT);
  pinMode(gron, OUTPUT);
  Serial.begin(9600);
}
void loop() 
{
  if (tid >= 0)
  {
        if (tid <= 30597 && tid >= 27900)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, LOW);
      digitalWrite(gron, HIGH);
      Serial.println("GRØN"); //Green
    }
    if (tid <= 30600 && tid >= 30597)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL"); //Yellow
    }

    if (tid <= 35997 && tid >= 30600)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, LOW);
      digitalWrite(gron, LOW);
      Serial.println("RØD"); //Red
    }
    
    // 10:00 - 10:30
    if (tid >= 35997 && tid <= 36000)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL"); //Yellow
    }
    if (tid >= 36000 && tid <= 37797) 
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, LOW);
      digitalWrite(gron, HIGH);    
      Serial.println("GRØN");   //green
    }
    if (tid >= 37797 && tid <= 37800)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL"); // yellow
    }

    if (tid >= 37800 && tid <= 43197)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, LOW);
      digitalWrite(gron, LOW);
      Serial.println("RØD"); //red
    }
    
    // 12:00 - 12:40
    if (tid >= 43197 && tid <= 43200)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL"); //yellow
    }
    if (tid >= 43200 && tid <= 45597) 
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, LOW);
      digitalWrite(gron, HIGH);
      Serial.println("GRØN"); //green
    if (tid >= 45597 && tid <= 45600)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL"); //yellow
    }  

    if (tid >= 45600 && tid <= 48297) // tid = time
    {
      digitalWrite(rod, HIGH); //red
      digitalWrite(gul, LOW); //yellow
      digitalWrite(gron, LOW); //green
      Serial.println("RØD"); //Red
    }

    // 13:25 - 13:30
    if (tid >= 48297 && tid <= 48300)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL"); // yellow
    }
    if (tid >= 48300 && tid <= 48597) 
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, LOW);
      digitalWrite(gron, HIGH);      
      Serial.println("GRØN");
    }
    if (tid >= 48597 && tid <= 48600)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL");
    }

    if (tid <= 51297 && tid >= 48600)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, LOW);
      digitalWrite(gron, LOW);
      Serial.println("RØD");
    }

        // 14:15 - 14.20
    if (tid >= 51297 && tid <= 51300)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL");
    }
    if (tid >= 51300 && tid <= 51597) 
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, LOW);
      digitalWrite(gron, HIGH);      
      Serial.println("GRØN");
    }
    if (tid >= 51597 && tid <= 51600)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL");
    }

    if (tid <= 57297 && tid >= 51600)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, LOW);
      digitalWrite(gron, LOW);
      Serial.println("RØD");
    }

    if (tid <= 57300 && tid >= 57297)
    {
      digitalWrite(rod, HIGH);
      digitalWrite(gul, HIGH);
      digitalWrite(gron, LOW);
      Serial.println("GUL");
    }
    if (tid <= 52800 && tid >= 51600)
    {
      digitalWrite(rod, LOW);
      digitalWrite(gul, LOW);
      digitalWrite(gron, HIGH);
      Serial.println("GRØN");
    }
    }
  } 
  
  if (tid >= 52800)
  {
    tid = 27900;
    delay(61500000);
  }

  Serial.println(tid);

  digitalWrite(cykel, HIGH);
  tid ++;
  delay(1000);
}

Your image

Tid should be an unsigned long so won’t turn negative

What makes 45580 represent 10:10? Are you resetting the system every day?

An RTC would be more appropriate to keep time. Millis won’t be precise over a long time

The 10:10 mark, is old :smiley: Forgot to delete it. I also tried with a unsigned long, it didnt help. And i dont really have a rtc module laying around, and thought this was easier than buying one... Maybe it wasnt.

So what’s tid? the number of seconds since midnight?
Millis won’t be stable enough on most arduino to keep track of time with just a delay of 1s to drive it’s update

Note that Your tests are overlapping at boundaries since you are using >= and <=

What’s delay(61500000);

If the clock was 10 then tid would be 106060. Then i plug the arduino in at 10, when the variable is at 106060, and then it will run from there.
The big delay is for it turns off at 16 and waits until the time is 27900 or 7.45. Then it turns on again

Rumrobot:
If the clock was 10 then tid would be 106060.

Ok so number of seconds since midnight so this test if (tid <= 30600 && tid >= 30597)is if Tim is between 8:29:57 and 8:30:00 but i fail to see why

  // 10:00 - 10:30
    if (tid >= 35997 && tid <= 36000)

would be between 10:00 - 10:30
36000 Is indeed 10:00:00 but 35997 is 9:59:57

I would advise to validate all your magic numbers. (Or better make a macros computing the value) and possibly use else statement in between all the if

It won’t make the arduino more precise though...

    delay(61500000);

I make that just over 17 hours, is that what you intended?

Yes it is right. The 17 hours or so. Anyways i bought a rtx module, i realised that would be easier. Now is just the waiting...