Struggling to get with code to get relays to work from arduino iot

Hi All,

I an fairly new to arduino, I've been trying to get a 4 relay module to work from arduino iot cloud.
I am using the arduino mkr1010, ive gone though the setup process the code is loaded onto the arduino.
I've defined the pins and the pinModes in set up, but iam struggling in the loop to be able to turn them on and off. The aim is to be able to control via alexa.

my code is

/*
Sketch generated by the Arduino IoT Cloud Thing "garden"
https://create.arduino.cc/cloud/things/72316aad-6788-4408-aab1-5ee736b79c9d

Arduino IoT Cloud Properties description

The following variables are automatically generated and updated when changes are made to the Thing properties

CloudSwitch relay1;
CloudSwitch relay2;
CloudSwitch relay3;
CloudSwitch relay4;

Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#define relay1 2
#define relay2 3
#define relay3 4
#define relay4 5
void setup() {

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);

// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();
// Your code here

}

void onRelay1Change() {
// Do something
if (digitalRead(relay1) == 1){
digitalWrite(relay1, LOW);
}
if (digitalRead(relay1) == 0){
digitalWrite(relay1, HIGH);
}
}

void onRelay2Change() {
// Do something

}

void onRelay3Change() {
// Do somthing
}

void onRelay4Change() {
// Do somthing
}

I haven't filled the last 3 as I am just trying to get one to work first

any help is very much appreciated

Kind Regards

James

  if (digitalRead(relay1)  == 1){
    digitalWrite(relay1, LOW);  << write the output low
  }
  if (digitalRead(relay1) == 0){  << then read the low and immediately write it  high
    digitalWrite(relay1, HIGH);
  }

Give the relay a chance to react to the low. Add a simple delay after turning it on for example.

thank for the reply Crossroads,

unfortunately that didn’t work, ive added exta bit of code to test relay in setup to see if i had it wired up wrong but all seems well on front.

i was wondering if i am missing code anywhere

/*
Sketch generated by the Arduino IoT Cloud Thing “garden”
https://create.arduino.cc/cloud/things/72316aad-6788-4408-aab1-5ee736b79c9d

Arduino IoT Cloud Properties description

The following variables are automatically generated and updated when changes are made to the Thing properties

CloudSwitch relay1;
CloudSwitch relay2;
CloudSwitch relay3;
CloudSwitch relay4;

Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include “thingProperties.h”
#define relay1 2
#define relay2 3
#define relay3 4
#define relay4 5
void setup() {

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(relay1, HIGH);// add to test to see if relays are working correctly
delay(1000);
digitalWrite(relay1, LOW);
delay(1000);
digitalWrite(relay1, HIGH);
delay(1000);
digitalWrite(relay1, LOW);
delay(2000);

// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();
// Your code here

}

void onRelay1Change() {
// Do something
if (digitalRead(relay1) == 1){
digitalWrite(relay1, LOW);
delay(5000);
}
if (digitalRead(relay1) == 0){
digitalWrite(relay1, HIGH);
delay(5000);
}
}

void onRelay2Change() {
// Do something

}

void onRelay3Change() {

}

void onRelay4Change() {

}

any help is appreciated

james

Stick a serial print statement in onRelay1Change so you can see if it's being called.

Thank you wildbill,

as soon as i open the serial moniter it came up with a error
ERROR - please verify your THING ID
which ive double checked and even copied and pasted to make sure that is is correct but iam still gett the error
i pay for my arduino account so i can more board later on.
iam going to wipe all the things from my arduino account and start from strach too see if that works

Have you check the relay board and made it work before connecting it to an arduino pin? Verify that while you are working on the code side of things.