Sketch not reporting correct state of Buttons

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
MattyO
Posts: 4
Joined: 11 May 2018 09:50

Sketch not reporting correct state of Buttons

Post by MattyO »

I am trying to use this sketch to run some relays. it is working fine. BUT when I turn on in fibaro it turns the relay on but then the status of the switch shows off but the relay remains on. If I hit the off button it turns the relay off.


//Black Cat Control Systems
//Mike Novak July 2017
//4 channel relay with switches
// LED or Relay pin number

#define LED_PIN1 9 // 9 pin - user LED of Z-Uno board
#define LED_PIN2 10 // 10 pin - user LED of Z-Uno board
#define LED_PIN3 11 // 11 pin - user LED of Z-Uno board
#define LED_PIN4 12 // 12 pin - user LED of Z-Uno board
#define LED_PIN5 13 // 13 pin - user LED of Z-Uno board

byte currentLEDValue1; // Last saved LED1 value
byte currentLEDValue2; // Last saved LED2 value
byte currentLEDValue3; // Last saved LED3 value
byte currentLEDValue4; // Last saved LED4 value
byte currentLEDValue5; // Last saved LED5 value


byte lastButtonState1; // variable to store current button1 state
byte lastButtonState2; // variable to store current button2 state
byte lastButtonState3; // variable to store current button3 state
byte lastButtonState4; // variable to store current button4 state
byte lastButtonState5; // variable to store current button5 state

// button pin number

#define BTN_PIN1 8 // 8 pin - button(BTN1) of Z-Uno board
#define BTN_PIN2 7 // 7 pin - button(BTN2) of Z-Uno board
#define BTN_PIN3 6 // 6 pin - button(BTN2) of Z-Uno board
#define BTN_PIN4 5 // 5 pin - button(BTN2) of Z-Uno board
#define BTN_PIN5 4 // 4 pin - button(BTN2) of Z-Uno board

#define SWITCH_ON 0xff
#define SWITCH_OFF 0


// number of Association Group
#define CONTROL_GROUP 1


// Send Turn On/Off command to associated devices
ZUNO_SETUP_ASSOCIATIONS(ZUNO_ASSOCIATION_GROUP_SET_VALUE);

// channel number
#define ZUNO_CHANNEL_NUMBER_ONE 1
#define ZUNO_CHANNEL_NUMBER_TWO 2
#define ZUNO_CHANNEL_NUMBER_THREE 3
#define ZUNO_CHANNEL_NUMBER_FOUR 4
#define ZUNO_CHANNEL_NUMBER_FIVE 5

ZUNO_SETUP_CHANNELS(
ZUNO_SWITCH_BINARY(getterSwitch1, setterSwitch1),
ZUNO_SWITCH_BINARY(getterSwitch2, setterSwitch2),
ZUNO_SWITCH_BINARY(getterSwitch3, setterSwitch3),
ZUNO_SWITCH_BINARY(getterSwitch4, setterSwitch4),
ZUNO_SWITCH_BINARY(getterSwitch5, setterSwitch5)
);
// the setup routine runs once when you press reset:
void setup() {
pinMode(LED_PIN1, OUTPUT); // setup pin as output
pinMode(BTN_PIN1, INPUT_PULLUP); // set button pin as input
pinMode(LED_PIN2, OUTPUT); // setup pin as output
pinMode(BTN_PIN2, INPUT_PULLUP); // set button pin as input
pinMode(LED_PIN3, OUTPUT); // setup pin as output
pinMode(BTN_PIN3, INPUT_PULLUP); // set button pin as input
pinMode(LED_PIN4, OUTPUT); // setup pin as output
pinMode(BTN_PIN4, INPUT_PULLUP); // set button pin as input
pinMode(LED_PIN5, OUTPUT); // setup pin as output
pinMode(BTN_PIN5, INPUT_PULLUP); // set button pin as input
}


