#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// OLED display
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET); // Set the resolution
// LDR Characteristics (Light)
const float GAMMA = 0.7;
const float RL10 = 50;
float lux; // the measured light in unit lux
// Temperature sensor characteristics
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
float temp_c; // the temperature measured in unit celsius
// IR remote control
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
IRrecv receiver(PIN_RECEIVER);
int cont=0; // Represent the control of user
// Ultrasonic sensor detecting distance
#define ECHO_PIN 3
#define TRIG_PIN 4
float dis_cm;
void setup() {
Serial.begin(9600);
receiver.enableIRIn(); // Start the receiver
pinMode(TRIG_PIN, OUTPUT); // pin 4 output
pinMode(ECHO_PIN, INPUT); // pin 3 input
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
}
void loop() {
get_light();
get_temp();
dis_cm=get_dis();
// Checks received an IR signal
if (receiver.decode()) {
translateIR();
receiver.resume(); // Receive the next value
}
// Get the relevant sensor data
if (cont==1 & lux>500 & temp_c>35 & dis_cm>5)
close();
else if (cont==1 & (lux<450 || temp_c<33) & dis_cm <50)
open();
else if (cont==2 & dis_cm<50)
open();
else if (cont==3 & dis_cm>5)
close();
}
// The function to get light
void get_light(){
int light_read = analogRead(A0);
float voltage = light_read / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
/* Print the light in lux
Serial.println("Light:");
Serial.println(lux);
Serial.println("lux"); */
delay(100);
}
// The function to get temperature
void get_temp(){
int temp_read = analogRead(A1);
temp_c = 1 / (log(1 / (1023. / temp_read - 1)) / BETA + 1.0 / 298.15) - 273.15;
/* Print the temperature in celsius
Serial.print("Temperature: ");
Serial.print(temp_c);
Serial.println(" ℃"); */
delay(100);
}
// The function to get the IR signal
void translateIR(){
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 48: // '1' was pressed
cont=1; // Represent the self-control method is on
break;
case 24: // '2' was pressed
cont=2; // Represent the user want to open the curtain
break;
case 122: // '3' was pressed
cont=3; // Represent the user want to close the curtain
break;
case 16: // '4' was pressed
cont=4; // Represent the user want to close the curtain
break;
}
}
// The function to get distance between the middle point and the curtain
float get_dis(){
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration = pulseIn(ECHO_PIN, HIGH);
return duration / 50 ;
delay (100);
}
// The function to open the curtain
void open(){
digitalWrite(12,1); // denote HIGH as open
digitalWrite(11,0);
delay(10);
digitalWrite(11,1);
delay(100);
digitalWrite(11,0);
delay(10);
}
// The function to close the curtain
void close(){
digitalWrite(12,0); // denote LOW as close
digitalWrite(11,0);
delay(10);
digitalWrite(11,1);
delay(100);
digitalWrite(11,0);
delay(10);
}
/*
// OLED initialize
void oled_on(){
display.clearDisplay(); // clears the screen and buffer
display.setTextSize(1); // set the font size
display.setTextColor(WHITE); // set the font color
display.setCursor(0,0); // set the start of the cursor
display.print("Temperature Outside: ");
display.print(temp_c);
display.println("°");
display.print("Light Outside: ");
display.print(lux);
display.println("Lux");
delay(2000);
}*/