Code runs into infinite loop I cannot seem to find

The code below is used to turn on my relay (“outpin”=7). What is suppose to happen is the relay should turn on after 5 seconds, then stay on for 3.666 seconds and turn off for 45 seconds (ignore the nested if statement) then turn on for 3.666 seconds and off for 45 etc… What is currently happening is the relay will turn on for 3.666 seconds as expected then turn off and 45 seconds later it will turn on again, but STAY on. It never turns off. The weird thing is that if I change the 45 seconds to something like 5 seconds, it works fine! I am new to this so go ez on me :confused:

int analogPin= A0;
int raw= 0;
int inpin=7;
int Vin= 3.3;
int dis=600;
int Max=5600;
int inch=(Max-dis)/12;
int timer=0;
float Vout= 0;
float R1= 970;
float R2= 0;
float buffer= 0;

void setup() {
Serial.begin(9600); // setup serial
pinMode(inpin, OUTPUT);
delay(1000*5);
}

void loop() {

digitalWrite(inpin, HIGH);
timer=millis();
while(millis()-timer<3666){
}

digitalWrite(inpin, LOW);
timer=millis();
while(millis()-timer<45000){
raw = analogRead(analogPin);
Serial.print("Vout: ");
Serial.println(raw);
Serial.print("millis(): ");
Serial.println(millis()-timer);
Serial.print("timer(): ");
Serial.println(timer);
if(raw>1000){
digitalWrite(inpin, HIGH);
delay(100040);
digitalWrite(inpin, LOW);
delay(1000
60*60);
}
}
}

Final.ino (847 Bytes)

Try

while(millis()-timer<45000UL){

Note the UL after the 45000.

Note also timer is declared as a 16-bit signed int. What is the largest number a 16-bit signed in can hold? What kind of number does millis() return?

Regards, Ray L.

@RayLivingston

Missed that one :( Good catch.