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.