-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparking.ino
56 lines (42 loc) · 1.06 KB
/
parking.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
#include <SR04.h>
#define MIN_DISTANCE 5
#define MAX_DISTANCE 50
#define BUZZER_PIN 10
#define ECHO_PIN 11
#define TRIG_PIN 12
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long distanceInCm;
void setup() {
// Provide an output channel for the serial data
Serial.begin(9600);
// Initialise the buzzer pin as an output
pinMode(BUZZER_PIN,OUTPUT);
// Wait 1 second
delay(1000);
}
void loop() {
unsigned char i;
// Record the measured distance
distanceInCm=sr04.Distance();
// Output the distance
Serial.println(distanceInCm);
// If an object is less than a metre and a half away
if((distanceInCm<=MAX_DISTANCE)&&(distanceInCm>MIN_DISTANCE)){
// Output a frequency
for(i=0;i<(distanceInCm);i++)
{
digitalWrite(BUZZER_PIN,HIGH);
delay(2);
digitalWrite(BUZZER_PIN,LOW);
delay(2);
}
} else if (distanceInCm<=MIN_DISTANCE){
while(sr04.Distance()<=MIN_DISTANCE){
// Beep solidly
digitalWrite(BUZZER_PIN,HIGH);
delay(2);
digitalWrite(BUZZER_PIN,LOW);
delay(2);
}
}
}