-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmartCity.ino
162 lines (115 loc) · 3.13 KB
/
SmartCity.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
// C++ code
//
//#################### GAS SENSOR VARIABLES #############
int gasSensorPin = A1;
int gasPiezo = 8;
int GREEN_LED = 4;
int BLUE_LED = 5;
int YELLOW_LED = 6;
int RED_LED = 7;
int temperaturePin = A0;
//#################### STATS VARIABLES #############
//cars
long passedCars = 0;
long lastLog = millis();
long LOG_INTERVAL = 2000;
long hourMillis = 3600000;
long lastHourMillis = 0;
long lastHourPassedCars = 0;
long maximumCarsPerHour = 5;
//stoplight
int redLight = 10;
int greenLight = 9;
void setup(){
Serial.begin(9600);
lastHourMillis = millis();
initLeds(); //init stoplightLeds
}
void loop()
{
checkGas();
checkCarPass();
Serial.println(getTemperature());
delay(250);
}
void checkGas(){
int value = getAirQuality();
if(value > 80){
tone(gasPiezo, 460); // play tone 59 (B4 = 494 Hz)
delay(1000);
}
noTone(gasPiezo);
digitalWrite(GREEN_LED, HIGH);
digitalWrite(BLUE_LED, value >= 30 ? HIGH : LOW);
digitalWrite(YELLOW_LED, value >= 50 ? HIGH : LOW);
digitalWrite(RED_LED, value >= 80 ? HIGH : LOW);
}
void checkCarPass(){
int carSensor = analogRead(A2);
//Serial.print("Sensor: ");
//Serial.println(sensor);
/** if the value is greater than 0
it means that a car has passed.
*/
if(carSensor > 0){
if(millis() - lastLog > LOG_INTERVAL){
if(millis() - lastHourMillis < hourMillis){
lastHourPassedCars +=1; //adding a car
if(lastHourPassedCars > maximumCarsPerHour){
//block the lane
digitalWrite(redLight, HIGH);
digitalWrite(greenLight, LOW);
}else{
initLeds();
}
}else{
lastHourMillis = millis();
lastHourPassedCars = 0;
initLeds();
}
lastLog = millis();
passedCars +=1;
writeLog("A car passed");
}
}
}
//########################## UTILS #########################
/**
* This method should write to an SD card,
* but TinkerCad does not have such module.
* Instead i just print to screen the log.
*/
String writeLog(String action){
Serial.print("{");
//the timestamp of the event
Serial.print("\"Timestamp\" : \"");
Serial.print(millis());
//the action that triggered the log
Serial.print("\"; \"Action\" : \"");
Serial.print(action);
//the amount of cars that passed till this moment
Serial.print("\"; \"passedCars\" : \"");
Serial.print(passedCars);
//the amount of cars that passed in the last hour
Serial.print("\"; \"passedCarsLastHour\" : \"");
Serial.print(lastHourPassedCars);
//the air quality at this point in time
Serial.print("\"; \"airQuality\" : \"");
Serial.print(getAirQuality());
Serial.print("%");
//the air temperature
Serial.print("\"; \"airTemperature\" : \"");
Serial.print(getTemperature());
Serial.println("\"}");
}
int getAirQuality(){
int gas = analogRead(gasSensorPin);
return map(gas, 300, 750, 0, 100);
}
int getTemperature(){
return map(((analogRead(temperaturePin) - 20) * 3.04), 0, 1023, -40, 125);
}
void initLeds(){
digitalWrite(redLight, LOW); //stoplight
digitalWrite(greenLight, HIGH); //stoplight
}