delay() issues with digitalWrite
Posted: 23 Oct 2019 21:39
Hi,
I've got a simple function to turn on the LEDs in sequence (eventually relays, but testing with LEDs for now), with a 1 second gap, i.e. delay(1000). I've noticed that using this with digitalWrite (and possibly other functions - not tested though) that is causes an issue.
Instead of turning all the LEDs on in sequence with a 1 second gap, they flash on very quickly, and then go off. Is this a bug? I've looked into using millis() but not sure if this is the best approach.
WORKS:
void setter5(byte newValue5) {
// newValue is a variable, holding a "value"
// which came from the controller or other Z-Wave device
if (newValue5 > 0) { // if greater then zero
digitalWrite(relayPin1, HIGH); //turn relay on
digitalWrite(relayPin2, HIGH); //turn relay on
digitalWrite(relayPin3, HIGH); //turn relay on
digitalWrite(relayPin4, HIGH); //turn relay on
}
else { // if equals zero
digitalWrite(relayPin1, LOW); //turn relay off
digitalWrite(relayPin2, LOW); //turn relay off
digitalWrite(relayPin3, LOW); //turn relay off
digitalWrite(relayPin4, LOW); //turn relay off
}
// save the new value in a variable
lastSetValue5 = newValue5;
}
DOESN'T WORK:
void setter5(byte newValue5) {
// newValue is a variable, holding a "value"
// which came from the controller or other Z-Wave device
if (newValue5 > 0) { // if greater then zero
digitalWrite(relayPin1, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin2, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin3, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin4, HIGH); //turn relay on
delay(1000);
}
else { // if equals zero
digitalWrite(relayPin1, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin2, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin3, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin4, LOW); //turn relay off
delay(1000);
}
// save the new value in a variable
lastSetValue5 = newValue5;
}
I've got a simple function to turn on the LEDs in sequence (eventually relays, but testing with LEDs for now), with a 1 second gap, i.e. delay(1000). I've noticed that using this with digitalWrite (and possibly other functions - not tested though) that is causes an issue.
Instead of turning all the LEDs on in sequence with a 1 second gap, they flash on very quickly, and then go off. Is this a bug? I've looked into using millis() but not sure if this is the best approach.
WORKS:
void setter5(byte newValue5) {
// newValue is a variable, holding a "value"
// which came from the controller or other Z-Wave device
if (newValue5 > 0) { // if greater then zero
digitalWrite(relayPin1, HIGH); //turn relay on
digitalWrite(relayPin2, HIGH); //turn relay on
digitalWrite(relayPin3, HIGH); //turn relay on
digitalWrite(relayPin4, HIGH); //turn relay on
}
else { // if equals zero
digitalWrite(relayPin1, LOW); //turn relay off
digitalWrite(relayPin2, LOW); //turn relay off
digitalWrite(relayPin3, LOW); //turn relay off
digitalWrite(relayPin4, LOW); //turn relay off
}
// save the new value in a variable
lastSetValue5 = newValue5;
}
DOESN'T WORK:
void setter5(byte newValue5) {
// newValue is a variable, holding a "value"
// which came from the controller or other Z-Wave device
if (newValue5 > 0) { // if greater then zero
digitalWrite(relayPin1, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin2, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin3, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin4, HIGH); //turn relay on
delay(1000);
}
else { // if equals zero
digitalWrite(relayPin1, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin2, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin3, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin4, LOW); //turn relay off
delay(1000);
}
// save the new value in a variable
lastSetValue5 = newValue5;
}