Independent Switch

Hi Shramik,

Need your expertise (once again).

I have created a project that opens a safety box that opens based on a correct pass code. The keypad is made up of button widgets. The servo serves as the lock. The code worked (thanks to your help before). But this time I want a fallback mechanism like a switch. The switch should be able to open the box in the absence of cayenne and internet connection. Here is my code.

#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernetW5500.h>
#include <Servo.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “----”;
char password = “”;
char clientID = “----”;

#define VIRTUAL_CHANNEL_LED 11
#define ACTUATOR_PIN_LED A0
#define VIRTUAL_CHANNEL_0 0
#define ACTUATOR_PIN_0 0
#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 1
#define VIRTUAL_CHANNEL_2 2
#define ACTUATOR_PIN_2 2
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 3
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 4
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 5
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 A2
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 7
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 8
#define VIRTUAL_CHANNEL_9 9
#define ACTUATOR_PIN_9 9
#define VIRTUAL_CHANNEL_12 12
#define ACTUATOR_PIN_12 A1
#define VIRTUAL_CHANNEL_13 13
#define SENSOR_PIN_13 A4
#define VIRTUAL_CHANNEL_14 14
#define SENSOR_PIN_14 A5

// Do not use digital pins 0 or 1 since those conflict with the use of Serial.
Servo Servo1;
int servoPin = 6;
void setup()
{

```
Serial.begin(9600);
Servo1.attach(servoPin); 
pinMode(ACTUATOR_PIN_LED, OUTPUT);
```

pinMode(ACTUATOR_PIN_0, OUTPUT);
pinMode(ACTUATOR_PIN_1, OUTPUT);
pinMode(ACTUATOR_PIN_2, OUTPUT);
pinMode(ACTUATOR_PIN_3, OUTPUT);
pinMode(ACTUATOR_PIN_4, OUTPUT);
pinMode(ACTUATOR_PIN_5, OUTPUT);
pinMode(ACTUATOR_PIN_6, OUTPUT);
pinMode(ACTUATOR_PIN_7, OUTPUT);
pinMode(ACTUATOR_PIN_8, OUTPUT);
pinMode(ACTUATOR_PIN_9, OUTPUT);
pinMode(ACTUATOR_PIN_12, OUTPUT);
pinMode(SENSOR_PIN_13, INPUT);
pinMode(SENSOR_PIN_14, INPUT);
Cayenne.begin(username, password, clientID);
}
int b0;
int b1;
int b2;
int b3;
int b4;
int b5;
int b6;
int b7;
int b8;
int b9;

int Switch =0;

void loop()
{
Cayenne.loop();
Switch = digitalRead(SENSOR_PIN_13);

if((b0 == 1) && (b1 == 1) && (b2 == 1) && (b3 == 1) && (b4 == 0) && (b5 == 0) && (b6 == 0) && (b7 == 0) && (b8 == 0) && (b9 == 0) || Switch == 1){

```
Serial.print("Correct!");
Servo1.write(180);
digitalWrite(ACTUATOR_PIN_LED, HIGH);
```

}else{
Serial.print(“Error!”);
Servo1.write(0);
digitalWrite(ACTUATOR_PIN_LED, LOW);

}

}

CAYENNE_IN(0)
{
b0 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_0, ACTUATOR_PIN_0, b0);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_0, b0);
}

CAYENNE_IN(1)
{
b1 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_1, ACTUATOR_PIN_1, b1);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_1, b1);
}

CAYENNE_IN(2)
{
b2 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_2, ACTUATOR_PIN_2, b2);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_2, b2);
}

CAYENNE_IN(3)
{
b3 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_3, ACTUATOR_PIN_3, b3);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_3, b3);
}

CAYENNE_IN(4)
{
b4 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_4, ACTUATOR_PIN_4, b4);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_4, b4);
}

CAYENNE_IN(5)
{
b5 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_5, ACTUATOR_PIN_5, b5);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_5, b5);
}

CAYENNE_IN(6)
{
b6 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_6, ACTUATOR_PIN_6, b6);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_6, b6);
}

CAYENNE_IN(7)
{
b7 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_7, ACTUATOR_PIN_7, b7);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_7, b7);
}

CAYENNE_IN(8)
{
b8 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_8, ACTUATOR_PIN_8, b8);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_8, b8);
}

CAYENNE_IN(9)
{
b9 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_9, ACTUATOR_PIN_9, b9);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_9, b9);
}

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