// the loop routine runs over and over again forever:
void loop() {
byte currentButtonState1 = digitalRead(BTN_PIN1);
byte currentButtonState2 = digitalRead(BTN_PIN2);
byte currentButtonState3 = digitalRead(BTN_PIN3);
byte currentButtonState4 = digitalRead(BTN_PIN4);
byte currentButtonState5 = digitalRead(BTN_PIN5);


if (currentButtonState1 != lastButtonState1) { // if state changes
lastButtonState1 = currentButtonState1; // save new state
zunoSendReport(ZUNO_CHANNEL_NUMBER_ONE); // send report over the Z-Wave to the controller
if (currentButtonState1 == LOW) { // if button is pressed
digitalWrite(LED_PIN1, HIGH); // turn the LED on
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF);
} else { // if button is released
digitalWrite(LED_PIN1, LOW); // turn the LED off
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_ON);
}
}

if (currentButtonState2 != lastButtonState2) { // if state changes
lastButtonState2 = currentButtonState2; // save new state
zunoSendReport(ZUNO_CHANNEL_NUMBER_TWO); // send report over the Z-Wave to the controller
if (currentButtonState2 == LOW) { // if button is pressed
digitalWrite(LED_PIN2, HIGH); // turn the LED on
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF);
} else { // if button is released
digitalWrite(LED_PIN2, LOW); // turn the LED off
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_ON);
}
}


if (currentButtonState3 != lastButtonState3) { // if state changes
lastButtonState3 = currentButtonState3; // save new state
zunoSendReport(ZUNO_CHANNEL_NUMBER_THREE); // send report over the Z-Wave to the controller
if (currentButtonState3 == LOW) { // if button is pressed
digitalWrite(LED_PIN3, HIGH); // turn the LED on
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF);
} else { // if button is released
digitalWrite(LED_PIN3, LOW); // turn the LED off
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_ON);
}
}


if (currentButtonState4 != lastButtonState4) { // if state changes
lastButtonState4 = currentButtonState4; // save new state
zunoSendReport(ZUNO_CHANNEL_NUMBER_FOUR); // send report over the Z-Wave to the controller
if (currentButtonState4 == LOW) { // if button is pressed
digitalWrite(LED_PIN4, HIGH); // turn the LED on
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF);
} else { // if button is released
digitalWrite(LED_PIN4, LOW); // turn the LED off
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_ON);
}
}


if (currentButtonState5 != lastButtonState5) // if state changes
{lastButtonState5 = currentButtonState5; // save new state
zunoSendReport(ZUNO_CHANNEL_NUMBER_FIVE); // send report over the Z-Wave to the controller
if (currentButtonState5 == LOW) { // if button is pressed
digitalWrite(LED_PIN5, HIGH); // turn the LED on
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF);
} else { // if button is released
digitalWrite(LED_PIN5, LOW); // turn the LED off
zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_ON);
}
}
}

byte getterSwitch1() {
if (lastButtonState1 == 0) { // if button is pressed
return 0xff; // return "Triggered" state to the controller
} else { // if button is released
return 0; // return "Idle" state to the controller
}
}

byte getterSwitch2() {
if (lastButtonState2 == 0) { // if button is pressed
return 0xff; // return "Triggered" state to the controller
} else { // if button is released
return 0; // return "Idle" state to the controller
}
}

byte getterSwitch3() {
if (lastButtonState3 == 0) { // if button is pressed
return 0xff; // return "Triggered" state to the controller
} else { // if button is released
return 0; // return "Idle" state to the controller
}
}

byte getterSwitch4() {
if (lastButtonState4 == 0) { // if button is pressed
return 0xff; // return "Triggered" state to the controller
} else { // if button is released
return 0; // return "Idle" state to the controller
}
}

byte getterSwitch5() {
if (lastButtonState5 == 0) { // if button is pressed
return 0xff; // return "Triggered" state to the controller
} else { // if button is released
return 0; // return "Idle" state to the controller
}
}

