Page 1 of 1

delay() issues with digitalWrite

Posted: 23 Oct 2019 21:39
by cmpts_cmp
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;
}

Re: delay() issues with digitalWrite

Posted: 23 Oct 2019 22:00
by michap
Hi,

please read https://z-uno.z-wave.me/reference/delay/
Due to strict radio timeouts, it is forbidden to call this function in the Z-Wave callback functions (getters and setters of Z-Wave channels). The Z-Wave.Me bootloader will ignore delay() in this situation.
Michael

Re: delay() issues with digitalWrite

Posted: 23 Oct 2019 22:03
by cmpts_cmp
Thanks

Any examples for a way around this?

Re: delay() issues with digitalWrite

Posted: 24 Oct 2019 16:53
by MastrUsr
Just set a variable in the setter then in main() do

if (Var1){
relay1
delay
relay2
.
.
.
Var1 = false;
}

Re: delay() issues with digitalWrite

Posted: 24 Oct 2019 17:17
by cmpts_cmp
Sorted. This worked. Thanks!

bool relaySequence1 = false;

void loop() {
// loop is empty, because all the control comes over the Z-Wave

if (relaySequence1 == 1) {
digitalWrite(relayPin1, HIGH); //turn relay on
delay(1000);
digitalWrite(relayPin2, HIGH); //turn relay on
delay(1000);
}
else {
digitalWrite(relayPin1, LOW); //turn relay off
delay(1000);
digitalWrite(relayPin2, LOW); //turn relay off
delay(1000);
}
}

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
relaySequence1 = 1;
} else { // if equals zero
relaySequence1 = 0;
}

// save the new value in a variable
lastSetValue5 = newValue5;
}