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: 28
Joined: 02 Mar 2016 00:52

4 Ch. Relay with Switches Some help required with sketch

Post by Black Cat » 12 Jun 2017 04:52

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 193 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: 3416
Joined: 26 Jan 2011 19:36

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

Post by PoltoS » 12 Jun 2017 12:23

You are defining BTN_PIN 4 times! Use different names like BTN1_PIN, BTN2_PIN, ...

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

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

Post by Black Cat » 13 Jun 2017 03:22

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 176 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: 3416
Joined: 26 Jan 2011 19:36

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

Post by PoltoS » 13 Jun 2017 03:32

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: 28
Joined: 02 Mar 2016 00:52

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

Post by Black Cat » 13 Jun 2017 03:43

In the sketch the /b is not present. I have edited it out above.

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

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

Post by Black Cat » 13 Jun 2017 03:45

Association Control3.jpg
Association Control3.jpg (49.73 KiB) Viewed 173 times

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

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

Post by PoltoS » 13 Jun 2017 03:54

Looks it complains on RelayPIN1. Please paste the full current code.

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

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

Post by Black Cat » 13 Jun 2017 05:01

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: 28
Joined: 02 Mar 2016 00:52

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

Post by Black Cat » 13 Jun 2017 05:18

Found it. I used mixed Uppercase and lower case.

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

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

Post by Black Cat » 13 Jun 2017 05:54

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

Who is online

Users browsing this forum: No registered users and 2 guests