-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpperControlWithSensor+SmartLED+Lighting.ino
213 lines (186 loc) · 4.87 KB
/
UpperControlWithSensor+SmartLED+Lighting.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
209
210
211
212
213
// Include RadioHead Amplitude Shift Keying Library
// The transmitter pin will use pin 12
// The receiver pin will use pin 11
#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#include <Adafruit_NeoPixel.h>
#include <Ultrasonic.h>
#define PIN 8 // LED pin
#define NUM_LEDS 12 // LED number
#define SD_ChipSelectionPin 7 // define CS pin
Ultrasonic ultrasonic(A0,A1);
TMRpcm tmrpcm; //crete an object for speaker library
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// Setting all the pins
// smartlight uses pin 6;
// PIR uses pin 5
// Current Crosswalk Light uses pin 1
int smartLightPin = 8;
int remoteControlPin = 2;
int additionalLED=6;
int trafficTrafLightStatePin=4;
int pirPin = 5;
// Setting the variables
int pirValue = 0;
int pirResult = 0;
int crossSmartHigh = 0;
int delayTime = 5000;
int sensorResult=0;
unsigned long currenttime = 0;
unsigned long redlightTimer = 0;
int USresult=0;
int TrafLightState=0;
int lightvoltage;
int photovoltage;
int volume=4;
char counterTransmitter;
char counterSmartLight;
void setup() {
strip.begin();
strip.show();
strip.setBrightness(10);
tmrpcm.speakerPin = 9;
pinMode(smartLightPin,OUTPUT);
pinMode(trafficTrafLightStatePin,INPUT);//1
pinMode(additionalLED,OUTPUT);
pinMode(remoteControlPin,OUTPUT);
Serial.begin(9600);
currenttime = millis();
if(!SD.begin(SD_ChipSelectionPin))
{
return;
}
tmrpcm.setVolume(4);
tmrpcm.play("1.wav");
}
void loop() {
// To see if own side should turn on the light
lightvoltage=analogRead(A5);//trafficlight
if (lightvoltage>500)
{TrafLightState=1;}
else
{
TrafLightState=0;
redlightTimer=millis();//record the current time
}
pirResult = readPIR();
//pirResult=1;
USresult = readDIS();
sensorResult=pirResult*USresult;//yu
delay(100);//detect every 100 ms
photovoltage=analogRead(A4);//photo
Serial.print("Light Levvel: ");
Serial.println(photovoltage);
if (photovoltage>400)
{
volume=6;
}
else{
volume=2;
}
// Serial.println(lightvoltage);
// Serial.println("sensor");
// Serial.println(USresult);
// Serial.println("traffic");
// Serial.println(TrafLightState);
crossSmartHigh = sensorResult*TrafLightState; //
if ((millis()-redlightTimer < delayTime) && (TrafLightState*pirResult==1))//first 5 sec
{
Serial.println("Block detected from PIR in the first 5 seconds!");
digitalWrite(remoteControlPin,HIGH);
smartlightHigh();
if(volume==2){
digitalWrite(additionalLED,HIGH);
}
else{
digitalWrite(additionalLED,LOW);
}
tmrpcm.setVolume(volume);
tmrpcm.play("3.wav");
delay(2000);
currenttime = millis();
Serial.println(currenttime);
}
if ((millis()-redlightTimer >= delayTime) && (crossSmartHigh == 1))
//if (crossSmartHigh == 1)
{
Serial.println("Block detected!");
digitalWrite(remoteControlPin,HIGH);
smartlightHigh();
if(volume==2){
digitalWrite(additionalLED,HIGH);
}
else{
digitalWrite(additionalLED,LOW);
}
tmrpcm.setVolume(volume);
tmrpcm.play("2.wav");
delay(2000);
currenttime = millis();
Serial.println(currenttime);
}
if (crossSmartHigh == 0)
{
if ((millis()-currenttime < delayTime) && (TrafLightState=1))
{
Serial.println("Block not detected, light shut down in ");
Serial.println(millis()-currenttime);
Serial.println(currenttime);
smartlightHigh();
if(volume==2){
digitalWrite(additionalLED,HIGH);
}
else{
digitalWrite(additionalLED,LOW);
}
// tmrpcm.setVolume(volume);
// tmrpcm.play("2.wav");
// delay(2000);
}
else
{
//Serial.println("PIR not detected!");
digitalWrite(remoteControlPin,LOW);
smartlightLow();
digitalWrite(additionalLED,LOW);
}
}
}
void smartlightHigh(){
for (int i = 0;i<12; i++) {
strip.setPixelColor(i,0,100,100);
strip.show();
}
}
void smartlightLow() {
for (int i = 0; i<12;i++) {
strip.setPixelColor(i,0,0,0);
strip.show();
}
}
int readDIS(){
int detectresult = 0;
int distance;
distance = ultrasonic.read(); //Use 'CM' for centimeters or 'INC' for inches
Serial.print("Object found at: ");
Serial.print(distance);
Serial.println("cm");
if (distance<30 && distance>20){
detectresult=1;}
else{
detectresult=0;}
return detectresult;
}
int readPIR(){
pirValue = digitalRead(pirPin);
if(pirValue== HIGH){
pirResult = 1;
Serial.println("PIR detected!");
}
if(pirValue== LOW){
pirResult = 0;
Serial.println("PIR not detected!");
}
return pirResult;
}