-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallax_ping_sonar_avg.ino
46 lines (43 loc) · 1.29 KB
/
parallax_ping_sonar_avg.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
const int pingPin = 11;
unsigned int duration, inches;
unsigned int measure1, measure2, measure3;
unsigned int distanceReading;
unsigned int counter;
const int threshHold = 67;
const int outputPin = 8;
void setup() {
Serial.begin(9600);
pinMode(outputPin, OUTPUT);
counter = 0;
}
void loop() {
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 microsecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
distanceReading = duration / 74 / 2; // Convert to inches
if (counter == 0) {
measure1 = inches;
measure2 = inches;
measure3 = inches;
inches = distanceReading;
} else {
if (abs(distanceReading - inches) < 4) {
measure3 = measure2;
measure2 = measure1;
measure1 = inches;
inches = distanceReading;
}
}
Serial.println(inches); // Display result
if(inches <= threshHold){
digitalWrite(outputPin,HIGH);
}else{
digitalWrite(outputPin,LOW);
}
delay(100); // Short delay
}