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)