-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRCCarSerial.ino
191 lines (157 loc) · 3.99 KB
/
RCCarSerial.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
/*
Control a simple RX2-TX2 based remote control car by soldering wires to the remote control buttons.
*/
/* Connect the wires from the RC remote to the following pins */
const int fwdPin = 2; // Forward button
const int bckPin = 3; // Back button
const int lftPin = 4; // Left button
const int rgtPin = 5; // Right button
/* Each direction gets a number */
const int NEUTRAL = 0;
const int FORWARD = 1;
const int REVERSE = 2;
const int LEFT = 3;
const int RIGHT = 4;
/* Just leave this as it is */
void setup() {
pinMode( fwdPin, OUTPUT );
pinMode( bckPin, OUTPUT );
pinMode( lftPin, OUTPUT );
pinMode( rgtPin, OUTPUT );
pinMode( 13, OUTPUT );
digitalWrite( fwdPin, HIGH );
digitalWrite( bckPin, HIGH );
digitalWrite( lftPin, HIGH );
digitalWrite( rgtPin, HIGH );
Serial.begin(9600);
}
/*
This function gets executed over and over. So after the last command
has executed it will start again at the top.
You can edit the commands between the EDIT THIS & STOP HERE comments.
Available commands are:
forward( time );
forward_left( time );
forward_right( time );
back( time );
back_left( time );
back_right( time );
delay( time);
TIPS:
- delay means 'do nothing'.
- 'time' should be a number in milliseconds (1000 ms = 1 second)
- Don't forget the closing semicolon (;), without it the code won't compile!
*/
void loop() {
while (Serial.available()) {
char inChar = (char)Serial.read();
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
if( inChar == 'A' ){
forward_left( 200 );
} else if( inChar == 'B' ) {
forward_right( 200 );
}
Serial.println("ok");
}
/* BUT STOP HERE :) */
}
/**
* Next functions below control the car. All take a time in ms as a parameter
**/
void forward( int duration ) {
gas( FORWARD );
steer( NEUTRAL );
delay( duration );
stop();
}
void forward_left( int duration ) {
gas( FORWARD );
steer( LEFT );
delay( duration );
stop();
}
void forward_right( int duration ) {
gas( FORWARD );
steer( RIGHT );
delay( duration );
stop();
}
void back( int duration ) {
gas( REVERSE );
steer( NEUTRAL );
delay( duration );
stop();
}
void back_left( int duration ) {
gas( REVERSE );
steer( LEFT );
delay( duration );
stop();
}
void back_right( int duration ) {
gas( REVERSE );
steer( RIGHT );
delay( duration );
stop();
}
/**
* Push the forward or back button
* @param state (int) Direction, use direction constant
**/
void gas( int state ) {
if ( state == FORWARD ) {
switchPins( fwdPin, bckPin, fwdPin );
} else if ( state == REVERSE ) {
switchPins( fwdPin, bckPin, bckPin );
} else {
switchPins( fwdPin, bckPin, 0 );
}
delay( 5 );
}
/**
* Push the left or right button
* @param state (int) Direction to steer, use direction constant
**/
void steer( int state ) {
if ( state == LEFT ) {
switchPins( lftPin, rgtPin, lftPin );
} else if ( state == RIGHT ) {
switchPins( lftPin, rgtPin, rgtPin );
} else {
switchPins( lftPin, rgtPin, 0 );
}
delay( 5 );
}
/**
* Unpress all buttons
**/
void stop() {
gas( NEUTRAL );
steer( NEUTRAL );
}
/**
* We always exclusively use either forward OR back or left OR right,
* so this function toggles one to LOW (pushes the button) while the other
* or both (when 'which' does not match any pin) get set to an
* INPUT (button not pressed / high impedance).
* @param pin1 (int) First pin (use pin constant)
* @param pin2 (int) Second pin (use pin constant)
* @param which (int) If this matches pin1 or pin2 that pin (IE button) will be toggled low.
if there is no match both pins will be inputs and no button will be pressed
*/
void switchPins( int pin1, int pin2, int which ) {
if ( which == pin1 ) {
pinMode( pin1, OUTPUT );
digitalWrite( pin1, LOW );
pinMode( pin2, INPUT );
} else if ( which == pin2 ) {
pinMode( pin1, INPUT );
pinMode( pin2, OUTPUT );
digitalWrite( pin1, LOW );
} else {
pinMode( pin1, INPUT );
pinMode( pin2, INPUT );
}
}