Not sure if this is a good place to ask this but I’ve just got voice command to connect to one or more relays as a revised simple led on/off script. But I want to add a bank of relays via gpio numbers.
How to switch individual relays has me stumped within one script. I set up the IFTTT commands and then connect to the script below but want to set up commands for each relay. So typical commnad “Relay 1 on” “Relay 1 off”, etc. How do I do this?
I know I need some sort of If then else nesting but can anyone give me any pointers?
int relay02 = D4; //pin to which relay is connected
int relay03 = D5; //ditto
int relay04 = D6; //ditto
int relay05 = D7; //ditto
int relay06 = D8; //ditto
bool vin = LOW; //a virtual boolean variable
// setup() is run only once, it's where we set up GPIO and initialise peripherals
void setup() {
// Setup GPIO
pinMode(relay02,OUTPUT); // relay2 pin is set as output
digitalWrite(relay02,HIGH);
pinMode(relay03,OUTPUT); // relay3 pin is set as output
digitalWrite(relay03,HIGH);
pinMode(relay04,OUTPUT); // etc
digitalWrite(relay04,HIGH);
pinMode(relay05,OUTPUT);
digitalWrite(relay05,HIGH);
pinMode(relay06,OUTPUT);
digitalWrite(relay06,HIGH);
// Subscribe to events published by IFTTT using Particle.subscribe
Particle.subscribe("LOWERPATH-OFFxxx", myHandler2); // turning off .. Want to vary this as Relay 01, 02 etc
Particle.subscribe("LOWERPATH-ONxxx", thisHandler2); // turning on...I Want to add more commands to switch differing relays.
}
// loop() runs continuously, it's our infinite loop.
void loop() {
if (vin==HIGH)
{
digitalWrite(relay02,LOW); //I want to IF this part, ie, If LOWERPATH-OFFxxx Then
digitalWrite(relay03,LOW); // If Relay3-ON then.. etc
digitalWrite(relay04,LOW);
digitalWrite(relay05,LOW);
digitalWrite(relay06,LOW);
}
else if (vin==LOW)
{
digitalWrite(relay02,HIGH);
digitalWrite(relay03,HIGH);
digitalWrite(relay04,HIGH);
digitalWrite(relay05,HIGH);
digitalWrite(relay06,HIGH);
}
}
//our events are called when IFTTT applets are triggered
void myHandler2(const char *event, const char *data)
{
vin=LOW;
}
void thisHandler2(const char *event, const char *data)
{
vin=HIGH;