-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththrottle_adjustment_using_ultrasonic.ino
95 lines (62 loc) · 1.55 KB
/
throttle_adjustment_using_ultrasonic.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <Servo.h>
#define trigPin 13
#define echoPin 12
Servo Throttle;
int pin_in = 7;
int pin_out = 6;
unsigned long rc_throttle;
void setup()
{
Throttle.attach(pin_out);
pinMode(pin_in,INPUT);
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
/*
CODE FOR RECEIVING RC_THROTTLE
rc_throttle = pulseIn(pin_in, HIGH);
Serial.print("old - ");
Serial.println(rc_throttle);
*/
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.print("Out of range but distance is: ");
Serial.println(distance);
rc_throttle = 0;
}
if (distance >=1 && distance <=50){
Serial.print(distance);
Serial.println(" cm");
rc_throttle = 1900;
}
if (distance >= 51 && distance <=100){
Serial.print(distance);
Serial.println(" cm");
rc_throttle = 1700;
}
if (distance >= 101 && distance <=150){
Serial.print(distance);
Serial.println(" cm");
rc_throttle = 1500;
}
if (distance >=151 && distance <=199){
Serial.print(distance);
Serial.println(" cm");
rc_throttle = 1200;
}
delay(250);
Throttle.writeMicroseconds(rc_throttle);
Serial.print("throttle value- ");
Serial.println(rc_throttle);
}