#include <SoftwareSerial.
h>
SoftwareSerial BT(0, 1); // RX, TX
const int fanPin = 3;
const int ledPin = 4;
const int buttonPin = 2;
bool fanStatus = false;
void setup() {
pinMode(fanPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
BT.begin(9600);
digitalWrite(fanPin, LOW);
digitalWrite(ledPin, LOW);
void loop() {
if (BT.available()) {
String cmd = BT.readStringUntil('\n');
cmd.trim();
if (cmd == "ON") {
fanStatus = true;
} else if (cmd == "OFF") {
fanStatus = false;
updateFan();
if (digitalRead(buttonPin) == LOW) {
fanStatus = !fanStatus;
updateFan();
delay(500); // Debounce delay
void updateFan() {
digitalWrite(fanPin, fanStatus ? HIGH : LOW);
digitalWrite(ledPin, fanStatus ? HIGH : LOW);
BT.println(fanStatus ? "Fan ON" : "Fan OFF");