-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDumbWaiter
94 lines (72 loc) · 2.2 KB
/
DumbWaiter
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
//Dumbwaiter code
//adapted from MAKE; "HOT-COLD LED"
//Henry
// define pins
int SensorPinOne=13;
int SensorPinTwo=12;
int RedLedPin=6;
int YellowLedPin=5;
int GreenLedPin=3;
int pulseDuration;
int distance;
int distanceOne;
int distanceTwo;
unsigned long pulseDurationOne=0;
unsigned long pulseDurationTwo=0;
void setup() {
pinMode(RedLedPin,OUTPUT);
pinMode(GreenLedPin,OUTPUT);
pinMode(YellowLedPin,OUTPUT);
}
void loop() {
// measure distance: send "PingOne"
pinMode(SensorPinOne, OUTPUT);
digitalWrite(SensorPinOne, HIGH);
delayMicroseconds(5);
digitalWrite(SensorPinOne, LOW);
// measure distance: listen for "PingOne"
pinMode(SensorPinOne, INPUT);
pulseDurationOne=pulseIn(SensorPinOne, HIGH);
delay(10);
// measure distance: send "PingTwo"
pinMode(SensorPinTwo, OUTPUT);
digitalWrite(SensorPinTwo, HIGH);
delayMicroseconds(5);
digitalWrite(SensorPinTwo, LOW);
// measure distance: listen for "PingTwo"
pinMode(SensorPinTwo, INPUT);
pulseDurationTwo=pulseIn(SensorPinTwo, HIGH);
//Average of pulseDurationOne and pulseDurationTwo
pulseDuration=(pulseDurationOne+pulseDurationTwo)/2;
// divide by two (back/forth to single trip), divide by speed of sound = distance in in
pulseDurationOne=pulseDurationOne/2;
distanceOne = int(pulseDurationOne/74);
// divide by two (back/forth to single trip), divide by speed of sound = distance in in
pulseDurationTwo=pulseDurationTwo/2;
distanceTwo = int(pulseDurationTwo/74);
// distnce is lesser of both distnaces
distance = min(distanceOne,distanceTwo);
// light up red led: inverted linear of 0-15in to 0-255 eq. off to max. brightness
if (distance > 0 && distance < 9.5){
analogWrite(RedLedPin, 0);
}
else {
analogWrite(RedLedPin, 255);
}
// light up yellow led: 0-25in ^= 0-255, 25-50in ^= 255-0 on YellowLedPin
if (distance > 9.5 && distance <= 16.5){
analogWrite(YellowLedPin, 0);
}
else {
analogWrite(YellowLedPin, 255);
}
// light up green led: 0-25in ^= 0-255, 25-50in ^= 255-0 on greenLedPin
if (distance > 16.5 && distance <= 24){
analogWrite(GreenLedPin, 0);
}
else {
analogWrite(GreenLedPin, 255);
}
// wait a little...
delay(200);
}