-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar_control.ino
81 lines (71 loc) · 2.04 KB
/
car_control.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
#include <AFMotor.h> // Library for Bluetooth communication
#include <SoftwareSerial.h> // Library for L293d Arduino Motor Driver
int incomingByte = 0; // Data received from Bluetooth
// Define motors and their speeds here. Motors can operate in the range of 0–255 speed.
AF_DCMotor motor1(1, 255);
AF_DCMotor motor2(2, 255);
AF_DCMotor motor3(3, 255);
AF_DCMotor motor4(4, 255);
void setup() {
Serial.begin(9600); // Initialize Serial Monitor
Serial.println("WAY!"); // Display "WAY!" on Serial Monitor at the beginning. You can modify this message or leave it empty.
}
void loop() {
// Define speeds again individually.
motor1.setSpeed(255);
motor2.setSpeed(255);
motor3.setSpeed(255);
motor4.setSpeed(255);
// Read data if available from Serial (greater than 0).
if (Serial.available() > 0) {
incomingByte = Serial.read();
}
// Implementing functions based on received data using Switch-Case.
switch (incomingByte) {
case 'S':
// All motors stop.
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
Serial.println("STOP\n");
incomingByte = '*';
break;
case 'F':
// Motors move forward.
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
Serial.println("FORWARD\n");
incomingByte = '*';
break;
case 'B':
// Motors move backward.
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
Serial.println("BACKWARD\n");
incomingByte = '*';
break;
case 'R':
// Motors turn right.
motor1.run(FORWARD);
motor2.run(RELEASE);
motor3.run(FORWARD);
motor4.run(FORWARD);
Serial.println("RIGHT\n");
incomingByte = '*';
break;
case 'L':
// Motors turn left.
motor1.run(RELEASE);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
Serial.println("LEFT\n");
incomingByte = '*';
break;
}
}