-
Notifications
You must be signed in to change notification settings - Fork 0
/
arduino_bluetooth_car.ino
193 lines (180 loc) · 6.17 KB
/
arduino_bluetooth_car.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
186
187
188
189
190
191
192
193
/*
* Arduino Bluetooth Car
* Author: Top Hot Apps
* Website: https://hotapps.top
* Email: [email protected]
* This sketch runs with Arduino Remote LITE. Download: https://play.google.com/store/apps/details?id=it.hieund.arduino_remote_lite
*/
// Commands
const char CMD_STOP = '0';
const char CMD_FORWARD = 'w';
const char CMD_BACKWARD = 's';
const char CMD_FORWARD_LEFT = 'a';
const char CMD_FORWARD_RIGHT = 'd';
const char CMD_BACKWARD_LEFT = '[';
const char CMD_BACKWARD_RIGHT = 'o';
const char CMD_TURN_ARROUND = 'x';
const char CMD_LIGHT_ON = 'v';
const char CMD_LIGHT_OFF = 'b';
const char CMD_HEAD_LIGHT_ON = '1';
const char CMD_HEAD_LIGHT_OFF = '2';
const char CMD_BACK_LIGHT_ON = '3';
const char CMD_BACK_LIGHT_OFF = '4';
const char CMD_ALL_LIGHTS_OFF = '5';
const char CMD_HORN_ON = '^';
const char CMD_HORN_OFF = '.';
// Constants
const char RELAY_ON = 1; // Relay on
const char RELAY_OFF = 0; // Relay off
const byte MAX_POWER = 255;
const byte MOTOR_POWER = 200; // From 0 to 255
const byte TURN_POWER = 35; // From 0 to 255
// Ports
const char PORT_LIGHT = 2; // Blue
const char PORT_HL_LIGHT = 3; // Yellow
const char PORT_HR_LIGHT = 4; // Orange
const char PORT_MOTOR_A_1 = 5; // Black
const char PORT_MOTOR_A_2 = 6; // White
const char PORT_BL_LIGHT = 7; // Broun
const char PORT_BR_LIGHT = 8; // Red
const char PORT_MOTOR_B_1 = 9; // Purple
const char PORT_MOTOR_B_2 = 10; // Gray
const char PORT_HORN = 11; // Green
char cmd = '_';
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
digitalWrite(PORT_LIGHT, RELAY_OFF);
digitalWrite(PORT_HORN, RELAY_OFF);
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_OFF);
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_OFF);
}
void loop() {
if (Serial.available()) {
cmd = Serial.read();
Serial.println("Command: " + cmd);
switch (cmd) {
case CMD_STOP:
digitalWrite(PORT_MOTOR_A_1, LOW);
digitalWrite(PORT_MOTOR_A_2, LOW);
digitalWrite(PORT_MOTOR_B_1, LOW);
digitalWrite(PORT_MOTOR_B_2, LOW);
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_OFF);
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_OFF);
break;
case CMD_FORWARD:
analogWrite(PORT_MOTOR_A_1, MOTOR_POWER);
analogWrite(PORT_MOTOR_A_2, LOW);
analogWrite(PORT_MOTOR_B_1, MOTOR_POWER);
analogWrite(PORT_MOTOR_B_2, LOW);
break;
case CMD_BACKWARD:
analogWrite(PORT_MOTOR_A_1, LOW);
analogWrite(PORT_MOTOR_A_2, MOTOR_POWER);
analogWrite(PORT_MOTOR_B_1, LOW);
analogWrite(PORT_MOTOR_B_2, MOTOR_POWER);
break;
case CMD_FORWARD_LEFT:
analogWrite(PORT_MOTOR_A_1, TURN_POWER);
analogWrite(PORT_MOTOR_A_2, LOW);
analogWrite(PORT_MOTOR_B_1, MAX_POWER);
analogWrite(PORT_MOTOR_B_2, LOW);
digitalWrite(PORT_HL_LIGHT, RELAY_ON);
digitalWrite(PORT_HR_LIGHT, RELAY_OFF);
digitalWrite(PORT_BL_LIGHT, RELAY_ON);
digitalWrite(PORT_BR_LIGHT, RELAY_OFF);
break;
case CMD_FORWARD_RIGHT:
analogWrite(PORT_MOTOR_A_1, MAX_POWER);
analogWrite(PORT_MOTOR_A_2, LOW);
analogWrite(PORT_MOTOR_B_1, TURN_POWER);
analogWrite(PORT_MOTOR_B_2, LOW);
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_ON);
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_ON);
break;
case CMD_BACKWARD_LEFT:
analogWrite(PORT_MOTOR_A_1, LOW);
analogWrite(PORT_MOTOR_A_2, TURN_POWER);
analogWrite(PORT_MOTOR_B_1, LOW);
analogWrite(PORT_MOTOR_B_2, MAX_POWER);
digitalWrite(PORT_HL_LIGHT, RELAY_ON);
digitalWrite(PORT_HR_LIGHT, RELAY_OFF);
digitalWrite(PORT_BL_LIGHT, RELAY_ON);
digitalWrite(PORT_BR_LIGHT, RELAY_OFF);
break;
case CMD_BACKWARD_RIGHT:
analogWrite(PORT_MOTOR_A_1, LOW);
analogWrite(PORT_MOTOR_A_2, MAX_POWER);
analogWrite(PORT_MOTOR_B_1, LOW);
analogWrite(PORT_MOTOR_B_2, TURN_POWER);
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_ON);
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_ON);
break;
case CMD_TURN_ARROUND:
analogWrite(PORT_MOTOR_A_1, MOTOR_POWER);
analogWrite(PORT_MOTOR_A_2, LOW);
analogWrite(PORT_MOTOR_B_1, TURN_POWER + 10);
analogWrite(PORT_MOTOR_B_2, LOW);
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_ON);
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_ON);
break;
case CMD_LIGHT_ON:
digitalWrite(PORT_LIGHT, RELAY_ON);
break;
case CMD_LIGHT_OFF:
digitalWrite(PORT_LIGHT, RELAY_OFF);
break;
case CMD_HEAD_LIGHT_ON:
digitalWrite(PORT_HL_LIGHT, RELAY_ON);
digitalWrite(PORT_HR_LIGHT, RELAY_ON);
break;
case CMD_HEAD_LIGHT_OFF:
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_OFF);
break;
case CMD_BACK_LIGHT_ON:
digitalWrite(PORT_BL_LIGHT, RELAY_ON);
digitalWrite(PORT_BR_LIGHT, RELAY_ON);
break;
case CMD_BACK_LIGHT_OFF:
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_OFF);
break;
case CMD_ALL_LIGHTS_OFF:
digitalWrite(PORT_HL_LIGHT, RELAY_OFF);
digitalWrite(PORT_HR_LIGHT, RELAY_OFF);
digitalWrite(PORT_BL_LIGHT, RELAY_OFF);
digitalWrite(PORT_BR_LIGHT, RELAY_OFF);
break;
case CMD_HORN_ON:
digitalWrite(PORT_HORN, RELAY_ON);
break;
case CMD_HORN_OFF:
digitalWrite(PORT_HORN, RELAY_OFF);
break;
default:
break;
}
}
}