Two light switches

Here is code, i know this must be very basic but I am quite new to this and I have spent quite a lot of time and cannot work out how it works. CHANNEL 1 will work, but I can get channel 2 to work, if i call them both channel 1 with different pins it will only switch one, i would really appreciate to know what I am doing wrong.

Ben

#define VIRTUAL_CHANNEL 1
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL 2
#define ACTUATOR_PIN 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN, value);
}

i have made changes to your code. in place of just copy and paste try to understand the code and find out where was the mistake.

#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 5 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
Serial.begin(9600);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_2, OUTPUT);
Cayenne.begin(username, password, clientID);
}

void loop()
{
Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)
{
int value_1 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_1, value_1);
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
int value_2 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_2, value_2);
}
1 Like