#include <Wire.h>
#include <EEPROM.h>
#include <IRremote.h>
int irPin=11;
char Received;
IRrecv irrecv(irPin);
decode_results results;
int relay2=2;
int relay3=3;
int relay4=4;
int relay5=5;
int M2=EEPROM.read(0);
int M3=EEPROM.read(1);
int M4=EEPROM.read(2);
int M5=EEPROM.read(3);
int sw2= 14;
int sw3= 15;
int sw4= 16;
int sw5= 17;
void setup()
{
Serial.begin(9600);
pinMode(irPin,INPUT);
pinMode(sw2,INPUT);
pinMode(sw3,INPUT);
pinMode(sw4,INPUT);
pinMode(sw5,INPUT);
digitalWrite(sw2,HIGH);
digitalWrite(sw3,HIGH);
digitalWrite(sw4,HIGH);
digitalWrite(sw5,HIGH);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
pinMode(relay5,OUTPUT);
pinMode(11,INPUT); // power for ir sensor
digitalWrite(relay2,M2);
digitalWrite(relay3,M3);
digitalWrite(relay4,M4);
digitalWrite(relay5,M5);
if (M2==1){Serial.print("Relay 2 OFF");}
else{Serial.print("Relay 2 ON"); }
if (M3==1){Serial.print("Relay 3 OFF"); }
else{Serial.print("Relay 3 ON"); }
if (M4==1){Serial.print("Relay 4 OFF"); }
else{Serial.print("Relay 4 ON"); }
if (M5==1){Serial.print("Relay 5 OFF"); }
else{Serial.println("Relay 5 ON"); }
delay(1000);
irrecv.enableIRIn();
}
void loop(){
int s2=digitalRead(sw2);
int s3=digitalRead(sw3);
int s4=digitalRead(sw4);
int s5=digitalRead(sw5);
////////////////////////////////////////////// IR Remote Code //////////////////////////////////////////////////////////////////////
if (irrecv.decode(&results)){
Serial.println(results.value, DEC);
Serial.println(results.value, HEX);
if (results.value ==16756815 || results.value ==16740495 || results.value ==33444015)
{switch (M2){
case 0: M2=1; EEPROM.write(0,M2); Serial.println("Relay 2 OFF"); digitalWrite(relay2, HIGH); delay(100); break;
case 1: M2=0; EEPROM.write(0,M2); Serial.println("Relay 2 ON"); digitalWrite(relay2, LOW); delay(100); break; }}
if (results.value ==16750695 || results.value ==16742535 || results.value ==33478695)
{switch (M3){
case 0: M3=1; EEPROM.write(1,M3); Serial.println("Relay 3 OFF"); digitalWrite(relay3, HIGH); delay(100); break;
case 1: M3=0; EEPROM.write(1,M3); Serial.println("Relay 3 ON"); digitalWrite(relay3, LOW); delay(100); break; }}
if (results.value ==16767015 || results.value ==16734375 ||results.value ==33486855)
{ switch (M4){
case 0: M4=1; EEPROM.write(3,M4); Serial.println("Relay 4 OFF"); digitalWrite(relay4, HIGH); delay(100); break;
case 1: M4=0; EEPROM.write(3,M4); Serial.println("Relay 4 ON"); digitalWrite(relay4, LOW); delay(100); break; }}
if (results.value ==16744575 || results.value ==16732335|| results.value == 33435855 )
{switch (M5){
case 0: M5=1; EEPROM.write(4,M5); Serial.println("Relay 5 OFF"); digitalWrite(relay5, HIGH); delay(100); break;
case 1: M5=0; EEPROM.write(4,M5); Serial.println("Relay 5 ON"); digitalWrite(relay5, LOW); delay(100); break; }}
if (results.value ==16748655 ||results.value == 33441975)
{
M2=1; EEPROM.write(0,M2); Serial.println("Relay 2 OFF"); digitalWrite(relay2, HIGH); delay(100);
M3=1; EEPROM.write(1,M3); Serial.println("Relay 3 OFF"); digitalWrite(relay3, HIGH); delay(100);
M4=1; EEPROM.write(2,M4); Serial.println("Relay 4 OFF"); digitalWrite(relay4, HIGH); delay(100);
M5=1; EEPROM.write(3,M5); Serial.println("Relay 5 OFF"); digitalWrite(relay5, HIGH); delay(100); }
if (results.value ==16760895 ||results.value == 33454215)
{
M2=0; EEPROM.write(0,M2); Serial.println("Relay 2 ON"); digitalWrite(relay2, LOW); delay(100);
M3=0; EEPROM.write(1,M3); Serial.println("Relay 3 ON"); digitalWrite(relay3, LOW); delay(100);
M4=0; EEPROM.write(2,M4); Serial.println("Relay 4 ON"); digitalWrite(relay4, LOW); delay(100);
M5=0; EEPROM.write(3,M5); Serial.println("Relay 5 ON"); digitalWrite(relay5, LOW); delay(100); }
irrecv.resume();
delay(10);
}
////////////////////////////////////////////// Bluetooth Code //////////////////////////////////////////////////////////////////////
if(Serial.available()>0)
{
Received = Serial.read();
Serial.println(Received);
}
if (Received =='a'){M2=1; EEPROM.write(0,M2); Serial.println("Relay 2 OFF"); digitalWrite(relay2,HIGH); Received ='o'; delay(250);}
if (Received =='A'){M2=0; EEPROM.write(0,M2); Serial.println("Relay 2 ON"); digitalWrite(relay2, LOW); Received ='o'; delay(250);}
if (Received =='b'){M3=1; EEPROM.write(1,M3); Serial.println("Relay 3 OFF"); digitalWrite(relay3,HIGH); Received ='o'; delay(250);}
if (Received =='B'){M3=0; EEPROM.write(1,M3); Serial.println("Relay 3 ON"); digitalWrite(relay3, LOW); Received ='o'; delay(250);}
if (Received =='c'){M4=1; EEPROM.write(2,M4); Serial.println("Relay 4 OFF"); digitalWrite(relay4,HIGH); Received ='o'; delay(250);}
if (Received =='C'){M4=0; EEPROM.write(2,M4); Serial.println("Relay 4 ON"); digitalWrite(relay4, LOW); Received ='o'; delay(250);}
if (Received =='d'){M5=1; EEPROM.write(3,M5); Serial.println("Relay 5 OFF"); digitalWrite(relay5,HIGH); Received ='o'; delay(250);}
if (Received =='D'){M5=0; EEPROM.write(3,M5); Serial.println("Relay 5 ON"); digitalWrite(relay5, LOW); Received ='o'; delay(250);}
if (Received =='z'){
M2=1; EEPROM.write(0,M2); Serial.println("Relay 2 OFF"); digitalWrite(relay2, HIGH); delay(100);
M3=1; EEPROM.write(1,M3); Serial.println("Relay 3 OFF"); digitalWrite(relay3, HIGH); delay(100);
M4=1; EEPROM.write(2,M4); Serial.println("Relay 4 OFF"); digitalWrite(relay4, HIGH); delay(100);
M5=1; EEPROM.write(3,M5); Serial.println("Relay 5 OFF"); digitalWrite(relay5, HIGH); delay(100); Received ='o';}
if (Received =='Z'){
M2=0; EEPROM.write(0,M2); Serial.println("Relay 2 ON"); digitalWrite(relay2, LOW); delay(100);
M3=0; EEPROM.write(1,M3); Serial.println("Relay 3 ON"); digitalWrite(relay3, LOW); delay(100);
M4=0; EEPROM.write(2,M4); Serial.println("Relay 4 ON"); digitalWrite(relay4, LOW); delay(100);
M5=0; EEPROM.write(3,M5); Serial.println("Relay 5 ON"); digitalWrite(relay5, LOW); delay(100); Received ='o';}
delay(10);
////////////////////////////////////////////// Push Button Code //////////////////////////////////////////////////////////////////////
if (s2==LOW){ switch (M2){
case 0: M2=1; EEPROM.write(0,M2); Serial.println("Relay 2 OFF"); digitalWrite(relay2, HIGH); delay(500);break;
case 1: M2=0; EEPROM.write(0,M2); Serial.println("Relay 2 ON"); digitalWrite(relay2, LOW);delay(500); break; }}
if (s3==LOW){ switch (M3){
case 0: M3=1; EEPROM.write(1,M3); Serial.println("Relay 3 OFF"); digitalWrite(relay3, HIGH);delay(500); break;
case 1: M3=0; EEPROM.write(1,M3); Serial.println("Relay 3 ON"); digitalWrite(relay3, LOW);delay(500); break; }}
if (s4==LOW){ switch (M4){
case 0: M4=1; EEPROM.write(2,M4); Serial.println("Relay 4 OFF"); digitalWrite(relay4, HIGH);delay(500); break;
case 1: M4=0; EEPROM.write(2,M4); Serial.println("Relay 4 ON"); digitalWrite(relay4, LOW);delay(500); break; }}
if (s5==LOW){ switch (M5){
case 0: M5=1; EEPROM.write(3,M5); Serial.println("Relay 5 OFF"); digitalWrite(relay5, HIGH);delay(500); break;
case 1: M5=0; EEPROM.write(3,M5); Serial.println("Relay 5 ON"); digitalWrite(relay5, LOW); delay(500);break; }}
delay(10);
}
No comments:
Post a Comment