CODE
// Define the pin numbers for the relays
const int relay1Pin = 3;
const int relay2Pin = 4;
const int relay3Pin = 5;
const int relay4Pin = 6;
void setup() {
// Start the serial communication
Serial.begin(9600);
// Set relay pins as output
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(relay4Pin, OUTPUT);
// Make sure all relays are turned off initially
digitalWrite(relay1Pin, LOW);
digitalWrite(relay2Pin, LOW);
digitalWrite(relay3Pin, LOW);
digitalWrite(relay4Pin, LOW);
}
void loop() {
// Check if data is available in the serial buffer
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n'); // Read the command
// Check the command and turn on/off the corresponding relay
if (command == "back light on") {
digitalWrite(relay1Pin, LOW); // Turn on relay 1
Serial.println("Relay 1 is ON");
}
else if (command == "back light off") {
digitalWrite(relay1Pin, HIGH); // Turn off relay 1
Serial.println("Relay 1 is OFF");
}
else if (command == "room light on") {
digitalWrite(relay2Pin, LOW); // Turn on relay 2
Serial.println("Relay 2 is ON");
}
else if (command == "room light off") {
digitalWrite(relay2Pin, HIGH); // Turn off relay 2
Serial.println("Relay 2 is OFF");
}
else if (command == "bathroom light on") {
digitalWrite(relay3Pin, LOW); // Turn on relay 3
Serial.println("Relay 3 is ON");
}
else if (command == "bathroom light off") {
digitalWrite(relay3Pin, HIGH); // Turn off relay 3
Serial.println("Relay 3 is OFF");
}
else if (command == "home light on") {
digitalWrite(relay4Pin, LOW); // Turn on relay 4
Serial.println("Relay 4 is ON");
}
else if (command == "home light off") {
digitalWrite(relay4Pin, HIGH); // Turn off relay 4
Serial.println("Relay 4 is OFF");
}
else {
Serial.println("Unknown command");
}
}
}