Another Noob Cant Figure Out millis() delays

I apologize in advance. I worked through blink without delay, the doing many things at same time post, and the sticky on the subject at top of this board but I cant seem to figure it out.

I have 6 LEDs I am controlling with portB, at any given time 2 should be on in a certain sequence with time delays. I will be adding a button at a later time to cause two specific lights to turn on for 20 seconds, so I need to do this without delay(). I have not written any code for button yet because I wanted to understand how to time things using millis() first.

everything works fine until after the port output is set to B001100, then it locks up. Ive tried this a bunch of different ways... using if then, while, do while, but the result is the same or worse. Ive commented in code where things seem to go wrong.

I appreciate your time and help, thank you!!!

const int delay1 = 25000, delay2 = 30000, delay3 = 50000, delay4 = 54000;
unsigned long currentMillis = 0, previousMillis = 0, timer = 0;
void setup() 
{
DDRB = B111111;
}

void loop() {
currentMillis = millis();
timer = currentMillis - previousMillis;

if (timer <= delay1) //turn set of lights on for 25 seconds
{PORTB = B100001;}

if ((timer > delay1) && (timer <= delay2)) //turn another set of lights on for 5 seconds
{PORTB = B100010;}

if ((timer > delay2) && (timer <= delay3)) //turn another set of lights on for 20 seconds
{PORTB = B001100;} //last output that seems to work, timing is perfect but after this output the next if statement wont execute

if (timer > delay3) //turn another set of lights on, should stay on for 4 seconds after that next if statement causes timer to reset to 0
{PORTB = B010100;}

if (timer > delay4)
{previousMillis += currentMillis;} //reset "timer"
}

Sketch.ino (919 Bytes)

try

{previousMillis = millis();} //reset "timer"
const int delay1 = 25000, delay2 = 30000, delay3 = 50000, delay4 = 54000;

How large of a number is an int?

What will your code do, when the overflowed value rolls over to a negative number?

Type the delays as unsigned int.

Or better, use unsigned long, that is what millis() and micros() return.

Ah! I see. Int is ~ -32,000 to 32,000 right? so I choose the wrong data type for my delay constants.

I made the changes you recommended and its working as intended!

Thank you all for your help! I will pay more attention to my data types in the future.

Yes, -32767 to 32768.
Or -32786 to 32767, I forget which.

There's one more negative number than positives. Because there's no negative zero, that bit configuration can be used for another number and it just happens to be negative.