-
Notifications
You must be signed in to change notification settings - Fork 0
/
AACRCC.ino
167 lines (149 loc) · 3.73 KB
/
AACRCC.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const int motor1Pin1 = 2;
const int motor1Pin2 = 3;
const int ENA=9;
const int motor2Pin1 = 4;
const int motor2Pin2 = 5;
const int ENB=10;
const int trigPin = 12;
const int echoPin = 11;
long duration;
int distance;
const int redbuzz=7;
const int green=6;
int ObstacleCheck(){
// Clears the trigPin
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);
// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
if(distance<10){
digitalWrite(redbuzz,HIGH);
delay(500);
digitalWrite(redbuzz,LOW);
return 1;} //obstacle present
else return 0;}
void Forward(){
digitalWrite(motor1Pin1,HIGH);
digitalWrite(motor1Pin2,LOW);
digitalWrite(motor2Pin1,HIGH);
digitalWrite(motor2Pin2,LOW);
analogWrite(ENA,255);
analogWrite(ENB,255);}
void Left(){
digitalWrite(motor1Pin1,HIGH);
digitalWrite(motor1Pin2,LOW);
digitalWrite(motor2Pin1,HIGH);
digitalWrite(motor2Pin2,LOW);
analogWrite(ENA,255);
analogWrite(ENB,0);}
void Right(){
digitalWrite(motor1Pin1,HIGH);
digitalWrite(motor1Pin2,LOW);
digitalWrite(motor2Pin1,HIGH);
digitalWrite(motor2Pin2,LOW);
analogWrite(ENA,0);
analogWrite(ENB,255);}
void Backward(){
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2,HIGH);
digitalWrite(motor2Pin1,LOW);
digitalWrite(motor2Pin2,HIGH);
analogWrite(ENA,255);
analogWrite(ENB,255);}
void FL() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(ENA, 127);
analogWrite(ENB, 255);}
void FR() {
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor2Pin1, HIGH);
digitalWrite(motor2Pin2, LOW);
analogWrite(ENA, 255);
analogWrite(ENB, 127);}
void BL() {
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2,HIGH);
digitalWrite(motor2Pin1,LOW);
digitalWrite(motor2Pin2,HIGH);
analogWrite(ENA, 150);
analogWrite(ENB, 255);}
void BR() {
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2,HIGH);
digitalWrite(motor2Pin1,LOW);
digitalWrite(motor2Pin2,HIGH);
analogWrite(ENA, 255);
analogWrite(ENB, 150);}
void Stop(){
digitalWrite(motor1Pin1,LOW);
digitalWrite(motor1Pin2,HIGH);
digitalWrite(motor2Pin1,LOW);
digitalWrite(motor2Pin2,HIGH);
analogWrite(ENA,0);
analogWrite(ENB,0);;}
void setup(){
// put your setup code here, to run once:
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin1, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(redbuzz,OUTPUT);
pinMode(green,OUTPUT);
Serial.begin(9600); //bluetooth
}
void loop() {
// put your main code here, to run repeatedly:
int v = ObstacleCheck();
if (v == 0) {
while (Serial.available() > 0) {
char State = Serial.read();
Serial.println(State);
switch (State) {
case 'F':
Forward();
break;
case 'B':
Backward();
break;
case 'L':
Left();
break;
case 'R':
Right();
break;
case 'I':
FL();
break;
case 'G':
FR();
break;
case 'J':
BL();
break;
case 'H':
BR();
break;
default:
Stop();
}
}
} else {
Stop();
}
delay(500);
}