-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTunnel_LED_Single_Unit.ino
More file actions
44 lines (40 loc) · 1.06 KB
/
Copy pathTunnel_LED_Single_Unit.ino
File metadata and controls
44 lines (40 loc) · 1.06 KB
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
const int triggerPin = 8;
const int echoPin = 9;
long time, distance;
int maxDistance = 400;
const int ledPin = 11;
void setup() {
Serial.begin(9600);
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(ledPin,OUTPUT);
}
void loop() {
digitalWrite(triggerPin,LOW);
delayMicroseconds(2);
digitalWrite(triggerPin,HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
time = pulseIn(echoPin,HIGH);
distance = time/58.3;
if (distance <= maxDistance){
Serial.println(distance);
}
if (distance < 40){
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}
if (distance > 40){
digitalWrite(ledPin,LOW);
}
}