// C++ code
//
int irpin = 13;
unsigned long duration;
String Data;
int servo = 5;
int Led1 = 6;
int Led2 = 7;
int Led3 = 8;
int Fan = 12;
int Alarm = 10;
int IRT = 9;
int sensorValue = 0;
void setup()
{
pinMode(irpin, INPUT);
pinMode(servo, OUTPUT);
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
pinMode(Led3, OUTPUT);
pinMode(Fan, OUTPUT);
pinMode(Alarm, OUTPUT);
pinMode(IRT, OUTPUT);
digitalWrite(IRT, HIGH);
Serial.begin(9600);
}
void loop()
{
//LDR
sensorValue = analogRead(A1);
// print out the value you read:
Serial.print("Ldr = ");
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if(sensorValue < 20)
{
digitalWrite(Led1, HIGH);
digitalWrite(Led2, HIGH);
digitalWrite(Led3, HIGH);
}
else
{
digitalWrite(Led1, LOW);
digitalWrite(Led2, LOW);
digitalWrite(Led3, LOW);
}
//Fan code
sensorValue = analogRead(A2);
// print out the value you read:
Serial.print("Fan = ");
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if(sensorValue > 490)
{
digitalWrite(Fan, HIGH);
}
else
{
digitalWrite(Fan, LOW);
}
//delay(2000);
//Alarm
sensorValue = analogRead(A0);
// print out the value you read:
Serial.print("Alarm = ");
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
if(sensorValue < 25)
{
digitalWrite(Alarm, HIGH);
}
else
{
digitalWrite(Alarm, LOW);
}
//Remote
duration = pulseIn(irpin,LOW);
//when the remote starts it sends a big LOW start bit
if(duration > 2000)
{
//we clear all data and counters
Data = "";
int i=1;
while (i < 32) //we count 32 bits of data
{
duration = pulseIn(irpin,LOW);
i = i + 1;
if (i > 16 && i <= 24) //we capture the 8 bits of data we want
{
//If light is on for more then 1000us its a 1
if(duration > 1000) Data = Data + 1;
//if light is on for less then 1000us its a 0
if(duration < 1000) Data = Data + 0;
}
}// All 32 bits have counted we now Print our Data
Serial.println(Data);
if(Data=="10101100")
{
for (int i = 0; i <= 25; i++) {
digitalWrite(servo, HIGH);
delay(2); // Wait for 1000 millisecond(s)
digitalWrite(servo, LOW);
delay(18); // Wait for 1000
}
Serial.println("Open Door");
//you can digitalWirite
}
if(Data=="11101100")
{
for (int i = 0; i <= 25; i++) {
digitalWrite(servo, HIGH);
delay(1); // Wait for 1000 millisecond(s)
digitalWrite(servo, LOW);
delay(19); // Wait for 1000
}
Serial.println("Close Door");
//you can digitalWirite
}
}
}