adown:
Hi everyone, I am trying to use a blink without delay timer to turn on an output for 8 seconds, then turn it off. I can't get it to work. Am I even close. Thanks adown
unsigned long interval = 8000; // Time we need to wait
unsigned long previousMillis = 0; //millis() returns an unsigned long
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
if ((unsigned long)(millis() - previousMillis) <= interval) {
digitalWrite(13, HIGH);
}
else {
digitalWrite(13, LOW);
previousMillis = millis();
}
}
It's really not so bad. But switching HIGH and LOW both belong inside of the if millis-etc time check.
Make it easy to know if the led should be HIGH or LOW with a variable.
One way to do it:
byte ledState; // if the led pin is HIGH or LOW, this is the same
// then when the code switches the led HIGH or LOW, the next step is to change ledState.
// whenever the interval is up,
if ( ledState == LOW )
{
// set ledState HIGH
}
else
{
// set ledState LOW
}
// set the led pin to whatever is in ledState
// tip about the above:
// I keep my braces all on the same level, it makes them easier to find and to line up the indent levels
// when my eyes don't have to look all over the place for lines ending in { to line up the matching }.
// When you code 8+ hours a day for a while, it does add up even when you're still young.
Do you have the missing puzzle pieces now?