Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
307CR-assignment2/Room temperature pervasive system.ino
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
155 lines (121 sloc)
3.01 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <LiquidCrystal.h> | |
#include <Servo.h> | |
// About LCD | |
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; | |
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); | |
int Temp_Sensor_Pin = 0; | |
float Tmp_C; | |
// create servo object to control a servo | |
Servo myservo; | |
// variable to store the servo position | |
int myServoPos = 0; | |
int Red_LED = 8; | |
int Green_LED = 9; | |
int Bule_LED = 7; | |
float getTemperatureValue(int tmpPin) | |
{ | |
//Get a data from temperature Sensor | |
float tmp = analogRead(tmpPin); | |
// Convert the data into voltage and convert voltage to temperature(Celsius) | |
float volt = tmp * (5000 / 1024.0); | |
float tmp_C = (volt - 500) / 10; | |
return tmp_C; | |
} | |
void setup() | |
{ | |
//Servo Motor | |
myservo.attach(10); | |
// LED | |
pinMode(Red_LED, OUTPUT); //Red | |
pinMode(Green_LED, OUTPUT); //Green | |
pinMode(Bule_LED, OUTPUT); //blue | |
// LCD | |
lcd.begin(16, 2); | |
//Serial Monitor | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
Tmp_C = getTemperatureValue(Temp_Sensor_Pin); | |
lcd.setCursor(0, 0); | |
lcd.print("Room Temperature: "); | |
lcd.setCursor(0, 1); | |
lcd.print(Tmp_C); | |
lcd.print(" 'C"); | |
Serial.print("Room Temperature : "); | |
Serial.print(Tmp_C); | |
Serial.println(" 'C"); | |
delay(1000); | |
lcd.clear(); | |
if(Tmp_C < 25.0f) | |
{ | |
lcd.setCursor(0, 0); | |
lcd.print("Room is cold : "); | |
lcd.setCursor(0, 1); | |
lcd.print(Tmp_C, 1); | |
lcd.print(" 'C"); | |
} | |
else if(Tmp_C > 27.0f) | |
{ | |
lcd.setCursor(0, 0); | |
lcd.print("Room is Hot : "); | |
lcd.setCursor(0, 1); | |
lcd.print(Tmp_C, 1); | |
lcd.print(" 'C"); | |
} | |
else | |
{ | |
lcd.setCursor(0, 0); | |
lcd.print("Room is Normal : "); | |
lcd.setCursor(0, 1); | |
lcd.print(Tmp_C, 1); | |
lcd.print(" 'C"); | |
} | |
// Room is cold | |
// If the Room is cold, we need to turn off the fan. | |
if( Tmp_C < 25.0f) | |
{ | |
digitalWrite(Bule_LED, HIGH); | |
delay(3000); // 1000ms=1 sec | |
digitalWrite(Bule_LED, LOW); | |
delay(200); | |
} | |
// Room is Hot | |
// If the Room's temperature is high, we need to turn the fan on high. To decrease the room's temperature. | |
else if(Tmp_C > 27.0f) | |
{ | |
digitalWrite(Red_LED, HIGH); | |
delay(3000); // 1000ms=1 sec | |
digitalWrite(Red_LED, LOW); | |
delay(200); | |
for (myServoPos = 0; myServoPos <= 180; myServoPos += 1) | |
{ | |
myservo.write(myServoPos); | |
delay(15); | |
} | |
for (myServoPos = 180; myServoPos >= 0; myServoPos -= 1) | |
{ | |
myservo.write(myServoPos); | |
delay(15); | |
} | |
} | |
else // normal | |
// If the Room's temperature is normal, we do not need to turn it on high. just turn it on mid. | |
{ | |
digitalWrite(Green_LED, HIGH); | |
delay(3000); | |
digitalWrite(Green_LED, LOW); | |
delay(200); | |
for (myServoPos = 0; myServoPos <= 60; myServoPos += 1) | |
{ | |
myservo.write(myServoPos); | |
delay(15); | |
} | |
for (myServoPos = 60; myServoPos >= 0; myServoPos -= 1) | |
{ | |
myservo.write(myServoPos); | |
delay(15); | |
} | |
} | |
lcd.clear(); | |
} | |