void setterSwitch1(byte value1) {
// value is a variable, holding a "new value1"
// which came from the controller or other Z-Wave device
if (value1 > 0) { // if greater then zero
digitalWrite (LED_PIN1, HIGH); //turn the LED on (HIGH is the voltage level)
} else { // if equals zero
digitalWrite(LED_PIN1, LOW); //turn the LED off by making the voltage LOW
}
// we'll save our value for the situation, when the controller will ask us about it
currentLEDValue1 = value1;
}

void setterSwitch2(byte value2) {
// value is a variable, holding a "new value2"
// which came from the controller or other Z-Wave device
if (value2 > 0) { // if greater then zero
digitalWrite (LED_PIN2, HIGH); //turn the LED on (HIGH is the voltage level)
} else { // if equals zero
digitalWrite(LED_PIN2, LOW); //turn the LED off by making the voltage LOW
}
// we'll save our value for the situation, when the controller will ask us about it
currentLEDValue2 = value2;
}

void setterSwitch3(byte value3) {
// value is a variable, holding a "new value3"
// which came from the controller or other Z-Wave device
if (value3 > 0) { // if greater then zero
digitalWrite (LED_PIN3, HIGH); //turn the LED on (HIGH is the voltage level)
} else { // if equals zero
digitalWrite(LED_PIN3, LOW); //turn the LED off by making the voltage LOW
}
// we'll save our value for the situation, when the controller will ask us about it
currentLEDValue3 = value3;
}

void setterSwitch4(byte value4) {
// value is a variable, holding a "new value4"
// which came from the controller or other Z-Wave device
if (value4 > 0) { // if greater then zero
digitalWrite (LED_PIN4, HIGH); //turn the LED on (HIGH is the voltage level)
} else { // if equals zero
digitalWrite(LED_PIN4, LOW); //turn the LED off by making the voltage LOW
}
// we'll save our value for the situation, when the controller will ask us about it
currentLEDValue4 = value4;
}

void setterSwitch5(byte value5) {
// value is a variable, holding a "new value5"
// which came from the controller or other Z-Wave device
if (value5 > 0) { // if greater then zero
digitalWrite (LED_PIN5, HIGH); //turn the LED on (HIGH is the voltage level)
} else { // if equals zero
digitalWrite(LED_PIN5, LOW); //turn the LED off by making the voltage LOW
}
// we'll save our value for the situation, when the controller will ask us about it
currentLEDValue5 = value5;

}
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

Re: Sketch not reporting correct state of Buttons

Post by Black Cat »

That was the first sketch that I adapted. It was based on the 4 channel relay example, I just added the extra "5" switch.
Works well in Vera & Homeseer, so that to me indicates a Fibario problem.
MattyO
Posts: 4
Joined: 11 May 2018 09:50

Re: Sketch not reporting correct state of Buttons

Post by MattyO »

Thanks Black Cat.
Riri
Posts: 25
Joined: 22 Jun 2018 10:08

Re: Sketch not reporting correct state of Buttons

Post by Riri »

Hi,
the getter must not return the state of the switch, but relay (invers of setter, both must be synchronous).
try with this :

Code: Select all

byte getterSwitch1() {
  return currentLEDValue1;
} 
and for all modify of switch, store value in currentLEDValue1

Code: Select all

if (currentButtonState1 != lastButtonState1) { // if state changes
  lastButtonState1 = currentButtonState1; // save new state
  if (currentButtonState1 == LOW) { // if button is pressed
    digitalWrite(LED_PIN1, HIGH); // turn the LED on
    zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_OFF);
    currentLEDValue1=HIGH; // Store HIGH for getter
  } else { // if button is released
    digitalWrite(LED_PIN1, LOW); // turn the LED off
    zunoSendToGroupSetValueCommand(CONTROL_GROUP, SWITCH_ON);
    currentLEDValue1=LOW; // Store LOW for getter
  }
  // Send report after modify value
  zunoSendReport(ZUNO_CHANNEL_NUMBER_ONE); // send report over the Z-Wave to the controller (by the getter)
}
Post Reply