-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.cpp
151 lines (150 loc) · 3.14 KB
/
Code.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Left Motor
#define LM_En 3
#define LM_F 4
#define LM_B 10
int LSpeed;
// Right Motor
#define RM_En 5
#define RM_F 7
#define RM_B 6
int RSpeed;
int MotorSpeed = 200;
// IR Sensors
#define LeftIRSensor 11
#define RightIRSensor 12
#define midIRSensor 13
// UltraSonic Sensor
#define UltraSonicDelay 150
#define echoPin 8
#define trigPin 9
#define DistanceLimit 20
// Car Important Variables
#define TurningTime 1000
// Variables
bool RightIR_read, LeftIR_read, midIR_read;
float US_timer;
char direction;
float distance;
float duration;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
// Left Motor Initialization
pinMode(LM_En, OUTPUT);
pinMode(LM_F, OUTPUT);
pinMode(LM_B, OUTPUT);
// Right Motor Initialization
pinMode(RM_En, OUTPUT);
pinMode(RM_F, OUTPUT);
pinMode(RM_B, OUTPUT);
//IR sensors
pinMode(LeftIRSensor, INPUT);
pinMode(RightIRSensor, INPUT);
pinMode(midIRSensor, INPUT);
// Initialize going forward
direction = 'F';
move(direction);
// Timers to control sensors
US_timer = millis();
}
void loop() {
// CHECKING FOR OBSTACLE USING ULTRASONIC SENSOR
if (millis() - US_timer >= UltraSonicDelay) {
// Send sound waves
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Calculate the distance
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.035 / 2;
// Make a decision
if (distance < DistanceLimit) {
// Right
direction = 'R';
move(direction);
delay(TurningTime);
// Left
direction = 'L';
move(direction);
delay(TurningTime-300);
// Forward
direction = 'F';
move(direction);
delay(TurningTime - 100);
// Left
direction = 'L';
move(direction);
delay(TurningTime - 300);
// Forward
direction = 'F';
// while(true) {
// RightIR_read = digitalRead(RightIRSensor);
// LeftIR_read = digitalRead(LeftIRSensor);
// if (RightIR_read == HIGH || LeftIR_read == HIGH) {
// break;
// }
// }
}
// Print distance on Serial
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
US_timer = millis();
}
RightIR_read = digitalRead(RightIRSensor);
LeftIR_read = digitalRead(LeftIRSensor);
midIR_read = digitalRead(midIRSensor);
if ((RightIR_read == HIGH && LeftIR_read == HIGH) || (midIR_read == HIGH &&
LeftIR_read == LOW && RightIR_read == LOW)) {
direction = 'F';
}
else if (RightIR_read == HIGH && LeftIR_read == LOW) {
direction = 'L';
}
else if (RightIR_read == LOW && LeftIR_read == HIGH) {
direction = 'R';
}
move(direction);
}
// Car Functions
void move(char D) {
switch (D) {
case 'L':
digitalWrite(LM_F, HIGH);
digitalWrite(LM_B, LOW);
digitalWrite(RM_F, HIGH);
digitalWrite(RM_B, LOW);
RSpeed = MotorSpeed;
LSpeed = 50;
break;
case 'R':
digitalWrite(RM_F, HIGH);
digitalWrite(RM_B, LOW);
digitalWrite(LM_F, HIGH);
digitalWrite(LM_B, LOW);
RSpeed = 50;
LSpeed = MotorSpeed;
break;
case 'F':
digitalWrite(RM_F, HIGH);
digitalWrite(RM_B, LOW);
digitalWrite(LM_F, HIGH);
digitalWrite(LM_B, LOW);
RSpeed = MotorSpeed;
LSpeed = MotorSpeed;
break;
case 'S':
digitalWrite(RM_F, LOW);
digitalWrite(RM_B, LOW);
digitalWrite(LM_F, LOW);
digitalWrite(LM_B, LOW);
RSpeed = MotorSpeed;
LSpeed = MotorSpeed;
break;
}
analogWrite(RM_En, RSpeed);
analogWrite(LM_En, LSpeed);
}