Delay() not working

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
johnny_boy_1984
Posts: 4
Joined: 08 Feb 2019 18:33

Delay() not working

Post by johnny_boy_1984 » 11 Feb 2019 12:44

Hello,

I am trying for my first project to digitalWrite several pins high or low with a delay, but the delays are ignored. I used the control relay example and added extra output pins. The result is all three defined pins are going on and off simultaniously instead of with a delay.

Code: Select all

/*
 * This example shows basic Switch Binary functionality
 * Control an external relay on pin 13
 */

// pin number, where relay is connected
#define RELAY_PIN9     9
#define RELAY_PIN10    10
#define RELAY_PIN11    11

// variable to store current relay state
byte lastSetValue;

// next macro sets up the Z-Uno channels
// in this example we set up 1 switch binary channel
// you can read more on http://z-uno.z-wave.me/Reference/ZUNO_SWITCH_BINARY/
ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getter, setter));

void setup() {
  pinMode(RELAY_PIN9, OUTPUT); // set up relay pin as output
  pinMode(RELAY_PIN10, OUTPUT); // set up relay pin as output
  pinMode(RELAY_PIN11, OUTPUT); // set up relay pin as output
  
}

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


// function, which returns the previously saved relay value
// this function runs only once the controller asks
byte getter() {
    return lastSetValue;
}


// function, which sets new relay state
// this function runs only once the controller sends new value
void setter(byte newValue) {
  // newValue is a variable, holding a "value"
  // which came from the controller or other Z-Wave device
  if (newValue > 0) { // if greater then zero
    digitalWrite(RELAY_PIN9, HIGH); //turn relay on
    delay(1000);
    digitalWrite(RELAY_PIN10, HIGH); //turn relay on
    delay(1000);
    digitalWrite(RELAY_PIN11, HIGH); //turn relay on
  } else {            // if equals zero
    digitalWrite(RELAY_PIN9, LOW); //turn relay off
    delay(1000);
    digitalWrite(RELAY_PIN10, LOW); //turn relay off
    delay(1000);
    digitalWrite(RELAY_PIN11, LOW); //turn relay off
  }

  // save the new value in a variable
  lastSetValue = newValue;
}

User avatar
PoltoS
Posts: 4571
Joined: 26 Jan 2011 19:36

Re: Delay() not working

Post by PoltoS » 14 Feb 2019 23:17

delay is not allowed in getters and setters. Please check the manual.

This is because setter and getter is like an interrupt

johnny_boy_1984
Posts: 4
Joined: 08 Feb 2019 18:33

Re: Delay() not working

Post by johnny_boy_1984 » 07 Mar 2019 20:04

Is it possible to create a delay on another way? I tried a lot of possibilities, but i am kinda stuck here.
What i would like to do is opening and closing ports one after another in a sequence.

User avatar
PoltoS
Posts: 4571
Joined: 26 Jan 2011 19:36

Re: Delay() not working

Post by PoltoS » 08 Mar 2019 00:32

We suggest to do minimal things in the getter/setter. Like saving the state to variable. The rest should be done in the loop

johnny_boy_1984
Posts: 4
Joined: 08 Feb 2019 18:33

Re: Delay() not working

Post by johnny_boy_1984 » 08 Mar 2019 18:50

I get the hint, i created an integer that changes when the state of the setter is changed, the rest happens in the loop. It is working now:

Code: Select all

// pin number, where relay is connected
#define RELAY_PIN9      9
#define RELAY_PIN10     10
#define RELAY_PIN11     11

// variable to store current relay state
byte lastSetValue;
int state;    // integer to store the switch value

// next macro sets up the Z-Uno channels
// in this example we set up 1 switch binary channel
// you can read more on http://z-uno.z-wave.me/Reference/ZUNO_SWITCH_BINARY/
ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getter, setter));

void setup() {
  pinMode(RELAY_PIN9, OUTPUT); // set up relay pin as output
  pinMode(RELAY_PIN10, OUTPUT); // set up relay pin as output
  pinMode(RELAY_PIN11, OUTPUT); // set up relay pin as output

}

void loop() {
  if (state == 1) { // if state equals 1
    digitalWrite(RELAY_PIN10, HIGH); //turn relay on
    delay (1000);
    digitalWrite(RELAY_PIN11, HIGH); //turn relay off
          } else {            // if state equal snot 1
    digitalWrite(RELAY_PIN10, LOW); //turn relay off
    delay (1000);
    digitalWrite(RELAY_PIN11, LOW); //turn relay off
     }
  // loop used for delayed ports
}


// function, which returns the previously saved relay value
// this function runs only once the controller asks
byte getter() {
    return lastSetValue;
}


// function, which sets new relay state
// this function runs only once the controller sends new value
void setter(byte newValue) {
  // newValue is a variable, holding a "value"
  // which came from the controller or other Z-Wave device
  if (newValue > 0) { // if greater then zero
    digitalWrite(RELAY_PIN9, HIGH); //turn relay on
    state = 1;
      } else {            // if equals zero
    digitalWrite(RELAY_PIN9, LOW); //turn relay off
    state = 0;
     }

  // save the new value in a variable
  lastSetValue = newValue;
}

Post Reply