0% found this document useful (0 votes)
9 views2 pages

Arduino IR Remote Control Code

This document contains an Arduino code for controlling home appliances via infrared (IR) remote and serial commands. It defines pins for relays controlling a light, fan, TV, and charger, and includes functions for toggling their states and providing feedback. The setup initializes the serial communication and IR receiver, while the loop handles incoming IR signals and serial commands to manage the devices.

Uploaded by

siddeshgowdam8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Arduino IR Remote Control Code

This document contains an Arduino code for controlling home appliances via infrared (IR) remote and serial commands. It defines pins for relays controlling a light, fan, TV, and charger, and includes functions for toggling their states and providing feedback. The setup initializes the serial communication and IR receiver, while the loop handles incoming IR signals and serial commands to manage the devices.

Uploaded by

siddeshgowdam8
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

08.

21 12:10 PM
// ===== Arduino: IR + Serial Home Extension (with status) =====
// IRremote v3.x recommended.

#include <IRremote.h>

// ---- Pins ----


#define IR_RECEIVE_PIN 11
#define RELAY1 2 // Light
#define RELAY2 3 // Fan
#define RELAY3 4 // TV
#define RELAY4 5 // Charger

// ---- IR Hex codes (REPLACE with yours) ----


#define LIGHT_TOGGLE 0xFFA25D
#define FAN_TOGGLE 0xFF629D
#define TV_TOGGLE 0xFFE21D
#define CHARGER_TOGGLE 0xFF22DD

// LOW=ON, HIGH=OFF for most 4-ch relay boards


inline void setRelayPin(int pin, bool on) { digitalWrite(pin, on ? LOW : HIGH); }

bool stateLight=false, stateFan=false, stateTV=false, stateCharger=false;

void feedback(const char* msg){ [Link](msg); }

void applyAndReply(int idx, bool on){


switch(idx){
case 0: stateLight=on; setRelayPin(RELAY1,on);
feedback(on?"LIGHT_ON_OK":"LIGHT_OFF_OK"); break;
case 1: stateFan=on; setRelayPin(RELAY2,on);
feedback(on?"FAN_ON_OK":"FAN_OFF_OK"); break;
case 2: stateTV=on; setRelayPin(RELAY3,on);
feedback(on?"TV_ON_OK":"TV_OFF_OK"); break;
case 3: stateCharger=on; setRelayPin(RELAY4,on);
feedback(on?"CHARGER_ON_OK":"CHARGER_OFF_OK"); break;
}
}

void toggleAndReply(int idx){


switch(idx){
case 0: stateLight=!stateLight; setRelayPin(RELAY1,stateLight);
feedback(stateLight?"LIGHT_ON_OK":"LIGHT_OFF_OK"); break;
case 1: stateFan=!stateFan; setRelayPin(RELAY2,stateFan);
feedback(stateFan?"FAN_ON_OK":"FAN_OFF_OK"); break;
case 2: stateTV=!stateTV; setRelayPin(RELAY3,stateTV);
feedback(stateTV?"TV_ON_OK":"TV_OFF_OK"); break;
case 3: stateCharger=!stateCharger;
setRelayPin(RELAY4,stateCharger);feedback(stateCharger?"CHARGER_ON_OK":"CHARGER_OFF
_OK"); break;
}
}

void printStatus(){
[Link]("STATUS ");
[Link]("LIGHT="); [Link](stateLight ? "ON " : "OFF ");
[Link]("FAN="); [Link](stateFan ? "ON " : "OFF ");
[Link]("TV="); [Link](stateTV ? "ON " : "OFF ");
[Link]("CHARGER="); [Link](stateCharger ? "ON" : "OFF");
}

void queryDevice(const String& dev){


if(dev=="LIGHT") [Link](stateLight ? "LIGHT=ON" : "LIGHT=OFF");
else if(dev=="FAN") [Link](stateFan ? "FAN=ON" : "FAN=OFF");
else if(dev=="TV") [Link](stateTV ? "TV=ON" : "TV=OFF");
else if(dev=="CHARGER") [Link](stateCharger ? "CHARGER=ON" :
"CHARGER=OFF");
else [Link]("UNKNOWN_DEVICE");
}

void setup(){
[Link](9600);
[Link](IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);

pinMode(RELAY1,OUTPUT); pinMode(RELAY2,OUTPUT);
pinMode(RELAY3,OUTPUT); pinMode(RELAY4,OUTPUT);

// Default OFF
setRelayPin(RELAY1,false); setRelayPin(RELAY2,false);
setRelayPin(RELAY3,false); setRelayPin(RELAY4,false);
}

void loop(){
// ---- IR handling ----
if([Link]()){
unsigned long val = [Link];
if(val==LIGHT_TOGGLE) toggleAndReply(0);
else if(val==FAN_TOGGLE) toggleAndReply(1);
else if(val==TV_TOGGLE) toggleAndReply(2);
else if(val==CHARGER_TOGGLE) toggleAndReply(3);
[Link]();
}

// ---- Serial handling ----


if([Link]()){
String cmd = [Link]('\n'); [Link]();

if (cmd=="LIGHT_ON") applyAndReply(0,true);
else if (cmd=="LIGHT_OFF") applyAndReply(0,false);
else if (cmd=="FAN_ON") applyAndReply(1,true);
else if (cmd=="FAN_OFF") applyAndReply(1,false);
else if (cmd=="TV_ON") applyAndReply(2,true);
else if (cmd=="TV_OFF") applyAndReply(2,false);
else if (cmd=="CHARGER_ON") applyAndReply(3,true);
else if (cmd=="CHARGER_OFF") applyAndReply(3,false);
else if (cmd=="STATUS") printStatus();
else if ([Link]("QUERY ")){
String d = [Link](6); [Link](); [Link]();
queryDevice(d);
}
else if (cmd=="PING") feedback("PONG");
}
}

You might also like