-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmart-clock-thermometer-hygrometer.ino
165 lines (156 loc) · 5.01 KB
/
Smart-clock-thermometer-hygrometer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <IRremote.h>
#include <dht.h> //temperature measure
#include <LiquidCrystal.h>
#include <Wire.h> //for RTC
#include <RTClib.h> //TimeClock
#include <SevSeg.h>
const int RECV_PIN = A4;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long key_value = 0;
dht DHT;
#define DHT11_PIN A2
LiquidCrystal lcd(12, 11,6 , 5, 4, 3);
int backLight = A3;
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
SevSeg sevseg;
unsigned long current;
unsigned long current_IR;
int soundThreshold = 529; //customize this number depending on how loud the sound would be to activate the displays
int intervalClap = 500; //customize this number depending on how long between 2 claps would be to activate the display
void setup(){
Serial.begin(9600);
pinMode(A0, INPUT); // sound sensor
pinMode(A1, INPUT); //motion sensor
Wire.begin();
rtc.begin();
DateTime now = rtc.now();
/*Adjust the value 6 below if needed.
It is the number of second your computer need from the moment you click compile & upload or upload to the moment your Arduino starts working.
Once you uploaded the code successfully the first time, comment the 3 below lines of code by adding // at the beginning of each line to disable them then upload the program the second time*/
rtc.adjust(DateTime(__DATE__, __TIME__));
DateTime t = DateTime(rtc.now().unixtime()+6);
rtc.adjust(t);
}
void Time(unsigned long current){
DateTime now = rtc.now();
lcd.setCursor(1,0);
lcd.print(daysOfTheWeek[now.dayOfTheWeek()]);
lcd.print(",");
if (now.day() < 10 ) {
lcd.print("0");
}
lcd.print(now.day(), DEC);
lcd.print("/");
if (now.month() < 10 ) {
lcd.print("0");
}
lcd.print(now.month(), DEC);
lcd.print("/");
lcd.print(now.year(), DEC);
lcd.setCursor(4,1);
if (now.hour() <10){
lcd.print("0");
}
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second() < 10 ) {
lcd.print("0");
}
lcd.print(now.second(), DEC);
if ((unsigned long) (millis() - current) >= 10000){
digitalWrite(backLight, LOW);
}
delay(1000); //setting ban ngay less sound sensitive ban dem more sound sensitive
}
void TempHumid(unsigned long current){
int chk = DHT.read11(DHT11_PIN);
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(DHT.temperature);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(DHT.humidity);
lcd.print("%");
if ((unsigned long) (millis() - current) >= 10000){
digitalWrite(backLight, LOW);
}
delay(2000);
}
void loop()
{
Serial.println(analogRead(A0));
if (digitalRead(A1) ==LOW){
int clap =0;
if (analogRead(A0) <= soundThreshold + 1) {
digitalWrite(backLight, LOW);
byte numDigits = 4;
sevseg.Begin(COMMON_CATHODE, numDigits, 2, 8, 9, 10, 11, 6, 5, 4, 3, 12, 7, 13);
sevseg.SetBrightness(100);
char timeString[0];
DateTime now = rtc.now();
int hour_minute = now.hour()*100+now.minute();
sprintf(timeString, "%4d", hour_minute);
sevseg.DisplayString(timeString, 2);
delay(5);
} else if (analogRead(A0) > soundThreshold) {
lcd.begin(16, 2);
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
int volume = analogRead(A0);
Serial.println(volume, DEC);
current = millis();
digitalWrite(backLight, HIGH);
lcd.clear();
delay(200);
while ((unsigned long) (millis() - current ) <=intervalClap ) {
if (analogRead(A0) > soundThreshold + 1 ){
clap =1;
}
}
if (clap ==0) { //clap once
for (int i =0; i<10; i++ ) {
lcd.clear();
Time(current);
}
} else { //clap twice
for (int i=0; i<5; i++) {
lcd.clear();
TempHumid(current);
}
}
digitalWrite(backLight, LOW);
}
} else {
lcd.begin(16, 2);
pinMode(backLight, OUTPUT);
digitalWrite(backLight, HIGH);
current = millis();
for (int i =0; i<6; i++ ) {
lcd.clear();
Time(current);
}
for (int i=0; i<2; i++ ) {
lcd.clear();
TempHumid(current);
}
digitalWrite(backLight,LOW);
byte numDigits = 4;
sevseg.Begin(COMMON_CATHODE, numDigits, 2, 8, 9, 10, 11, 6, 5, 4, 3, 12, 7, 13);
sevseg.SetBrightness(100);
char timeString[0];
DateTime now = rtc.now();
int hour_minute = now.hour()*100+now.minute();
sprintf(timeString, "%4d", hour_minute);
sevseg.DisplayString(timeString, 2);
delay(5);
}
}