/
RaceCarCounter.ino
335 lines (277 loc) · 7.98 KB
/
RaceCarCounter.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Philip Loury 2013
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Credits to
David A. Mellis
Limor Fried (http://www.ladyada.net)
Tom Igoe
*/
// include the library code:
#include <LiquidCrystal.h>
#include <NewPing.h>
//Pins
const int red = 6;
const int yellow1 = 7;
const int yellow2 = 8;
const int green = 9;
const int speakerPin = 10;
const int buttonPin2 = 11;
const int buttonPin = 12;
const int pingPin = 13;
const int rightLed = A0;
const int leftLed = A1;
const int inPin = A2; //pin which delivers time to receive echo using pulseIn()
int buttonState = 1;
int buttonState2 = 1;
LiquidCrystal lcd(5, 4, 3, 2, A4, A5);
// Music Variables
int songLength = 4; // the number of notes
char notes[] = "cccC "; // a space represents a rest
int beats[] = {2, 2, 2, 3 };
int tempo = 400;
boolean firstTime = true;
// LED Variables;
int leds[] = {red,yellow1,yellow2,green};
int ledLength = 4;
// Timer Variables
double start;
double stop;
boolean raceMode = false;
double lapTimes[10];
boolean refresh=true;
//int triggerRef;
int lapCounter = 1;
double previousLap = 0;
double thisLap;
double lapTime;
double previousLapTime;
double bestLap;
double trackRecordLap = 5.00;
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers red LED
int rightTrack = 7;
int leftTrack = 13;
//right and Lef LED pin numbers
int flash = 0;
double fastestPossible = 0.75;
#define TRIGGER_PIN 13 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN A2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 100 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(115200);
pinMode(red,OUTPUT); //6
pinMode(yellow1,OUTPUT); //7
pinMode(yellow2,OUTPUT); //8
pinMode(green,OUTPUT); //9
pinMode(speakerPin, OUTPUT); //10
pinMode(buttonPin,INPUT); //11
pinMode(buttonPin2,INPUT); //12
pinMode(pingPin, OUTPUT); //13
pinMode(rightLed, OUTPUT); //A0
pinMode(leftLed, OUTPUT); //A1
pinMode(inPin, INPUT); //A2
lcd.begin(16, 2);
lcd.setCursor(1,0);
lcd.print("Press Play to");
lcd.setCursor(5,1);
lcd.print("Begin!");
}
void countTone(){
delay(100);
if(firstTime){
int countDown = 3;
int ledIndex = 0;
for (int i = 0; i < songLength; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
}
else {
lcd.setCursor(3,0);
lcd.print("Get Ready!");
lcd.setCursor(6,1);
if(countDown>0) lcd.print(countDown);
else lcd.print("GO!");
digitalWrite(leds[ledIndex],HIGH);
countDown--;
ledIndex++;
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
firstTime = false;
}
}
void playNote(char note, int duration) {
char names[] = {
'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = {
1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
double getTime(){
return millis()/1000.0;
}
void victory(){
}
void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if(buttonState == LOW){ // THIS USED TO BE LOW
lapCounter = 1;
firstTime = true;
for(int i=0; i<ledLength;i++){
digitalWrite(leds[i],LOW);
}
lcd.clear();
lcd.setCursor(3,0);
lcd.print("Get Ready!");
countTone();
start = getTime();//grabs the time at which
raceMode = true;
refresh = true;
// startTime();
}
if(raceMode){
if(refresh)lcd.clear();
if(flash%3>0){
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(getTime()-start);
}
pong();
refresh = false;
//delay(10);
flash++;
if(flash>99)flash=0;
}//end raceMode
}//end loop
void rightLapCount(){
lcd.setCursor(0,1);
lcd.print("Lap ");
lcd.print(lapCounter);
lcd.print(": ");
if(lapCounter == 1)previousLap = start; //previousLap in MILLIS TIME
thisLap = getTime();
lapTime = thisLap - previousLap;
previousLap = thisLap;
//store the Fastest Lap this Run
if(lapTime < previousLapTime)bestLap = lapTime;
previousLapTime = lapTime;
lcd.print(lapTime);
//Check for Best Ever
if(lapTime<trackRecordLap)victory();
//At End of Race
if(lapCounter >= 10){
lcd.clear();
double stop = getTime();
double total = stop-start;
lcd.home();
lcd.print("Time: ");
lcd.print(total);
lcd.setCursor(0,1);
lcd.print("Best Lap: ");
//Case for Broken Record
if(bestLap<trackRecordLap){
for(int i =0; i<10; i++){
lcd.clear();
lcd.print("Time: ");
lcd.print(total);
lcd.setCursor(0,1);
lcd.print("Best Lap: ");
lcd.print(bestLap);
delay(500);
lcd.clear();
lcd.print("Time: ");
lcd.print(total);
lcd.setCursor(0,1);
delay(250);} }
else{lcd.print(bestLap);
}
raceMode = false;
}//end of At End of Race
//delay(100);
lapCounter++;
//digitalWrite(rightLed,LOW);
}
void pong(){
delay(29); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
int d = uS / US_ROUNDTRIP_CM;
Serial.print(d); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
if(d<=60 && d>=1)digitalWrite(rightLed,HIGH);
else digitalWrite(rightLed,LOW);
if(d<=60 && d>=1 && (getTime()-previousLap)>fastestPossible){
//digitalWrite(rightLed,HIGH);
rightLapCount();
}
}
void usr(){
//raw duration in milliseconds, cm is the
//converted amount into a distance
long duration, cm;
//sending the signal, starting with LOW for a clean signal
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
//setting up the input pin, and receiving the duration in
//microseconds for the sound to bounce off the object infront
duration = pulseIn(inPin, HIGH);
// convert the time into a distance
cm = microsecondsToCentimeters(duration);
//printing the current readings to ther serial display
//Serial.print(cm), Serial.print("cm"), Serial.println();
//checking if anything is within the safezone
if (cm < rightTrack)
{
digitalWrite(rightLed, HIGH); //red is right!
digitalWrite(leftLed, LOW);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("BYAHH!!!");
}
else if(cm<leftTrack && cm >rightTrack)
{
digitalWrite(leftLed, HIGH); //green is left!
digitalWrite(rightLed, LOW);
}
else{
digitalWrite(leftLed,LOW);
digitalWrite(rightLed,LOW);
}
delay(100);
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}