-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.c
66 lines (57 loc) · 1.47 KB
/
Main.c
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
const int trigPin = 12;
const int echoPin = 14;
const int relay = 5;
//define sound velocity in cm/uS
#define SOUND_VELOCITY 0.034
#define CM_TO_INCH 0.393701
long duration;
float distanceCm;
float distanceInch;
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(relay, OUTPUT);
}
void loop() {
// Clears the trigPin
float dis=0;
for(int i=1;i<=5;i++)
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_VELOCITY/2;
dis+=distanceCm;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
// Prints the distance on the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
delay(100);
}
dis/=5;
Serial.print("Average Distance (cm) : ");
Serial.println(dis);
if(dis>=6)
{
digitalWrite(relay, LOW);
Serial.println("Current Flowing");
delay(5000);
}
if(dis<4)
{
digitalWrite(relay, HIGH);
Serial.println("Current not Flowing");
delay(5000);
}
delay(1000);
}