-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrc_tinkercad.ino
208 lines (176 loc) · 5.15 KB
/
src_tinkercad.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <Adafruit_LiquidCrystal.h>
const int LIGHT_RELAY_PIN = 12;
const int AL_LED = 2;
const int US_LED = 3;
const int PIR_LED = 4;
const int BUTTON_PIN = 5;
const int TEMP_PIN = A0;
const int PHOTORESISTOR_PIN = A1;
const int PIR_PIN = 8;
Adafruit_LiquidCrystal lcd_1(0);
int reading;
float celsius;
float relativeHumidity = 0;
float decibel = 0;
float cm;
boolean isPIRDetected;
boolean isLightOn = false;
int buttonPushCounter = 1; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
float readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(LIGHT_RELAY_PIN, OUTPUT);
pinMode(AL_LED, OUTPUT);
pinMode(US_LED, OUTPUT);
pinMode(PIR_LED, OUTPUT);
pinMode(TEMP_PIN, INPUT);
pinMode(PHOTORESISTOR_PIN, INPUT);
pinMode(PIR_PIN, INPUT);
// initialize the button pin as a input:
pinMode(BUTTON_PIN, INPUT);
// initialize serial communication:
Serial.begin(9600);
lcd_1.begin(16, 2); //sets up number of columns (16) and rows (2)
lcd_1.setBacklight(1);
}
void loop()
{
isPIRDetected = digitalRead(PIR_PIN);
sensorLED(PIR_LED, isPIRDetected);
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 7);
boolean isUltrasonicDetected = (cm <= 335);
sensorLED(US_LED, isUltrasonicDetected);
int light = analogRead(PHOTORESISTOR_PIN);
boolean isLightDetected = (light <= 512);
sensorLED(AL_LED, isLightDetected);
reading = analogRead(TEMP_PIN);
float volt = (reading/1023.0) * 5;
celsius = (volt - 0.5) * 100;
if (isPIRDetected && isUltrasonicDetected && isLightDetected) {
digitalWrite(LIGHT_RELAY_PIN, HIGH);
isLightOn = true;
} else if (isUltrasonicDetected && isLightOn) {
digitalWrite(LIGHT_RELAY_PIN, HIGH);
} else {
digitalWrite(LIGHT_RELAY_PIN, LOW);
isLightOn = false;
}
lcdButton();
//Serial.print(cm);
//Serial.println("cm");
//Serial.print(celsius);
//Serial.println("C");
//Serial.println(light);
//Serial.println("cd");
delay(38); // Wait for 100 millisecond(s)
}
void lcdButton() {
// read the pushbutton input pin:
buttonState = digitalRead(BUTTON_PIN);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
clearLCDFirstLine();
Serial.println("Button pushed");
switch (buttonPushCounter) {
case 1:
Serial.println("Temperature Mode");
break;
case 2:
Serial.println("Humidity Mode");
break;
case 3:
Serial.println("Loudness Mode");
break;
}
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("Button released");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// turns on the LED every four button pushes by checking the modulo of the
// button push counter. the modulo function gives you the remainder of the
// division of two numbers:
switch (buttonPushCounter) {
case 1:
lcdTemp();
break;
case 2:
lcdHumidity();
break;
case 3:
lcdLoudness();
// resets counter, starting new cycle
buttonPushCounter = 0;
break;
}
}
void sensorLED(int triggerPin, boolean value) {
if (value == true) {
digitalWrite(triggerPin, HIGH);
} else {
digitalWrite(triggerPin, LOW);
}
}
void clearLCDFirstLine() {
lcd_1.setCursor(0, 0);
lcd_1.print(" ");
}
void lcdTemp() {
lcd_1.setCursor(0, 0);
lcd_1.print("Temperature:");
lcd_1.setCursor(0, 1);
// Extra spaces prevent redundant " C"
// in case new value has more than 1 digit
// difference compared to previous value
// (example: -110.46 C -> -75.02 CC -> 1.24 CCCC)
lcd_1.setCursor(0, 1);
lcd_1.print(celsius);
lcd_1.print("C ");
}
void lcdHumidity() {
lcd_1.setCursor(0, 0);
lcd_1.print("Humidity:");
lcd_1.setCursor(0, 1);
// Extra spaces to prevent redundant %
// in case new value has more than 1 digit
// difference compared to previous value
// (example: 1%%)
lcd_1.setCursor(0, 1);
lcd_1.print(relativeHumidity);
lcd_1.print("% ");
}
void lcdLoudness() {
lcd_1.setCursor(0, 0);
lcd_1.print("Loudness:");
lcd_1.setCursor(0, 1);
// Extra spaces to prevent redundant db
// in case new value has more than 1 digit
// difference compared to previous value
// (example: 1dBB)
lcd_1.setCursor(0, 1);
lcd_1.print(decibel);
lcd_1.print(" dB ");
}