1#include <LiquidCrystal.h>
2# define temp_sensor_pin A0
3# define ref_pin A1
4#define fan_op_pin 9
5#define red_led 6
6#define green_led 7
7#define blue_led 8
8#define sound_pin 10
9float temperature;
10int fan_speed = 50;
11
12LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
13
14void setup()
15{
16 Serial.begin(9600);
17 lcd.begin(16, 4);
18 lcd.clear();
19 lcd.print("set temp:");
20 lcd.setCursor(0,1);
21 lcd.print("cur temp:");
22 pinMode(red_led,OUTPUT);
23 pinMode(green_led,OUTPUT);
24 pinMode(blue_led,OUTPUT);
25 analogWrite(fan_op_pin, fan_speed);
26}
27
28void loop()
29 {
30 int sensor_value,ref_value,max_temp_limit,min_temp_limit;
31 sensor_value = analogRead(temp_sensor_pin);
32 ref_value = analogRead(ref_pin);
33 ref_value = map(ref_value, 0, 1023, 0, 100);
34 max_temp_limit = ref_value+5;
35 min_temp_limit = ref_value-5;
36 temperature = (5*sensor_value)/10.31;
37 Serial.print("set temperature is ");
38 Serial.println(ref_value);
39 Serial.print("current temperature is ");
40 Serial.println(temperature);
41 lcd.setCursor(9,0);
42 lcd.print(ref_value);
43 lcd.print("*C");
44 lcd.setCursor(9,1);
45 lcd.print(temperature);
46 lcd.print("*C");
47 if(temperature > max_temp_limit)
48 {
49 lcd.setCursor(0,2);
50 lcd.print("over temperature");
51 lcd.setCursor(0,3);
52 lcd.print("fan speed incr");
53 if(fan_speed<150) fan_speed += 20;
54 analogWrite(fan_op_pin, fan_speed);
55 digitalWrite(red_led,HIGH);
56 digitalWrite(green_led,LOW);
57 digitalWrite(blue_led,LOW);
58 tone(sound_pin,1245,1000);
59 }
60 else if(temperature < min_temp_limit)
61 {
62 lcd.setCursor(0,2);
63 lcd.print("temperature down");
64 lcd.setCursor(0,3);
65 lcd.print("fan speed decr");
66 if(fan_speed > 50) fan_speed-=20;
67 analogWrite(fan_op_pin, fan_speed);
68 digitalWrite(red_led,LOW);
69 digitalWrite(green_led,LOW);
70 digitalWrite(blue_led,HIGH);
71 tone(sound_pin,1245,1000);
72 }
73 else if((temperature<max_temp_limit) && (temperature>min_temp_limit))
74 {
75 lcd.setCursor(0,2);
76 lcd.print("temperature is ");
77 lcd.setCursor(0,3);
78 lcd.print("within limit ");
79 digitalWrite(red_led,LOW);
80 digitalWrite(green_led,HIGH);
81 digitalWrite(blue_led,LOW);
82 }
83 delay(1000);
84}
85