#include <SoftwareSerial.
h>
// Define Bluetooth RX and TX pins
SoftwareSerial Bluetooth(0, 1); // RX = 10, TX = 11
// Relay pin definitions
const int relayPins[8] = { 2, 3, 4, 5, 6, 7, 8, 9 };
// Characters to turn relays ON ('a', 'c', ..., 'o')
// and OFF ('b', 'd', ..., 'p')
const char turnOnCommands[8] = { 'a', 'c', 'e', 'g', 'i', 'k', 'm', 'o' }; //a=97
const char turnOffCommands[8] = { 'b', 'd', 'f', 'h', 'j', 'l', 'n', 'p' };
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize Bluetooth Serial communication
Bluetooth.begin(9600);
// Set all relay pins as outputs and turn them OFF
for (int i = 0; i < 8; i++) {
pinMode(relayPins[i], OUTPUT);
digitalWrite(relayPins[i], HIGH);
}
Serial.println("Bluetooth Relay Control Ready");
Bluetooth.println("Bluetooth Relay Control Ready");
}
void loop() {
if (Bluetooth.available()) {
char command = Bluetooth.read();
//Serial.println(command);
for (int i = 0; i < 8; i++) {
if (command == turnOnCommands[i]) {
digitalWrite(relayPins[i], HIGH);
Serial.print("Relay ");
Serial.print(i + 1);
Serial.println(" turned ON");
Bluetooth.print("Relay ");
Bluetooth.print(i + 1);
Bluetooth.println(" turned ON");
} else if (command == turnOffCommands[i]) {
digitalWrite(relayPins[i], LOW);
Serial.print("Relay ");
Serial.print(i + 1);
Serial.println(" turned OFF");
Bluetooth.print("Relay ");
Bluetooth.print(i + 1);
Bluetooth.println(" turned OFF");
}
}
}
}