-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHC-SR04_Multiple.ino
77 lines (64 loc) · 1.93 KB
/
HC-SR04_Multiple.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
#include <HCSR04.h>
#define buzzer 33
#define ON LOW
#define OFF HIGH
unsigned int buzzerOnPeriod = 100; //milliseconds
unsigned int buzzerOffPeriodMax = 1000; //milliseconds
unsigned int buzzerOffPeriodMin = 50; //milliseconds
unsigned long now = 0;
unsigned long lastOffTime = 0;
unsigned long nextOnTime = 0;
unsigned long nextOffTime = 0;
unsigned int maxDetectionRange = 250; //centimeters
unsigned int minDetectionRange = 5; //centimeters
int notAvailable = -1;
byte triggerPin = 13;
byte echoCount = 2;
byte* echoPins = new byte[echoCount] { 12, 14 };
void setup () {
Serial.begin(9600);
pinMode(33, OUTPUT);
digitalWrite(buzzer, OFF);
HCSR04.begin(triggerPin, echoPins, echoCount);
}
void loop () {
//Reading distances
double* distances = HCSR04.measureDistanceCm();
Serial.print(distances[0]);
Serial.print(" ");
Serial.println(distances[1]);
//Select smaller distances to use
unsigned int tmpDis = (distances[0] < distances[1]) ? distances[0] : distances[1];
//If distances not in detection range
if (tmpDis == notAvailable || tmpDis < minDetectionRange || tmpDis > maxDetectionRange)
{
Serial.println("Not in detection, turn off buzzer");
digitalWrite(buzzer, OFF);
}
//If distances in detection range
else
{
//Mapping current distances to OFF period.
//The bigger OFF period then buzzer beeps with low frequency.
//The smaller OFF period then buzzer beeps with high frequency.
//if ( tmpDis > 50 && tmpDis < 200 )
// OnOffBlink(2000, buzzerOnPeriod);
if ( tmpDis < 50 )
OnOffBlink(50, buzzerOnPeriod);
else
digitalWrite(buzzer,OFF);
}
}
void OnOffBlink(int tOn, int tOff) {
static int timer = tOn;
static long previousMillis;
if ((millis() - previousMillis) >= timer) {
if (digitalRead(buzzer) == HIGH) {
timer = tOff;
} else {
timer = tOn;
}
digitalWrite(buzzer, !digitalRead(buzzer));
previousMillis = millis();
}
}