Hi, I am a complete novice and have adapted a sketch I found to operate two 4 relay modules.
The problem I have is that when the Mega 2560 powers up the relays energise and stay on even though the Cayenne dashboard shows the relevant buttons as off.
The only way I can resolve the issue is to switch on all buttons and then back off again. The relays all then work as they should do. The problem reoccurs when the Mega is switched off and then back on again.
I have very little understanding of the coding aspect so any help would be appreciated - code as follows:
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;
#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 22 // 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 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
//#define VIRTUAL_CHANNEL_9 9
//#define ACTUATOR_PIN_9 38 // 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);
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);
Cayenne.begin(username, password, clientID);
}
void loop()
{
Cayenne.loop();
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_3, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_4, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_5, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_6, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_7, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_7, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_8, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_8, LOW);
}
}
//CAYENNE_IN(VIRTUAL_CHANNEL_9)
//{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
//if (getValue.asInt() == 0) {
//digitalWrite(ACTUATOR_PIN_9, HIGH);
//}
//else {
//digitalWrite(ACTUATOR_PIN_9, LOW);
//}
//}
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “”;
char password = “”;
char clientID = “”;
#define VIRTUAL_CHANNEL_1 1
#define ACTUATOR_PIN_1 22 // 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 24 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_3 3
#define ACTUATOR_PIN_3 26 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_4 4
#define ACTUATOR_PIN_4 28 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_5 5
#define ACTUATOR_PIN_5 30 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_6 6
#define ACTUATOR_PIN_6 32 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_7 7
#define ACTUATOR_PIN_7 34 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
#define VIRTUAL_CHANNEL_8 8
#define ACTUATOR_PIN_8 36 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.
//#define VIRTUAL_CHANNEL_9 9
//#define ACTUATOR_PIN_9 38 // 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);
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);
Cayenne.begin(username, password, clientID);
}
void loop()
{
Cayenne.loop();
}
// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL_1)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_1, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_1, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_2)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_2, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_2, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_3)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_3, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_3, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_4)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_4, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_4, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_5)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_5, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_5, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_6)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_6, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_6, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_7)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_7, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_7, LOW);
}
}
CAYENNE_IN(VIRTUAL_CHANNEL_8)
{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
if (getValue.asInt() == 0) {
digitalWrite(ACTUATOR_PIN_8, HIGH);
}
else {
digitalWrite(ACTUATOR_PIN_8, LOW);
}
}
//CAYENNE_IN(VIRTUAL_CHANNEL_9)
//{
// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
//if (getValue.asInt() == 0) {
//digitalWrite(ACTUATOR_PIN_9, HIGH);
//}
//else {
//digitalWrite(ACTUATOR_PIN_9, LOW);
//}
//}