source: robotics/car/firmware/car/car.ino @ 3a28fb6

Last change on this file since 3a28fb6 was 3a28fb6, checked in by Steve Castellotti <sc@…>, 8 years ago
  • repeat function complete
  • debug output added
  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*
2
3Puzzlebox - Robotics - Car
4
5Puzzlebox Robotics car driving sketch.
6
7Copyright Puzzlebox Productions, LLC (2014)
8
9This code is released under the GNU Pulic License (GPL) version 2
10
11This software is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
15You should have received a copy of the GNU General Public
16License along with this code; if not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19For more information about this licence please refer to http://www.gnu.org/copyleft/gpl.html
20
21For more details about the product please check http://puzzlebox.info
22
23Original Author: Steve Castellotti <sc@puzzlebox.info>
24
25Modified 2014-06-16
26by Steve Castellotti <sc@puzzlebox.info>
27
28*/
29
30#include <Servo.h>
31
32#define DEBUG_OUTPUT 1 // 1 for debug
33
34#define SERVO_PIN_BRAKE 7
35#define SERVO_PIN_DIRECTION 8
36#define SERVO_PIN_TURN 9
37
38Servo servoBrake;
39Servo servoDirection;
40Servo servoTurn;
41
42char _command;
43char lastCommand = '2'; // stop
44
45
46// ################################################################
47
48/////////
49//SETUP//
50/////////
51
52void setup() {
53       
54  Serial.begin(115200);
55
56  servoBrake.attach(SERVO_PIN_BRAKE);
57  servoDirection.attach(SERVO_PIN_DIRECTION);
58  servoTurn.attach(SERVO_PIN_TURN);
59
60} // setup
61
62
63// ################################################################
64
65/////////////
66//MAIN LOOP//
67/////////////
68
69void loop() {
70
71  parseSerialInput();
72
73} // Main loop
74
75
76// ################################################################
77
78int parseValue() {
79 
80  char inByte=0;
81  int a=0;
82  int b=0;
83  int c=0;
84  int newValue=0;
85
86  while (Serial.available() == 0);
87  inByte = Serial.read() - '0';
88  a = inByte;
89
90  while (Serial.available() == 0);
91  inByte = Serial.read() - '0';
92  b = inByte;
93
94  while (Serial.available() == 0);
95  inByte = Serial.read() - '0';
96  c = inByte;
97
98  newValue = (a * 100) + (b * 10) + c;
99
100  return(newValue);
101
102} // parseValue
103
104
105// ################################################################
106
107void parseSerialInput() {
108
109  if (Serial.available() > 0)  {
110
111    _command = Serial.read();
112
113    #if DEBUG_OUTPUT
114      Serial.print("Serial.read(): ");
115      Serial.println(_command);
116    #endif
117
118    parseCommand(_command);
119   
120    if (_command != '0') {
121      // Don't directly set lastCommand to "repeat"
122      lastCommand = _command;
123    }
124   
125  }
126
127} // parseSerialInput()
128
129
130// ################################################################
131
132void parseCommand(char command) {
133
134    switch (command) {
135
136      case '0':  driveRepeat(); break;
137      case '1':  driveForward(); break;
138      case '2':  driveStop(); break;
139      case '3':  driveLeft(); break;
140      case '4':  driveRight(); break;
141     
142    }
143}
144
145// ################################################################
146
147void driveRepeat() {
148 
149  #if DEBUG_OUTPUT
150    Serial.println("driveRepeat()");
151  #endif
152 
153  parseCommand(lastCommand);
154
155}
156
157// ################################################################
158
159void driveForward() {
160
161  #if DEBUG_OUTPUT
162    Serial.println("driveForward()");
163  #endif
164 
165}
166
167// ################################################################
168
169void driveStop() {
170
171  #if DEBUG_OUTPUT
172    Serial.println("driveStop()");
173  #endif
174 
175}
176
177// ################################################################
178
179void driveLeft() {
180
181  #if DEBUG_OUTPUT
182    Serial.println("driveLeft()");
183  #endif
184 
185}
186
187// ################################################################
188
189void driveRight() {
190
191  #if DEBUG_OUTPUT
192    Serial.println("driveRight()");
193  #endif
194 
195}
196
197// ################################################################
Note: See TracBrowser for help on using the repository browser.