-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduino code.txt
50 lines (38 loc) · 1 KB
/
Arduino code.txt
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
int echo=9;
int trig=8;
int val;
void setup() {
Serial.begin(9600);
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trig, HIGH);
delayMicroseconds(20);
digitalWrite(trig, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
int duration = pulseIn(echo, HIGH);
// Calculating the distance
int distance = duration * 0.034 / 2;
if (Serial.available() > 0) {
if(distance<=25 && distance >0)
{
String message = (String) "detected" +distance+ " centimetres ahead";
Serial.println(message);
}
delay(5000);
}
// switch (val) {
// case '1':
// String message =(String)"Distance =" +distance+ " front";
// Serial.println(message)1
// break;
// case '2':
// break;
// }
}