/*
CS 983: Embedded, Cyber Physical Systems and IoT Security Assignment 1 (Task 1)
Group 8
Authors:
Sanket Deokule
Udayan Singh
Priyaranjan Kumar Jha
Veera Kumar
*/
#include <LiquidCrystal_I2C.h>
// Identifying the pins connections on Arduino Uno
int PIRLED = 13;
int PIRPIN = 4;
int PIRSTATE = LOW;
int REDPIN = 5;
int GREENPIN = 6;
int BLUEPIN = 7;
int ECHOPIN = 2;
int TRIGPIN = 3;
int DISTLED = 12;
// Initialization of default values
int val = 0;
LiquidCrystal_I2C LCD(0x27, 30, 4);
const float BETA = 3950; // To match the Beta Coefficient of the thermistor
// Function to clear LCD screen
void resetLCD() {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Sita's House"); // Constant LCD display
}
// Function to set the RGB LED colors
void setTempColor(int red, int green, int blue) {
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
}
// Setting up Arduino for execution
void setup() {
// Initialize the LCD screen
LCD.init();
LCD.backlight();
LCD.print("Sita's House"); // Constant LCD display
pinMode(PIRLED, OUTPUT); // Declare Blue LED for output
pinMode(PIRPIN, INPUT); // Declare sensor for input
pinMode(REDPIN, OUTPUT); // Declare Red pin of RGB LED for output
pinMode(GREENPIN, OUTPUT); // Declare Green pin of RGB LED for output
pinMode(BLUEPIN, OUTPUT); // Declare Blue pin of RGB LED for output
pinMode(TRIGPIN, OUTPUT); // Declare trigger pin of distance sensor as output
pinMode(ECHOPIN, INPUT); // Declare echo pin of distance sensor as input
pinMode(DISTLED, OUTPUT); // Declare Red LED for output
}
void loop() {
/* Temperature Sensor
The temperature sensor reads temperature vaue through analogRead A0 pin on Arduino.
Initially the temperature is converted into Celcius value using a default coefficient value of termister,
then its read constantly based on the thermister slider vlaue.
The temperature value in Celcius is constantly displayed on LCD screen 2nd row.
The RGB LED is programmed to change color based on below scenarios,
Temp below 10C -> Blue color
Temp between 11C and 34C -> Green color
Temp 35C and above -> Red color
*/
// Read temperature
int analogValue = analogRead(A0);
// Convert the temperature value in Celcuis
float celcius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Set color of RGB LED based on the temperature value
if (celcius >= 35.0) {
setTempColor(255, 0, 0);
}
else if (celcius > 10.0 && celcius < 35.0) {
setTempColor(0, 255, 0);
}
else if (celcius <= 10.0) {
setTempColor(0, 0, 255);
}
// Print temperature value on LCD screen
LCD.setCursor(0, 1);
LCD.print("Temp: ");
LCD.print(celcius);
LCD.print(" C ");
/* Motion Detector
The PIR Motion sensor is activated using digitalRead pin 4 (PIRPIN) on Arduino.
When the motion sensor is activated by simulating motion,
"Motion detected!" message is displayed on the LCD screen row 1.
Blue LED turns ON
The LED and LCD message stays ON for 5 seconds and ends with message "Motion ended!", and LED turns OFF
The detection is paused for 1.2 seconds before it starts reading again
*/
// Read for any motion sensed
val = digitalRead(PIRPIN);
// Turn ON/OFF the message on LCD screen and Blue LED
if (val == HIGH) {
digitalWrite(PIRLED, HIGH);
if (PIRSTATE == LOW) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Motion detected!");
PIRSTATE = HIGH;
}
}
else {
digitalWrite(PIRLED, LOW);
if (PIRSTATE == HIGH) {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Motion ended!");
delay(1200);
resetLCD();
PIRSTATE = LOW;
}
}
/* Distance Sensor
Initially, the Ultrasonic Distance sensor is reset to LOW and HIGH values.
This gets ready to read the ECHO pin (no. 2 on Arduino) by pulseIn in duration value.
The duration value is converted into distance value in CM which is constantly displayed on the LCD screen 3rd row.
When the distance is adjusted in the sensor slider,
The changed value gets displayed on the LCD screen 3rd row.
If the distance is < 30 CM, Red LED turns ON
If the distance is >= 30 CM, Red LED turns OFF
*/
long duration;
int distance;
// Send a pulse on the TRIGPIN
digitalWrite(TRIGPIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGPIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGPIN, LOW);
// Read the echo pin, duration is the time (in microseconds) for the echo to be received
duration = pulseIn(ECHOPIN, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Display distance on the LCD
LCD.setCursor(0, 2);
LCD.print("Distance: ");
LCD.print(distance);
LCD.print(" cm ");
// Control the DISTLED based on the distance
if (distance < 30) {
digitalWrite(DISTLED, HIGH);
} else {
digitalWrite(DISTLED, LOW);
}
}