CAYENNE_IN(12)
{
int value = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_12, ACTUATOR_PIN_12, value);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_12, value);
}

CAYENNE_OUT(14)
{
Cayenne.virtualWrite(VIRTUAL_CHANNEL_14, analogRead(SENSOR_PIN_14));
}

The switch is connected to pin A4 and ORed to the button widgets. I turned on the programmer but the switch doesn’t activate the servo. It only works once cayenne is connected. I wanted the switch to work the first time the programmer opens even before cayenne connects. I might have misplaced the switch in the wrong location. I was hoping you help me figure out where to place it correctly so that the switch always works whether or not Cayenne is up.

Thank you in advance.

for the button to work in offline mode you have to use this code: How can i end the {cayenne.loop()} if there is no connection? - #5 by ahmed.blacky.xaver3
but then you will have to manually break the loop to connect to cayenne.

Hi Shramik,

Will this work?

Sorry forgot the code

/*
Cayenne Generic Digital Output Example

This sketch shows how to set up a Generic Digital Output with Cayenne.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:
1. In the Cayenne Dashboard add a new Generic Digital Output widget.
2. Select a virtual channel number for the widget.
3. Set the VIRTUAL_CHANNEL value below to virtual channel you selected.
4. Attach an output actuator (e.g. an LED) to a digital pin on your Arduino.
5. Set the ACTUATOR_PIN value below to the pin number you used when connecting your output actuator.
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
7. Compile and upload this sketch.
8. Once the Arduino connects to the Dashboard you can use the widget button to send digital values.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernetW5500.h>
#include <Servo.h> 
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "----";
char password[] = "";
char clientID[] = "----";

#define VIRTUAL_CHANNEL_LED 11
#define ACTUATOR_PIN_LED A0
#define VIRTUAL_CHANNEL_0 0
#define ACTUATOR_PIN_0 0
#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 1
#define VIRTUAL_CHANNEL_2 2 
#define ACTUATOR_PIN_2 2
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 3
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 4
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 5
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 A2
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 7
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 8
#define VIRTUAL_CHANNEL_9 9
#define ACTUATOR_PIN_9 9
#define VIRTUAL_CHANNEL_12 12
#define ACTUATOR_PIN_12 A1
#define VIRTUAL_CHANNEL_13 13
#define SENSOR_PIN_13 A4
#define VIRTUAL_CHANNEL_14 14
#define SENSOR_PIN_14 A5

// Do not use digital pins 0 or 1 since those conflict with the use of Serial.
Servo Servo1;
int servoPin = 6;  
void setup()
{
  
	Serial.begin(9600);
	Servo1.attach(servoPin); 
	pinMode(ACTUATOR_PIN_LED, OUTPUT);
  pinMode(ACTUATOR_PIN_0, OUTPUT);
  pinMode(ACTUATOR_PIN_1, OUTPUT);
  pinMode(ACTUATOR_PIN_2, OUTPUT);
  pinMode(ACTUATOR_PIN_3, OUTPUT);
  pinMode(ACTUATOR_PIN_4, OUTPUT);
  pinMode(ACTUATOR_PIN_5, OUTPUT);
  pinMode(ACTUATOR_PIN_6, OUTPUT);
  pinMode(ACTUATOR_PIN_7, OUTPUT);
  pinMode(ACTUATOR_PIN_8, OUTPUT);
  pinMode(ACTUATOR_PIN_9, OUTPUT);
  pinMode(ACTUATOR_PIN_12, OUTPUT);
  pinMode(SENSOR_PIN_13, INPUT);
  pinMode(SENSOR_PIN_14, INPUT);
	Cayenne.begin(username, password, clientID);
}
int b0;
int b1;
int b2;
int b3;
int b4;
int b5;
int b6;
int b7;
int b8;
int b9;

int Switch =0;




void loop()
{
	Cayenne.loop();
  
  if((b0 == 1) && (b1 == 1) && (b2 == 1) && (b3 == 1) && (b4 == 0) && (b5 == 0) && (b6 == 0) && (b7 == 0) && (b8 == 0) && (b9 == 0)) {

    Serial.print("Correct!");
    Servo1.write(180);
    digitalWrite(ACTUATOR_PIN_LED, HIGH);
 
  }else{
    Serial.print("Error!");
    Servo1.write(0);
    digitalWrite(ACTUATOR_PIN_LED, LOW);
    
  }
}

CAYENNE_CONNECTED() {
  Serial.println("connected.......");
}

CAYENNE_DISCONNECTED() {
  Serial.println("disconnected......");
  while (true)
  {
    Switch = digitalRead(SENSOR_PIN_13);
    if (Switch == 1){
    Servo1.write(180);
    }
  }
}

CAYENNE_IN(0)
{
  b0 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_0, ACTUATOR_PIN_0, b0);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_0, b0);
}

CAYENNE_IN(1)
{
  b1 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_1, ACTUATOR_PIN_1, b1);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_1, b1);
}

CAYENNE_IN(2)
{
  b2 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_2, ACTUATOR_PIN_2, b2);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_2, b2);
}

CAYENNE_IN(3)
{
  b3 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_3, ACTUATOR_PIN_3, b3);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_3, b3);
}

CAYENNE_IN(4)
{
  b4 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_4, ACTUATOR_PIN_4, b4);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_4, b4);
}

CAYENNE_IN(5)
{
  b5 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_5, ACTUATOR_PIN_5, b5);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_5, b5);
}

CAYENNE_IN(6)
{
  b6 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_6, ACTUATOR_PIN_6, b6);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_6, b6);
}

CAYENNE_IN(7)
{
  b7 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_7, ACTUATOR_PIN_7, b7);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_7, b7);
}

CAYENNE_IN(8)
{
  b8 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_8, ACTUATOR_PIN_8, b8);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_8, b8);
}

CAYENNE_IN(9)
{
  b9 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_9, ACTUATOR_PIN_9, b9);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_9, b9);
}




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

CAYENNE_IN(12)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_12, ACTUATOR_PIN_12, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_12, value);
}

CAYENNE_OUT(14)
{
  Cayenne.virtualWrite(VIRTUAL_CHANNEL_14, analogRead(SENSOR_PIN_14));
}

yup, should work. though this wont break if you again want to connect to cayenne. Also, what have you used pins 0 - 9 of arduino for?

pins 0 to 9 are the widget keypads. : )

will resetting the arduino Using the reset button) break the while loop? or just inserting break in the while statement?

so why are the arduino pins used then?

that’s the easiest option out there.

0-9 doesn’t connect to any device. I just used them for entering the password.

i guess you still did not understand it.
why there are two declaration here:

#define VIRTUAL_CHANNEL_0 0
#define ACTUATOR_PIN_0 0

CAYENNE_IN(0)
{
b0 = getValue.asInt();
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL_0, ACTUATOR_PIN_0, b0);
// Write the value received to the digital pin.
digitalWrite(ACTUATOR_PIN_0, b0);
}

what have you connected to ACTUATOR_PIN_0 of the arduino?

You are right. i do not need to make two declarations here. I guess just force of habit. : )

It is not about declaration it is about this line: digitalWrite(ACTUATOR_PIN_0, b0);

I guess I wanted to conform to the cayenne structure fearing that not doing so might not run the code but i do understand there are a lot f unnecessary things in the code. I guess I have a lot to learn.Cut me some slack man.: ) I am a work in progress.

remove all f unnecessary things from your code and share here once done.

Roger that Shramik. One last thing buddy. You said that pressing the reset button will break the loop. Will the break statement have the same effect?

not break statement, but you have to make the while loop false.

Here it is. Hope someone finds this code useful just as I did.

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernetW5500.h>
#include <Servo.h> 
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "----";
char password[] = "";
char clientID[] = "----";

#define VIRTUAL_CHANNEL_LED 11
#define ACTUATOR_PIN_LED A0
#define VIRTUAL_CHANNEL_0 0
#define ACTUATOR_PIN_0 0
#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 1
#define VIRTUAL_CHANNEL_2 2 
#define ACTUATOR_PIN_2 2
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 3
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 4
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 5
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 A2
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 7
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 8
#define VIRTUAL_CHANNEL_9 9
#define ACTUATOR_PIN_9 9
#define VIRTUAL_CHANNEL_12 12
#define ACTUATOR_PIN_12 A1
#define VIRTUAL_CHANNEL_13 13
#define SENSOR_PIN_13 A4
#define VIRTUAL_CHANNEL_14 14
#define SENSOR_PIN_14 A5

// Do not use digital pins 0 or 1 since those conflict with the use of Serial.
Servo Servo1;
int servoPin = 6;  
void setup()
{
  
  Serial.begin(9600);
  Servo1.attach(servoPin); 
  pinMode(ACTUATOR_PIN_LED, OUTPUT);
  pinMode(ACTUATOR_PIN_0, OUTPUT);
  pinMode(ACTUATOR_PIN_1, OUTPUT);
  pinMode(ACTUATOR_PIN_2, OUTPUT);
  pinMode(ACTUATOR_PIN_3, OUTPUT);
  pinMode(ACTUATOR_PIN_4, OUTPUT);
  pinMode(ACTUATOR_PIN_5, OUTPUT);
  pinMode(ACTUATOR_PIN_6, OUTPUT);
  pinMode(ACTUATOR_PIN_7, OUTPUT);
  pinMode(ACTUATOR_PIN_8, OUTPUT);
  pinMode(ACTUATOR_PIN_9, OUTPUT);
  pinMode(ACTUATOR_PIN_12, OUTPUT);
  pinMode(SENSOR_PIN_13, INPUT);
  pinMode(SENSOR_PIN_14, INPUT);
  Cayenne.begin(username, password, clientID);
}
int b0;
int b1;
int b2;
int b3;
int b4;
int b5;
int b6;
int b7;
int b8;
int b9;

int Switch =0;




void loop()
{
  Cayenne.loop();
  
  if((b0 == 1) && (b1 == 1) && (b2 == 1) && (b3 == 1) && (b4 == 0) && (b5 == 0) && (b6 == 0) && (b7 == 0) && (b8 == 0) && (b9 == 0)) {

    Serial.print("Correct!");
    Servo1.write(180);
    digitalWrite(ACTUATOR_PIN_LED, HIGH);
 
  }else{
    Serial.print("Error!");
    Servo1.write(0);
    digitalWrite(ACTUATOR_PIN_LED, LOW);
    
  }
}

CAYENNE_CONNECTED() {
  Serial.println("connected.......");
}

CAYENNE_DISCONNECTED() {
  Serial.println("disconnected......");
  while (true)
  {
    Switch = digitalRead(SENSOR_PIN_13);
    if (Switch == 1){
    Servo1.write(180);
    break;
    }
    else {
    Servo1.write(0);
    break;
    }
  }
}

CAYENNE_IN(0)
{
  b0 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_0, ACTUATOR_PIN_0, b0);
  
}

CAYENNE_IN(1)
{
  b1 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_1, ACTUATOR_PIN_1, b1);
  
}

CAYENNE_IN(2)
{
  b2 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_2, ACTUATOR_PIN_2, b2);
}

CAYENNE_IN(3)
{
  b3 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_3, ACTUATOR_PIN_3, b3);
}

CAYENNE_IN(4)
{
  b4 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_4, ACTUATOR_PIN_4, b4);
}

CAYENNE_IN(5)
{
  b5 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_5, ACTUATOR_PIN_5, b5);
}

CAYENNE_IN(6)
{
  b6 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_6, ACTUATOR_PIN_6, b6);
}

CAYENNE_IN(7)
{
  b7 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_7, ACTUATOR_PIN_7, b7);
}

CAYENNE_IN(8)
{
  b8 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_8, ACTUATOR_PIN_8, b8);
}

CAYENNE_IN(9)
{
  b9 = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_9, ACTUATOR_PIN_9, b9);
}




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

CAYENNE_IN(12)
{
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", VIRTUAL_CHANNEL_12, ACTUATOR_PIN_12, value);
  // Write the value received to the digital pin.
  digitalWrite(ACTUATOR_PIN_12, value);
}

CAYENNE_OUT(14)
{
  Cayenne.virtualWrite(VIRTUAL_CHANNEL_14, analogRead(SENSOR_PIN_14));
}

even better if you want to improve your code:

CAYENNE_IN_DEFAULT()
{
  switch (request.channel) {
    case 0 :
      b0 = getValue.asInt();
      break;
    case 1 :
      b1 = getValue.asInt();
      break;
    case 2 :
      b2 = getValue.asInt();
      break;
    case 3 :
      b3 = getValue.asInt();
      break;
  }
}

the code to handle the reconnect won’t work, Try this: connect another button to your Arduino.

CAYENNE_DISCONNECTED() {
  Serial.println("disconnected…");
  while (digitalRead(pushButton))
  {
     Switch = digitalRead(SENSOR_PIN_13);
     if (Switch == 1){
     Servo1.write(180);
  }
}

I didn’t get it Shramik. What is the pushButton for? And when you said connect another button, do you mean aside from the SENSOR_PIN_13 that I already have?

Isn’t the while (digitalRead(pushButton)) the same as while (true)?