1#include <SoftwareSerial.h>
2
3#define TxD 12
4 pins
5#define RxD 2
6
7#define MOTOR_FORWARD 9
8#define MOTOR_BACKWARD
9 10
10#define MOTOR_LEFT 5
11#define MOTOR_RIGHT 6
12
13#define LED_PIN 13
14#define
15 BUZZER 33
16#define BATTERY_VOLTAGE A0
17
18SoftwareSerial bluetoothSerial(TxD,
19 RxD);
20
21int c;
22int speedIntensity = 192;
23 at 3/4 max speed (where app slider begins)
24double batteryVoltageIn;
25double
26 batteryVoltage;
27boolean ledOn = false;
28boolean inMotion = false;
29boolean
30 goingForward = true;
31
32void setup() {
33 bluetoothSerial.begin(9600);
34
35 Serial.begin(9600);
36
37 pinMode(LED_PIN, OUTPUT);
38 pinMode(BUZZER,
39 OUTPUT);
40 pinMode(BATTERY_VOLTAGE, INPUT);
41
42 pinMode(MOTOR_FORWARD,
43 OUTPUT);
44 pinMode(MOTOR_BACKWARD, OUTPUT);
45 pinMode(MOTOR_LEFT, OUTPUT);
46
47 pinMode(MOTOR_RIGHT, OUTPUT);
48}
49
50void loop() {
51
52 99 up, 98 down, 100 right, 101 left, 102 stop
53 if (bluetoothSerial.available()){
54
55 in app
56 c = bluetoothSerial.read();
57 Serial.println(c);
58 if (c
59 == 1 || c == 99){
60 forwardSpeed(speedIntensity);
61
62 inMotion = true;
63 goingForward = true;
64 }
65 else if (c
66 == 2 || c == 98){
67 inMotion = true;
68 backwardSpeed(speedIntensity);
69
70 goingForward = false;
71 }
72 else if (c == 3 || c == 101){
73 leftTurn();
74
75 }
76 else if (c == 4 || c == 100){
77 rightTurn();
78 }
79 else
80 if (c == 0 || c == 102){
81 inMotion = false;
82 halt();
83 }
84
85 else if (c == 5){
86 ledOn = !ledOn;
87 if
88 (ledOn){
89 digitalWrite(LED_PIN, HIGH);
90 }
91 else if (!ledOn){
92
93 digitalWrite(LED_PIN, LOW);
94 }
95 }
96 else if (c >= 10
97 && c <= 50){
98 speedIntensity =
99 map(c, 10, 50, 0, 255);
100 if (inMotion){
101 if (goingForward){
102
103 forwardSpeed(speedIntensity);
104 }
105 else if (!goingForward){
106
107 backwardSpeed(speedIntensity);
108 }
109 }
110 }
111 else
112 if (c == 6){
113 digitalWrite(BUZZER, HIGH);
114 }
115 else if (c ==
116 7){
117 digitalWrite(BUZZER, LOW);
118 }
119 else if (c == 51){
120 digitalWrite(LED_PIN,
121 HIGH);
122 }
123 else if (c == 52){
124 digitalWrite(LED_PIN, LOW);
125
126 }
127 else if (c == 8){
128 batteryVoltageIn = (double)analogRead(BATTERY_VOLTAGE)
129 / 204.6;
130 batteryVoltage = (8.0/5.0) * batteryVoltageIn;
131 to voltage of 8V battery
132 if (batteryVoltage >= 7.40){
133 specify which of 4 battery levels car is at
134 bluetoothSerial.write(60);
135
136 }
137 else if (batteryVoltage >= 6.90){
138 bluetoothSerial.write(61);
139
140 }
141 else if (batteryVoltage >= 6.40){
142 bluetoothSerial.write(62);
143
144 }
145 else{
146 bluetoothSerial.write(63);
147 }
148 }
149
150 }
151}
152
153void rightTurn(){
154 digitalWrite(MOTOR_LEFT, LOW);
155 digitalWrite(MOTOR_RIGHT,
156 HIGH);
157}
158
159void leftTurn(){
160 digitalWrite(MOTOR_LEFT, HIGH);
161 digitalWrite(MOTOR_RIGHT,
162 LOW);
163}
164
165void forwardSpeed(int speedInput){
166 analogWrite(MOTOR_FORWARD,
167 speedInput);
168 digitalWrite(MOTOR_BACKWARD, LOW);
169 digitalWrite(MOTOR_LEFT,
170 LOW);
171 digitalWrite(MOTOR_RIGHT, LOW);
172}
173
174void backwardSpeed(int speedInput){
175
176 digitalWrite(MOTOR_FORWARD, LOW);
177 analogWrite(MOTOR_BACKWARD, speedInput);
178
179 digitalWrite(MOTOR_LEFT, LOW);
180 digitalWrite(MOTOR_RIGHT, LOW);
181}
182
183void
184 halt(){
185 digitalWrite(MOTOR_FORWARD, LOW);
186 digitalWrite(MOTOR_BACKWARD,
187 LOW);
188 digitalWrite(MOTOR_LEFT, LOW);
189 digitalWrite(MOTOR_RIGHT, LOW);
190}