#include <math.h>
#include <IRremote.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// Pin definitions
#define TRIG_PIN 3
#define ECHO_PIN 2
#define LED_BLUE_PIN 15
#define LED_RED_PIN 14
#define LED_GREEN_PIN 13
#define THERMISTOR_PIN 28
#define RGB_RED_PIN 8
#define RGB_GREEN_PIN 7
#define RGB_BLUE_PIN 6
#define IR_RECV_PIN 11
//#define LDR_DO_PIN -> Not used
#define LDR_AO_PIN 26
//#define DHT22_NC_PIN -> Not used
#define DHT22_SDA_PIN 22
/* Constants for the thermistor */
const float SERIES_RESISTOR = 10000.0; // Ohm
const float NOMINAL_RESISTANCE = 10000.0; // Resistance is at 25°C
const float NOMINAL_TEMPERATURE = 25; // Celsius
const float BETA_COEFFICIENT = 3950;
/* Constants for the photoresistor (LDR) */
const float GAMMA = 0.7;
const float RL10 = 50;
IRrecv receiver(IR_RECV_PIN); // Create IR receiver object
LiquidCrystal_I2C lcd(0x27, 20, 4); // Create 20x4 LCD (I2C) object with address 0x27
DHT dht22(DHT22_SDA_PIN, DHT22); // Create DHT22 object
void setup() {
/* Initialize GPIO pins */
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
//pinMode(LDR_DO_PIN, INPUT); -> Not used
pinMode(LDR_AO_PIN, INPUT);
receiver.enableIRIn(); // Initialize IR receiver
Serial1.begin(115200); // Initialize serial console
/* Initialize 20x4 LCD (I2C) */
lcd.init();
lcd.backlight();
dht22.begin(); // Initialize DHT22 Humidity and Temperature sensor
}
void loop() {
lcd.clear(); // Clear and refresh LCD screen
float tempC = readTemperature(); // Read temperature from thermistor
float tempF = (tempC * 9.0 / 5.0) + 32.0; // Convert Celsius to Fahrenheit
lcd.setCursor(0, 0); // Set beginning of the text at first column, first row on LCD
lcd.print("Temp: "); // Temperature
lcd.print(tempC, 1);
lcd.write(223); // ASCII value for degree symbol
lcd.print("C|");
lcd.print(tempF, 1);
lcd.write(223); // ASCII value for degree symbol
lcd.print("F");
/* If temperature is 40°C or higher, turn on the red LED */
if (tempC >= 40.0)
digitalWrite(LED_RED_PIN, HIGH);
else
digitalWrite(LED_RED_PIN, LOW);
float humidity = dht22.readHumidity(); // Read humidity value (percent) from DHT22 sensor
float temp2 = dht22.readTemperature(); // Read temperature value (celcius) from DHT22 sensor
lcd.setCursor(0, 1); // Set beginning of the text at first column, second row on LCD
lcd.print("Hmd: "); // Humidity
lcd.print(humidity, 1);
lcd.print("% - "); // Temperature 2
lcd.print(temp2, 1);
lcd.write(223); // ASCII value for degree symbol
lcd.print("C");
/* Read values from IR Receiver */
if (receiver.decode()) {
remote_RGB_LED();
receiver.resume(); // Read the next value
}
float distance = measureDistance(); // Measure distance from HC-SR04 ultrasonic distance sensor
lcd.setCursor(0, 2); // Set beginning of the text at first column, third row on LCD
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
/* If distance is lower than 40 cm, turn on the blue LED */
if (distance < 40.0)
digitalWrite(LED_BLUE_PIN, HIGH);
else
digitalWrite(LED_BLUE_PIN, LOW);
float brightness = measureBrightness();
lcd.setCursor(0, 3); // Set beginning of the text at first column, fourth row on LCD
lcd.print("Brightness: ");
lcd.print((int)brightness);
lcd.print(" lux");
/* If brightness is lower than 20 lux (if area is dark), turn on the green LED */
if (brightness < 20.0)
digitalWrite(LED_GREEN_PIN, HIGH);
else
digitalWrite(LED_GREEN_PIN, LOW);
delay(2000); // 2 second (2000 ms) delay
}
float measureDistance() { // for HC-SR04 ultrasonic distance sensor
/* Trigger a 10us pulse to the TRIG pin */
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
/* Wait for the echo pin to go HIGH and start time */
while (digitalRead(ECHO_PIN) == LOW);
int start_time = micros();
/* Wait for the echo pin to go LOW and stop time */
while (digitalRead(ECHO_PIN) == HIGH);
int end_time = micros();
/* Calculate the time difference and convert to distance (cm) */
float time_diff = (float)(end_time - start_time);
float distance = (time_diff * 0.0343) / 2; // Speed of sound is 0.0343 cm/us
return distance;
}
float readTemperature() { // for thermistor
int sensorValue = analogRead(THERMISTOR_PIN);
int resistorValue = 10000;
float resistance = resistorValue / ((1023.0 / sensorValue) - 1.0); // ADC is 10-bit
/* Steinhart-Hart equation */
float steinhart;
steinhart = resistance / 10000.0;
steinhart = log(steinhart);
steinhart /= 3950.0;
steinhart += 1.0 / (25.0 + 273.15);
steinhart = 1.0 / steinhart;
float tempC = steinhart - 273.15; // Kelvin to Celsius
return tempC;
}
void remote_RGB_LED() { // for IR receiver
lcd.setCursor(0, 3); // Set beginning of the text at first column, fourth row on LCD
switch (receiver.decodedIRData.command) {
case 104: // Button 0 -> Off
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 48: // Button 1 -> Red
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 24: // Button 2 -> Green
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 122: // Button 3 -> Blue
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 255);
break;
case 16: // Button 4 -> Cyan
analogWrite(RGB_RED_PIN, 0);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 255);
break;
case 56: // Button 5 -> Magenta
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 0);
analogWrite(RGB_BLUE_PIN, 255);
break;
case 90: // Button 6 -> Yellow
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 0);
break;
case 66: // Button 7 -> White
analogWrite(RGB_RED_PIN, 255);
analogWrite(RGB_GREEN_PIN, 255);
analogWrite(RGB_BLUE_PIN, 255);
break;
default:
Serial1.println("Other button pressed");
}
}
float measureBrightness() { // for photoresistor (LDR)
/* Convert value to lux */
int analogValue = analogRead(LDR_AO_PIN);
float voltage = analogValue / 1023. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
return lux;
}