I am trying to program a timed loop. I can't figure out why this isn't working. The only thing that happens is pin 13 flashes briefly for no reason, then nothing. Why doesn't the loop execute between 4 and 11 seconds after starting the program?
My code:
//Blink in staggered loop between 4 and 11 second into execution of program
unsigned long ttt = millis();
void setup() {
pinMode(11, OUTPUT); // sets the digital pin 11 as output
pinMode(12, OUTPUT); // sets the digital pin 12 as output
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
while (4000 < ttt < 11000);
{
digitalWrite(11, HIGH); // sets the digital pin 11 on
delay(100); // waits for a second
digitalWrite(11, LOW); // sets the digital pin 11 off
delay(100); // waits for a second
digitalWrite(12, HIGH); // sets the digital pin 12 on
delay(100); // waits for a second
digitalWrite(12, LOW); // sets the digital pin 12 off
delay(100); // waits for a second
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(100); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(100); // waits for a second
}
}
This is not the way to check if a variable has a value between two values. You will have to do two compares; e.g.
while (4000 < ttt && ttt < 11000);
// && is a logical AND; you can also use the keyword 'AND'
The semicolon ends the while-loop so whatever is after that will be executed unconditionally (the same affect as if you did not have the while condition).
Often beginners assume that this means that ttt will follow millis(); not sure if that is the case for you but you will have to refresh ttt ever so often so it reflects the actual millis().
You should not call functions that rely on hardware in the global declaration section of the program; millis() might not work as expected and if ttt gets a value between 4000 and 11000, you will enter the while-loop but you will never get out of it because you don't update ttt.
If you don't assign a value, you're program will kind-of do what you expect.
//unsigned long ttt = millis();
unsigned long ttt;
That might very well be the bootloader kicking in after a reset of the board; though I would expect two or three flashes.
To fix your problem, assign millis() to ttt in the beginning of loop() and replace the while loop by an if statement The below code will flash your leds in the period from 4 seconds after reset till 11 seconds after reset.
void loop()
{
// get the current time
ttt = millis();
if (4000 < ttt && ttt < 11000)
{
digitalWrite(11, HIGH); // sets the digital pin 11 on
delay(100); // waits for a second
digitalWrite(11, LOW); // sets the digital pin 11 off
delay(100); // waits for a second
digitalWrite(12, HIGH); // sets the digital pin 12 on
delay(100); // waits for a second
digitalWrite(12, LOW); // sets the digital pin 12 off
delay(100); // waits for a second
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(100); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(100); // waits for a second
}
}
You can ignore point (4) when using above code.
One last comment: mixing millis() and delay() is often not going to give the desired results.
If you have problems with modified code, post the full code. I only gave you loop(), I left the rest to you; so you need setup() and the variable declarations.
Hello
I think your sketch needs a time hander, based on the BlinkWithOutWait IDE exampel, to manage two so called 'virtual' switches.
One switch to handle the time frame from 4000 to 11000 and the second switch to blink the led.