-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.ino
185 lines (167 loc) · 3.74 KB
/
robot.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <SoftwareSerial.h>
SoftwareSerial BTserial(0, 1); // RX | TX
// LED PINS
int ledRight = 10;
int ledLeft = 13;
// MOTOR1 PINS
int ena = 9;
int in1 = 8;
int in2 = 7;
int in3 = 6;
int in4 = 5;
int enb = 3;
void forward() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, 200);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enb, 200);
}
void right() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, 0);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enb, 200);
}
void left() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, 200);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enb, 0);
}
void back() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, 200);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enb, 200);
}
void FR() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, 110);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enb, 190);
}
void FL() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(ena, 190);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enb, 105);
}
void BR() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, 190);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enb, 110);
}
void BL() {
//MOTOR_A CLOCKWISE MAX SPEED
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(ena, 105);
//MOTOR_B CLOCKWISE MAX SPEED
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
analogWrite(enb, 200);
}
void ledoff() {
digitalWrite(ledRight, LOW);
digitalWrite(ledLeft, LOW);
}
void ledon() {
digitalWrite(ledRight, HIGH);
digitalWrite(ledLeft, HIGH);
}
void Ron() {
digitalWrite(ledRight, HIGH);
digitalWrite(ledLeft, LOW);
}
void Lon(){
digitalWrite(ledRight, LOW);
digitalWrite(ledLeft, HIGH);
}
void setup() {
// setup serial communication with the HC-05 module
BTserial.begin(9600);
// setup pins for the motor driver
pinMode(ena, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(enb, OUTPUT);
// setup pins for the LEDs
pinMode(ledRight, OUTPUT);
pinMode(ledLeft, OUTPUT);
// set initial state for the motors and LEDs
digitalWrite(ena, LOW);
digitalWrite(enb, LOW);
digitalWrite(ledRight, LOW);
digitalWrite(ledLeft, LOW);
}
void loop() {
// check if there is data available to read
if (BTserial.available()) {
// read the incoming data
char command = BTserial.read();
// check the command and control the robot accordingly
if (command == 'F') {
// move forward
forward();
} else if (command == 'B') {
// move backward
back();
ledon();
} else if (command == 'L') {
// turn left
left();
Lon();
} else if (command == 'R') {
// turn right
right();
Ron();
} else if (command == 'G') {
FL();
Lon();
} else if (command == 'I') {
FR();
Ron();
} else if (command == 'J') {
BL();
Lon();
} else if (command == 'H') {
BR();
Ron();
} else if (command == 'S') {
// stop
digitalWrite(ena, LOW);
digitalWrite(enb, LOW);
}
}
}