delay() issues with digitalWrite

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
cmpts_cmp
Posts: 26
Joined: 26 Aug 2019 14:14

delay() issues with digitalWrite

Post by cmpts_cmp » 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;
}

michap
Posts: 341
Joined: 26 Mar 2013 10:35
Contact:

Re: delay() issues with digitalWrite

Post by michap » 23 Oct 2019 22:00

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

cmpts_cmp
Posts: 26
Joined: 26 Aug 2019 14:14

Re: delay() issues with digitalWrite

Post by cmpts_cmp » 23 Oct 2019 22:03

Thanks

Any examples for a way around this?

MastrUsr
Posts: 12
Joined: 13 Sep 2017 18:54

Re: delay() issues with digitalWrite

Post by MastrUsr » 24 Oct 2019 16:53

Just set a variable in the setter then in main() do

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

cmpts_cmp
Posts: 26
Joined: 26 Aug 2019 14:14

Re: delay() issues with digitalWrite

Post by cmpts_cmp » 24 Oct 2019 17:17

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;
}

Post Reply