Arduino IOT Physical switch OR Alexa

hi,
So i am working on a project to control lights with an Arduino nano IOT, I am using 4 230v relays control by the nano. What i want to do is be able to control the lights with either a physical switch or with an amazon echo. I have configured the 'relays' in the arduino cloud as read/write variables.

  if (switch_1 ==LOW || relay_1==HIGH)
          {digitalWrite(LED_LIGHTS_1,HIGH);
          Serial.print("Lights 1 ON\n");}
      else if(wisebox_1 ==HIGH && relay_1==LOW)
          {digitalWrite(LED_LIGHTS_1,LOW);
          Serial.print("Lights 1 OFF\n");
          }

This piece of code works to a certain extent, however it means i can not switch the system independent of the other,, I plan to change the switch to a momentary push button, so that this will change the state on 'relay_1' in the amazon app and the arduino code.
Any help is much appreciated

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Relay_1"
  https://create.arduino.cc/cloud/things/7c5e2098-ba55-48c7-b873-075655e50092 

  Arduino IoT Cloud Properties description

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

  CloudSmartPlug relay_2;
  CloudSmartPlug relay_1;
  CloudSmartPlug relay_3;
  CloudSmartPlug relay_4;

  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"
int wisebox1 = 2;
int wisebox2 = 3;
int wisebox3 = 4;
int wisebox4 = 5;
int LED_LIGHTS_1 = 6;
int LED_LIGHTS_2 = 7;
int LED_LIGHTS_3 = 8;
int LED_LIGHTS_4 = 9;
int wisebox_1,wisebox_2,wisebox_3,wisebox_4;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  
    // LEDs
    pinMode(LED_LIGHTS_1, OUTPUT);
    pinMode(LED_LIGHTS_2, OUTPUT);
    pinMode(LED_LIGHTS_3, OUTPUT);
    pinMode(LED_LIGHTS_4, OUTPUT);
    digitalWrite(LED_LIGHTS_1, LOW);
    digitalWrite(LED_LIGHTS_2, LOW);
    digitalWrite(LED_LIGHTS_3, LOW);
    digitalWrite(LED_LIGHTS_4, LOW);
    pinMode(wisebox1,INPUT);
    pinMode(wisebox2,INPUT);
    pinMode(wisebox3,INPUT);
    pinMode(wisebox4,INPUT);
    
    
  // 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 
  wisebox_1= digitalRead(wisebox1);
  wisebox_2= digitalRead(wisebox2);
  wisebox_3= digitalRead(wisebox3);
  wisebox_4= digitalRead(wisebox4);
 
 
  if (wisebox_1 ==LOW || relay_1==HIGH)
          {digitalWrite(LED_LIGHTS_1,HIGH);
          Serial.print("Lights 1 ON\n");}
      else if(wisebox_1 ==HIGH && relay_1==LOW)
          {digitalWrite(LED_LIGHTS_1,LOW);
          Serial.print("Lights 1 OFF\n");
          } 
  if (wisebox_2 ==LOW || relay_2==HIGH)
          {digitalWrite(LED_LIGHTS_2,HIGH);
          Serial.print("Lights 2 ON\n");}
      else if(wisebox_2 ==HIGH && relay_2==LOW)
          {digitalWrite(LED_LIGHTS_2,LOW);
          Serial.print("Lights 2 OFF\n");
          } 
  if (wisebox_3 ==LOW || relay_3==HIGH)
          {digitalWrite(LED_LIGHTS_3,HIGH);
          Serial.print("Lights 3 ON\n");}
      else if(wisebox_3 ==HIGH && relay_3==LOW)
          {digitalWrite(LED_LIGHTS_3,LOW);
          Serial.print("Lights 3 OFF\n");
          } 
  if (wisebox_4 ==LOW || relay_4==HIGH)
          {digitalWrite(LED_LIGHTS_4,HIGH);
          Serial.print("Lights 4 ON\n");}
      else if(wisebox_4 ==HIGH && relay_4==LOW)
          {digitalWrite(LED_LIGHTS_4,LOW);
          Serial.print("Lights 4 OFF\n");
          } 
  
}


void onRelay1Change() {
// do something
}



void onRelay2Change() {
  // Do something
}



void onRelay3Change() {
  // Do something
}


void onRelay4Change() {
  // Do something
}

That is the full code for the sketch, the wisebox inputs are inputs from physical switches

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.