-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistance Sensor Arduino.ino
More file actions
94 lines (68 loc) · 2.11 KB
/
Copy pathDistance Sensor Arduino.ino
File metadata and controls
94 lines (68 loc) · 2.11 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
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
const int TRIG_PIN = 13;
const int ECHO_PIN = 12;
const int buzzerPin = 5;
const int redLED = 2;
const int yellowLED = 7;
const int blueLED = 4;
const unsigned int MAX_DIST = 23200;
const int minSoundDelay = 50; //how fast at 0
const int maxSoundDelay = 2000; //how fast at 400cm
long timeOfLastSound = 0;
const int redLine = 100; //distance in cm to turn on a certain led
const int yellowLine = 200;
void setup() {
pinMode(redLED, OUTPUT);
pinMode(yellowLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
digitalWrite(TRIG_PIN, LOW);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
unsigned long t1;
unsigned long t2;
unsigned long pulse_width;
float cm;
float inches;
digitalWrite(TRIG_PIN, HIGH); //takemeasurement
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
while ( digitalRead(ECHO_PIN) == 0 );
t1 = micros();
while ( digitalRead(ECHO_PIN) == 1);
t2 = micros();
pulse_width = t2 - t1;
cm = pulse_width / 58.0;
inches = pulse_width / 148.0;
if ( pulse_width > MAX_DIST ) {
Serial.println("Out of range");
}
else {
if (cm <= redLine) {
digitalWrite(blueLED, LOW);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, HIGH);
}
if (cm > redLine &&cm <= yellowLine) {
digitalWrite(blueLED, LOW);
digitalWrite(yellowLED, HIGH);
digitalWrite(redLED, LOW);
}
if (cm > yellowLine) {
digitalWrite(blueLED, HIGH);
digitalWrite(yellowLED, LOW);
digitalWrite(redLED, LOW);
}
Serial.print(cm);
Serial.print(" cm \t");
Serial.print(inches);
Serial.println(" in");
long soundDelay = map(cm, 0, 400, minSoundDelay, maxSoundDelay); //turns beep on based on how far away how long beep delay
if (millis() > timeOfLastSound + soundDelay) { //takes delay and if long enough plays sound
tone(buzzerPin, 1000, 50);
timeOfLastSound = millis();
}
}
delay(50);
}