Arduino SA
Search…
Upload the Code

Code upload

Open up your Arduino IDE.
Connect your USB cable to your PC and Arduino board.
Please ensure that the TX and RX for the Bluetooth module is not connected. If this is connected then the code upload will fail.
Copy the code below to a new Arduino project and Click the upload button
1
// Motor A (Right) connections
2
#define ENA 9 // Enable/speed motor Right wheels
3
#define IN_1 8 // Right wheels
4
#define IN_2 7 // Right wheels
5
6
// Motor B (Left) connections
7
#define ENB 3 // Enable/speed motor Left Right
8
#define IN_3 5 // Left wheels
9
#define IN_4 4 // Left wheels
10
11
int command; //Int to store app command state.
12
int speedCar = 255; // 50 - 255.
13
14
void setup() {
15
16
pinMode(ENA, OUTPUT);
17
pinMode(ENB, OUTPUT);
18
pinMode(IN_1, OUTPUT);
19
pinMode(IN_2, OUTPUT);
20
pinMode(IN_3, OUTPUT);
21
pinMode(IN_4, OUTPUT);
22
23
Serial.begin(9600);
24
25
}
26
27
void goAhead() {
28
29
digitalWrite(IN_1, HIGH);
30
digitalWrite(IN_2, LOW);
31
analogWrite(ENA, speedCar);
32
33
digitalWrite(IN_3, LOW);
34
digitalWrite(IN_4, HIGH);
35
analogWrite(ENB, speedCar);
36
}
37
38
void goBack() {
39
40
digitalWrite(IN_1, LOW);
41
digitalWrite(IN_2, HIGH);
42
analogWrite(ENA, speedCar);
43
44
45
digitalWrite(IN_3, HIGH);
46
digitalWrite(IN_4, LOW);
47
analogWrite(ENB, speedCar);
48
}
49
50
void goRight() {
51
52
digitalWrite(IN_1, HIGH);
53
digitalWrite(IN_2, LOW);
54
analogWrite(ENA, speedCar);
55
56
57
digitalWrite(IN_3, LOW);
58
digitalWrite(IN_4, LOW);
59
analogWrite(ENB, speedCar);
60
}
61
62
void goLeft() {
63
64
digitalWrite(IN_1, LOW);
65
digitalWrite(IN_2, LOW);
66
analogWrite(ENA, speedCar);
67
68
digitalWrite(IN_3, LOW);
69
digitalWrite(IN_4, HIGH);
70
analogWrite(ENB, speedCar);
71
72
}
73
74
void stopRobot() {
75
76
digitalWrite(IN_1, LOW);
77
digitalWrite(IN_2, LOW);
78
analogWrite(ENA, speedCar);
79
80
81
digitalWrite(IN_3, LOW);
82
digitalWrite(IN_4, LOW);
83
analogWrite(ENB, speedCar);
84
}
85
86
void loop() {
87
88
if (Serial.available() > 0) {
89
command = Serial.read();
90
stopRobot(); //Initialize with motors stopped.
91
92
switch (command) {
93
case 'F': goAhead(); break;
94
case 'B': goBack(); break;
95
case 'L': goLeft(); break;
96
case 'R': goRight(); break;
97
case '0': speedCar = 100; break;
98
case '1': speedCar = 115; break;
99
case '2': speedCar = 130; break;
100
case '3': speedCar = 145; break;
101
case '4': speedCar = 160; break;
102
case '5': speedCar = 175; break;
103
case '6': speedCar = 190; break;
104
case '7': speedCar = 205; break;
105
case '8': speedCar = 220; break;
106
case '9': speedCar = 235; break;
107
case 'q': speedCar = 255; break;
108
109
}
110
}
111
}
112
113
114
Copied!
arduino-bluetooth-car/bluetoothCarCode.ino at main · RoboFactorySA/arduino-bluetooth-car
GitHub
Last modified 3mo ago
Copy link