Final Arduino Code: Button + Voice + Timer Control
// Combined Bluetooth Control System
// Button Commands + Voice Commands + Timer Control
#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1); // HC-06 Bluetooth module
// Relay pins
int lamp1 = 2;
int lamp2 = 3;
int fan = 4;
int motor = 5;
// Timer variables
unsigned long timerStart[4] = {0,0,0,0};
unsigned long timerDuration[4] = {0,0,0,0};
bool timerActive[4] = {false,false,false,false};
void setup() {
[Link](9600);
[Link](9600);
pinMode(lamp1, OUTPUT);
pinMode(lamp2, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(motor, OUTPUT);
digitalWrite(lamp1, HIGH);
digitalWrite(lamp2, HIGH);
digitalWrite(fan, HIGH);
digitalWrite(motor, HIGH);
[Link]("System Ready.");
}
void loop() {
// Check Bluetooth input
if ([Link]()) {
String cmd = [Link]('\n');
[Link]();
[Link]("CMD: ");
[Link](cmd);
// ============================
// Button & Voice Commands
// ============================
if (cmd == "L" || cmd == "lamp on") {
digitalWrite(lamp1, LOW);
}
if (cmd == "l" || cmd == "lamp off") {
digitalWrite(lamp1, HIGH);
}
if (cmd == "G" || cmd == "lamp2 on") {
digitalWrite(lamp2, LOW);
}
if (cmd == "g" || cmd == "lamp2 off") {
digitalWrite(lamp2, HIGH);
}
if (cmd == "F" || cmd == "fan on") {
digitalWrite(fan, LOW);
}
if (cmd == "f" || cmd == "fan off") {
digitalWrite(fan, HIGH);
}
if (cmd == "M" || cmd == "motor on") {
digitalWrite(motor, LOW);
}
if (cmd == "m" || cmd == "motor off") {
digitalWrite(motor, HIGH);
}
// ============================
// Timer Commands
// Format: T1 10 (Lamp1 ON for 10 seconds)
// ============================
if ([Link]("T")) {
int load = [Link](1,2).toInt();
int sec = [Link](3).toInt();
int pin;
switch(load){
case 1: pin = lamp1; break;
case 2: pin = lamp2; break;
case 3: pin = fan; break;
case 4: pin = motor; break;
default: pin = -1; break;
}
if(pin != -1){
digitalWrite(pin, LOW);
timerStart[load-1] = millis();
timerDuration[load-1] = sec * 1000UL;
timerActive[load-1] = true;
}
}
}
// ============================
// Timer Processing
// ============================
for(int i=0; i<4; i++){
if(timerActive[i]){
if(millis() - timerStart[i] >= timerDuration[i]){
int pin;
if(i==0) pin = lamp1;
if(i==1) pin = lamp2;
if(i==2) pin = fan;
if(i==3) pin = motor;
digitalWrite(pin, HIGH);
timerActive[i] = false;
}
}
}