-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObjectUl.ino
80 lines (59 loc) · 1.65 KB
/
ObjectUl.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
78
79
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
int relay=13;
int buzzer=10;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
float distance=0.0;
int j=1;
int k=0;
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
// initialize the LCD
pinMode(relay, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
}
void loop() {
delay(60); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
distance = sonar.ping_cm();
// Send results to Serial Monitor
if (distance <= 50 && distance >= 2)
{
if(j==1)
{
Serial.println("Reset Relay object came");
blink();
digitalWrite(buzzer, HIGH);
delay(1000);
j=0;
k=1;
}
Serial.print("Object Found at");
Serial.print(distance);
Serial.println(" cm");
}
else {
if(k==1)
{
Serial.println("Reset Relay object gone");
blink();
digitalWrite(buzzer, LOW);
delay(1000);
j=1;
k=0;
}
// Serial.println(k);
Serial.print("Object NOT Found");
Serial.print(" Distance = ");
Serial.print(distance);
Serial.println(" cm");
}
} //end loop
void blink()
{
digitalWrite(relay, HIGH);
delay(100);
digitalWrite(relay, LOW);
delay(100);
}