Relay button state problem

Hello,

Well is my first topic on Cayenne Community, nice to meet you.

I want to use Cayenne for my hobby greenhouse, but i have some issues with my state on relays.

I use Arduino Mega 2560.

My code:

#define VIRTUAL_PIN4 V4
#define RELAY_DIGITAL_PIN1 V4
#define VIRTUAL_PIN5 V5
#define RELAY_DIGITAL_PIN2 V5

pinMode(RELAY_DIGITAL_PIN1, OUTPUT);
digitalWrite(RELAY_DIGITAL_PIN1, LOW);
pinMode(RELAY_DIGITAL_PIN2, OUTPUT);
digitalWrite(RELAY_DIGITAL_PIN2, LOW);


CAYENNE_IN(V4)
{
  int currentValue = getValue.asInt(); 
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN1, LOW);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN1, HIGH);
  }
}

CAYENNE_IN(V5)
{
  int currentValue = getValue.asInt(); 
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN2, LOW);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  }
}

I only put relay codes, if need im gonna put all.

My problem: When i powerup arduino relay start as ON and my button on dashboard is on OFF (gray not pushed). Relay get off when i put on state ON ( colored ). Its everything ok with my code?

you can set the relay state to OFF in void setup() .
if you want to state to be same as before the restart of arduino then you might want to save the state in EEPROM and then read and write the state to the relay.

1 Like