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);
}
2 Likes

for me:

#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_1, ACTUATOR_PIN_1, value_1);
// 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 %”, VIRTUAL_CHANNEL_2, ACTUATOR_PIN_2, value_2);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_2, value_2);
}

@gcdodclan what is the issue?

Hi Sharamik, thanks for all help in forum

The code no run for me.

I changed the CAYENNE LOG (VIRTUAL_CHANNEL_1, ACTUATOR_PIN_1, value_1), , and run for me.

Thanks for all

these is just output info in the serial monitor.

Ok Thanks