-
Notifications
You must be signed in to change notification settings - Fork 1
/
CarRemote.ino
161 lines (155 loc) · 3.05 KB
/
CarRemote.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
/*
* Create: 2017-06-27 by CKD - Pham Duy Anh - [email protected]
*
* Arduino UNO -> L298 module
* Pin 2 -> IN4
* Pin 3 -> IN3
* Pin 4 -> IN1
* Pin 5 -> IN2
* Pin 9 ~ -> ENA PWM for speed control motor A
* Pin 10 ~ -> ENB PWM for speed control motor B
*
* Use SoftwareSerial for communication to Bluetooth module
* Arduino UNO -> HC-05 Bluetooth module
* A4 (Tx) -> Rx
* A5 (Rx) -> Tx
* 5V -> Vcc
* Gnd -> Gnd
*/
#define IN1 2
#define IN2 3
#define IN3 4
#define IN4 5
#define SP1 9
#define SP2 10
#include <SoftwareSerial.h>
SoftwareSerial Bluetooth(A5, A4); // RX, TX
char cmd;
int sp = 70;
void setup()
{
Serial.begin(115200);
Bluetooth.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(SP1, OUTPUT);
pinMode(SP2, OUTPUT);
motor(0, 0);
}
void loop()
{
if (Bluetooth.available())
{
cmd = Bluetooth.read();
switch (cmd)
{
default:
motor(0, 0);
break;
case 'S':
motor(0, 0);
break;
case 'F':
motor(1, 1);
break;
case 'B':
motor(-1, -1);
break;
case 'L':
motor(-1, 1);
break;
case 'R':
motor(1, -1);
break;
case 'I':
motor(1, 0);
break;
case 'G':
motor(0, 1);
break;
case 'H':
motor(0, -1);
break;
case 'J':
motor(-1, 0);
break;
case '1':
sp = 1*20 + 50;
break;
case '2':
sp = 2*20 + 50;
break;
case '3':
sp = 3*20 + 50;
break;
case '4':
sp = 4*20 + 50;
break;
case '5':
sp = 5*20 + 50;
break;
case '6':
sp = 6*20 + 50;
break;
case '7':
sp = 7*20 + 50;
break;
case '8':
sp = 8*20 + 50;
break;
case '9':
sp = 9*20 + 50;
break;
case 'q':
sp = 10*20 + 50;
break;
}
Serial.println(cmd);
Serial.println(sp);
analogWrite(SP1, sp);
analogWrite(SP2, sp);
}
}
void motor(int motor1, int motor2)
{
switch (motor1)
{
case 0:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
break;
case 1:
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
break;
case -1:
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
break;
default:
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
break;
}
switch (motor2)
{
case 0:
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
case 1:
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
break;
case -1:
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
break;
default:
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
break;
}
}