4 Ch. Relay with Switches Some help required with sketch

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

I've used the 4 Channel Relay example and want to add mechanical switching. NOt sure if I have used the correct method but I tried the Association Control example using Pin 18 to 21 for switching.
The sketch will not compile and returns an error
Association Control.jpg
Association Control.jpg (39.43 KiB) Viewed 9165 times
I'm attempting not to use Pins 3 to 6 and assume that Digital Pins 18 to 22 are the correct definitions.
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by PoltoS »

You are defining BTN_PIN 4 times! Use different names like BTN1_PIN, BTN2_PIN, ...
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

Thanks, that part is now clear.

Next problem is definition again, this time in the loop.

Code: Select all

void loop() {
  byte currentValue = !digitalRead(BTN_PIN1);
  if (currentValue != lastValue) {  // if state changes
    lastValue = currentValue;      // save new state

    if (lastValue) {
      zunoSendToGroupSetValueCommand(CTRL_GROUP_1, ZUNO_SWITCHED_ON);   // if button pressed - send switch ON command
      digitalWrite(RelayPIN1,HIGH);    //  turn the Relay on
    } else {
      zunoSendToGroupSetValueCommand(CTRL_GROUP_1, ZUNO_SWITCHED_OFF);  // if button released - send switch OFF command
      [b]digitalWrite(RelayPIN1,LOW);     // turn the Relay off
    }
  }
}

digitalWrite(RelayPIN1,HIGH) is defined, yet digitalWrite(RelayPIN1,LOW); is undeclared identifier

I also assume that this part needs to be coded for all 4 BTN_PIN (ie BTN_PIN 1 to 4)?
Association Control2.jpg
Association Control2.jpg (29.82 KiB) Viewed 9148 times
I'm slowly getting there and thank you for your help.

edit /b removed from code.
Last edited by Black Cat on 13 Jun 2017 03:43, edited 1 time in total.
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by PoltoS »

I don't see abt problem on the picture, but in the code above the picture I can notice a
LOW[/b]
Should be just
LOW
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

In the sketch the /b is not present. I have edited it out above.
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

Association Control3.jpg
Association Control3.jpg (49.73 KiB) Viewed 9145 times
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by PoltoS »

Looks it complains on RelayPIN1. Please paste the full current code.
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

Code: Select all

/* 
 * 
 * 4 Relays controlled thouth resistors 220Omh and optocouplers 817С 4 On/Off Switches
 * Off - HIGH
 * On - LOW
 */

// Pins definitions
#define RelayPin1 9
#define RelayPin2 10
#define RelayPin3 11
#define RelayPin4 12
#define BTN_PIN1 19       // switch pin19
#define BTN_PIN2 20       // switch pin20
#define BTN_PIN3 21       // switch pin21
#define BTN_PIN4 22       // switch pin22



byte lastValue;
byte buttonState; // Variables we use in this sketch




// Global variables to store data reported via getters
byte switchValue1 = 1;
byte switchValue2 = 1;
byte switchValue3 = 1;
byte switchValue4 = 1;

ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_ALWAYS_AWAKE);
ZUNO_SETUP_ASSOCIATIONS(ZUNO_ASSOCIATION_GROUP_SET_VALUE); // this macro defines association groups in Z-Uno


// Set up 10 channels
ZUNO_SETUP_CHANNELS(
  ZUNO_SWITCH_BINARY(getterSwitch1, setterSwitch1),
  ZUNO_SWITCH_BINARY(getterSwitch2, setterSwitch2),
  ZUNO_SWITCH_BINARY(getterSwitch3, setterSwitch3),
  ZUNO_SWITCH_BINARY(getterSwitch4, setterSwitch4)
);

void setup() {
  // set up I/O pins. Analog and PWM will be automatically set up on analogRead/analogWrite functions call
  pinMode(RelayPin1, OUTPUT);
  pinMode(RelayPin2, OUTPUT);
  pinMode(RelayPin3, OUTPUT);
  pinMode(RelayPin4, OUTPUT);
  pinMode(BTN_PIN1, INPUT_PULLUP); // setup pin as input with pullup
  pinMode(BTN_PIN2, INPUT_PULLUP); // setup pin as input with pullup
  pinMode(BTN_PIN3, INPUT_PULLUP); // setup pin as input with pullup
  pinMode(BTN_PIN4, INPUT_PULLUP); // setup pin as input with pullup
}

void loop() {
  byte currentValue = !digitalRead(BTN_PIN1);
  if (currentValue != lastValue) {  // if state changes
    lastValue = currentValue;      // save new state

    if (lastValue) {
      zunoSendToGroupSetValueCommand(CTRL_GROUP_1, ZUNO_SWITCHED_ON);   // if button pressed - send switch ON command
      digitalWrite(RelayPIN1,HIGH);    //  turn the Relay on
    } else {
      zunoSendToGroupSetValueCommand(CTRL_GROUP_1, ZUNO_SWITCHED_OFF);  // if button released - send switch OFF command
      digitalWrite(RelayPIN1,LOW);     // turn the Relay off
    }
  }
}

// Getters and setters

void setterSwitch1(byte value) {
  digitalWrite(RelayPin1, (value > 0) ? LOW : HIGH);
  switchValue1 = value;
}

byte getterSwitch1(){
  return switchValue1;
}

 void setterSwitch2(byte value) {
  digitalWrite(RelayPin2, (value > 0) ? LOW : HIGH);
  switchValue2 = value;
}

byte getterSwitch2(){
  return switchValue2;
}

void setterSwitch3(byte value) {
  digitalWrite(RelayPin3, (value > 0) ? LOW : HIGH);
  switchValue3 = value;
}

byte getterSwitch3(){
  return switchValue3;
}

void setterSwitch4(byte value) {
  digitalWrite(RelayPin4, (value > 0) ? LOW : HIGH);
  switchValue4 = value;
}

byte getterSwitch4(){
  return switchValue4;
}

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

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

Found it. I used mixed Uppercase and lower case.
Black Cat
Posts: 62
Joined: 02 Mar 2016 00:52

Re: 4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat »

Got it half working.

Relays will set state from UI, there is a problem with the loop as it doesn't detect the button when it is pushed.

The only other change was that I put a pullup resistor in the circuit for the buttons.
Post Reply