Components and supplies
Arduino Mega 2560
LED (generic)
Breadboard (generic)
Switch Actuator, Head for spring return push-button
Alphanumeric LCD, 16 x 2
Ultrasonic Sensor - HC-SR04 (Generic)
Tools and machines
Laser cutter (generic)
Project description
Code
Drag_Race_Code2.ino
arduino
1/*This is a program for a Drag Race Simulation Game. Using 3 Ultrasonic Sensors (Gas, Brake, Clutch), 2 * 3 Buttons(ON/OFF, UP Shift, Down Shift), 8 LEDs (REV Inicator), and a 16x2 Liquid Crystal Display. 3 * 4 * Sonar Code From https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ 5 * 6 * Rest of Code Original 7 * Gabriel Ogden | Trinity University Engineering Science Design V | gogden@trinity.edu | December 2021 8 9*/ 10 11//Initializing All variables needed 12#include <LiquidCrystal.h> 13int time = 0; 14float speed = 0; 15int speedDisplay = 0; 16LiquidCrystal lcd(2, 3, 4, 5, 6, 7); 17const int STARTBUTTON= 8; 18const int UPSHIFT = 9; 19const int DOWNSHIFT = 10; 20int gear = 1; 21bool ON = false; 22int sbuttontime = 0; 23int ubuttontime = 0; 24float distanceClutch = 0; 25float distanceBrake = 0; 26float distanceGas = 0; 27float duration, distance; 28 29//Setting Pin assignments 30int const LED0 = 22; 31int const LED1 = 23; 32int const LED2 = 24; 33int const LED3 = 30; 34int const LED4 = 26; 35int const LED5 = 27; 36int const LED6 = 28; 37int const LED7 = 29; 38int const BUZZ = 11; 39int trigC = 33; 40int echoC = 32; 41int trigB = 39; 42int echoB = 38; 43int trigG = 41; 44int echoG = 40; 45 46 47void setup() 48{ 49//Setting Our outputs and Inputs 50 pinMode(LED0, OUTPUT); 51 pinMode(LED1, OUTPUT); 52 pinMode(LED2, OUTPUT); 53 pinMode(LED3, OUTPUT); 54 pinMode(LED4, OUTPUT); 55 pinMode(LED5, OUTPUT); 56 pinMode(LED6, OUTPUT); 57 pinMode(LED7, OUTPUT); 58 pinMode(BUZZ, OUTPUT); 59 pinMode(STARTBUTTON, INPUT); 60 pinMode(UPSHIFT, INPUT); 61 pinMode(DOWNSHIFT, INPUT); 62 63 64 pinMode(trigC, OUTPUT); 65 digitalWrite(trigC, LOW); 66 pinMode(echoC, INPUT); 67 68 pinMode(trigB, OUTPUT); 69 digitalWrite(trigB, LOW); 70 pinMode(echoB, INPUT); 71 72 pinMode(trigG, OUTPUT); 73 digitalWrite(trigG, LOW); 74 pinMode(echoG, INPUT); 75 Serial.begin(9600); 76 77//Setting the Resting screen Of LCD 78 lcd.begin(16, 2); 79 lcd.print("TIME"); 80 lcd.setCursor(10,1); 81 lcd.print("MPH"); 82 83//Set starting Gear to 1st 84 gear = 1; 85} 86 87void loop() 88{ 89 90 //Cheching if button is pressed to turn on/off (delay time for checking to ensure user can comfortably use interface) 91if(digitalRead(STARTBUTTON) == HIGH && sbuttontime > 100){ 92 ON = !ON; 93 sbuttontime = 0; 94} 95sbuttontime++; 96 97 98//If system is on 99if(ON){ 100 101//Up shift and down shift checking (similar button delay system as with on/off to prevent misinputs) 102if(digitalRead(UPSHIFT) == LOW && ubuttontime > 15 && gear < 6 && distanceClutch < 5 ){ 103 gear++; 104 ubuttontime = 0; 105 106} 107ubuttontime++; 108if(digitalRead(DOWNSHIFT) == LOW && ubuttontime > 15 && gear > 1 && distanceClutch < 5){ 109 gear--; 110 ubuttontime = 0; 111} 112//Output current gear 113lcd.setCursor(15,0); 114 lcd.print(gear); 115 116 117 118 119//Making the buzzer noise proportional to the speed of car 120if(speed>2 && speed <= 300){ 121 122 tone(BUZZ,speed*5); 123}else{ 124 noTone(BUZZ); 125} 126 127 128 129 130//TIMER 131 lcd.setCursor(6,0); 132 lcd.print(time); 133delay(100); 134 time++; 135 136//display speed 137speedDisplay = speed; 138 lcd.setCursor(10,1); 139 lcd.print("MPH"); 140 lcd.setCursor(6,1); 141 lcd.print(speedDisplay); 142 143 144//Gathering Ultrasonic Sensor distances for Clutch, Brake, Gas 145 digitalWrite(trigC, LOW); 146 delayMicroseconds(2); 147 digitalWrite(trigC, HIGH); 148 delayMicroseconds(10); 149 digitalWrite(trigC, LOW); 150 duration = pulseIn(echoC, HIGH); 151 distance = (duration*.0343)/2; 152 if(distance<=9 && distance>=3){ 153 distanceGas = distance; 154 } 155 digitalWrite(trigB, LOW); 156 delayMicroseconds(2); 157 digitalWrite(trigB, HIGH); 158 delayMicroseconds(10); 159 digitalWrite(trigB, LOW); 160 duration = pulseIn(echoB, HIGH); 161 distance = (duration*.0343)/2; 162 if(distance<=9&&distance>=3){ 163 distanceBrake = distance; 164 } 165 digitalWrite(trigG, LOW); 166 delayMicroseconds(2); 167 digitalWrite(trigG, HIGH); 168 delayMicroseconds(10); 169 digitalWrite(trigG, LOW); 170 duration = pulseIn(echoG, HIGH); 171 distance = (duration*.0343)/2; 172 if(distance<=9&&distance>=3){ 173 distanceClutch = distance; 174 } 175 176 177 178//If gear is 1 because our LEDs, Top speed, and Crash speed will be different for every gear so we do this under an If statement 179if(gear == 1){ 180//setting top gear speed 181 int gearspeed = 50; 182 //This sends the car to the CRASHED screen if speed is too high when shifting down 183 if(speed > gearspeed+50){ 184 speed=-500; 185 } 186 //Acceleration and Braking 187 if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5){ 188 speed+= 10/distanceGas; 189 }else if(speed>1){ 190 speed-= .1; 191 if(distanceBrake<5 && distanceClutch >=5){ 192 speed-= 5/distanceBrake; 193 } 194 } 195 //Turning on LEDs to represent current speed in proportion to the gear's top speed 196 if(speed<=gearspeed*.125){ 197 digitalWrite(LED0, LOW); 198 }else{ 199 digitalWrite(LED0, HIGH); 200 } 201 if(speed<=gearspeed*.25){ 202 digitalWrite(LED1, LOW); 203 }else{ 204 digitalWrite(LED1, HIGH); 205 } 206 if(speed<=gearspeed*.35){ 207 digitalWrite(LED2, LOW); 208 }else{ 209 digitalWrite(LED2, HIGH); 210 } 211 if(speed<=gearspeed*.5){ 212 digitalWrite(LED3, LOW); 213 }else{ 214 digitalWrite(LED3, HIGH); 215 } 216 if(speed<=gearspeed*.7){ 217 digitalWrite(LED4, LOW); 218 }else{ 219 digitalWrite(LED4, HIGH); 220 } 221 if(speed<=gearspeed*.85){ 222 digitalWrite(LED5, LOW); 223 }else{ 224 digitalWrite(LED5, HIGH); 225 } 226 if(speed<=gearspeed*.95){ 227 digitalWrite(LED6, LOW); 228 digitalWrite(LED7, LOW); 229 }else{ 230 digitalWrite(LED6, HIGH); 231 digitalWrite(LED7, HIGH); 232 } 233 234} 235 236//Repeat of Gear 1 with different Values 237if(gear == 2){ 238 int gearspeed = 100; 239 if(speed > gearspeed+50){ 240 speed=-500; 241 } 242 if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 30){ 243 speed+= 10/distanceGas; 244 }else if(speed>1){ 245 speed-= .1; 246 if(distanceBrake<5 && distanceClutch >=5){ 247 speed-= 5/distanceBrake; 248 } 249 } 250 251 if(speed<=gearspeed*.125){ 252 digitalWrite(LED0, LOW); 253 }else{ 254 digitalWrite(LED0, HIGH); 255 } 256 if(speed<=gearspeed*.25){ 257 digitalWrite(LED1, LOW); 258 }else{ 259 digitalWrite(LED1, HIGH); 260 } 261 if(speed<=gearspeed*.35){ 262 digitalWrite(LED2, LOW); 263 }else{ 264 digitalWrite(LED2, HIGH); 265 } 266 if(speed<=gearspeed*.5){ 267 digitalWrite(LED3, LOW); 268 }else{ 269 digitalWrite(LED3, HIGH); 270 } 271 if(speed<=gearspeed*.7){ 272 digitalWrite(LED4, LOW); 273 }else{ 274 digitalWrite(LED4, HIGH); 275 } 276 if(speed<=gearspeed*.85){ 277 digitalWrite(LED5, LOW); 278 }else{ 279 digitalWrite(LED5, HIGH); 280 } 281 if(speed<=gearspeed*.95){ 282 digitalWrite(LED6, LOW); 283 digitalWrite(LED7, LOW); 284 }else{ 285 digitalWrite(LED6, HIGH); 286 digitalWrite(LED7, HIGH); 287 } 288 289} 290//Repeat of Gear 1 with different Values 291if(gear == 3){ 292 int gearspeed = 150; 293 if(speed > gearspeed+50){ 294 speed=-500; 295 } 296 if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 80){ 297 speed+= 10/distanceGas; 298 }else if(speed>1){ 299 speed-= .1; 300 if(distanceBrake<5 && distanceClutch >=5){ 301 speed-= 5/distanceBrake; 302 } 303 } 304 305 if(speed<=gearspeed*.125){ 306 digitalWrite(LED0, LOW); 307 }else{ 308 digitalWrite(LED0, HIGH); 309 } 310 if(speed<=gearspeed*.25){ 311 digitalWrite(LED1, LOW); 312 }else{ 313 digitalWrite(LED1, HIGH); 314 } 315 if(speed<=gearspeed*.35){ 316 digitalWrite(LED2, LOW); 317 }else{ 318 digitalWrite(LED2, HIGH); 319 } 320 if(speed<=gearspeed*.5){ 321 digitalWrite(LED3, LOW); 322 }else{ 323 digitalWrite(LED3, HIGH); 324 } 325 if(speed<=gearspeed*.7){ 326 digitalWrite(LED4, LOW); 327 }else{ 328 digitalWrite(LED4, HIGH); 329 } 330 if(speed<=gearspeed*.85){ 331 digitalWrite(LED5, LOW); 332 }else{ 333 digitalWrite(LED5, HIGH); 334 } 335 if(speed<=gearspeed*.95){ 336 digitalWrite(LED6, LOW); 337 digitalWrite(LED7, LOW); 338 }else{ 339 digitalWrite(LED6, HIGH); 340 digitalWrite(LED7, HIGH); 341 } 342 343} 344//Repeat of Gear 1 with different Values 345if(gear == 4){ 346 int gearspeed = 200; 347 if(speed > gearspeed+50){ 348 speed=-500; 349 } 350 if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5&& speed > 130){ 351 speed+= 10/distanceGas; 352 }else if(speed>1){ 353 speed-= .1; 354 if(distanceBrake<5 && distanceClutch >=5){ 355 speed-= 5/distanceBrake; 356 } 357 } 358 359 if(speed<=gearspeed*.125){ 360 digitalWrite(LED0, LOW); 361 }else{ 362 digitalWrite(LED0, HIGH); 363 } 364 if(speed<=gearspeed*.25){ 365 digitalWrite(LED1, LOW); 366 }else{ 367 digitalWrite(LED1, HIGH); 368 } 369 if(speed<=gearspeed*.35){ 370 digitalWrite(LED2, LOW); 371 }else{ 372 digitalWrite(LED2, HIGH); 373 } 374 if(speed<=gearspeed*.5){ 375 digitalWrite(LED3, LOW); 376 }else{ 377 digitalWrite(LED3, HIGH); 378 } 379 if(speed<=gearspeed*.7){ 380 digitalWrite(LED4, LOW); 381 }else{ 382 digitalWrite(LED4, HIGH); 383 } 384 if(speed<=gearspeed*.85){ 385 digitalWrite(LED5, LOW); 386 }else{ 387 digitalWrite(LED5, HIGH); 388 } 389 if(speed<=gearspeed*.95){ 390 digitalWrite(LED6, LOW); 391 digitalWrite(LED7, LOW); 392 }else{ 393 digitalWrite(LED6, HIGH); 394 digitalWrite(LED7, HIGH); 395 } 396 397} 398//Repeat of Gear 1 with different Values 399if(gear == 5){ 400 int gearspeed = 250; 401 if(speed > gearspeed+50){ 402 speed=-500; 403 } 404 if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 180){ 405 speed+= 10/distanceGas; 406 }else if(speed>1){ 407 speed-= .1; 408 if(distanceBrake<5 && distanceClutch >=5){ 409 speed-= 5/distanceBrake; 410 } 411 } 412 413 if(speed<=gearspeed*.125){ 414 digitalWrite(LED0, LOW); 415 }else{ 416 digitalWrite(LED0, HIGH); 417 } 418 if(speed<=gearspeed*.25){ 419 digitalWrite(LED1, LOW); 420 }else{ 421 digitalWrite(LED1, HIGH); 422 } 423 if(speed<=gearspeed*.35){ 424 digitalWrite(LED2, LOW); 425 }else{ 426 digitalWrite(LED2, HIGH); 427 } 428 if(speed<=gearspeed*.5){ 429 digitalWrite(LED3, LOW); 430 }else{ 431 digitalWrite(LED3, HIGH); 432 } 433 if(speed<=gearspeed*.7){ 434 digitalWrite(LED4, LOW); 435 }else{ 436 digitalWrite(LED4, HIGH); 437 } 438 if(speed<=gearspeed*.85){ 439 digitalWrite(LED5, LOW); 440 }else{ 441 digitalWrite(LED5, HIGH); 442 } 443 if(speed<=gearspeed*.95){ 444 digitalWrite(LED6, LOW); 445 digitalWrite(LED7, LOW); 446 }else{ 447 digitalWrite(LED6, HIGH); 448 digitalWrite(LED7, HIGH); 449 } 450 451} 452//Repeat of Gear 1 with different Values 453if(gear == 6){ 454 int gearspeed = 300; 455 if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 230){ 456 speed+= 10/distanceGas; 457 }else if(speed>1){ 458 speed-= .1; 459 if(distanceBrake<5 && distanceClutch >=5){ 460 speed-= 5/distanceBrake; 461 } 462 } 463 464 if(speed<=gearspeed*.125){ 465 digitalWrite(LED0, LOW); 466 }else{ 467 digitalWrite(LED0, HIGH); 468 } 469 if(speed<=gearspeed*.25){ 470 digitalWrite(LED1, LOW); 471 }else{ 472 digitalWrite(LED1, HIGH); 473 } 474 if(speed<=gearspeed*.35){ 475 digitalWrite(LED2, LOW); 476 }else{ 477 digitalWrite(LED2, HIGH); 478 } 479 if(speed<=gearspeed*.5){ 480 digitalWrite(LED3, LOW); 481 }else{ 482 digitalWrite(LED3, HIGH); 483 } 484 if(speed<=gearspeed*.7){ 485 digitalWrite(LED4, LOW); 486 }else{ 487 digitalWrite(LED4, HIGH); 488 } 489 if(speed<=gearspeed*.85){ 490 digitalWrite(LED5, LOW); 491 }else{ 492 digitalWrite(LED5, HIGH); 493 } 494 if(speed<=gearspeed*.95){ 495 digitalWrite(LED6, LOW); 496 digitalWrite(LED7, LOW); 497 }else{ 498 digitalWrite(LED6, HIGH); 499 digitalWrite(LED7, HIGH); 500 } 501 502 503} 504//Crash condition. Speed only goes negative when manually set within the gear If loops 505if(speed<-50){ 506 lcd.setCursor(6,1); 507 lcd.print("CRASHED "); 508 speed = 500; 509 noTone(BUZZ); 510 speed-=1000; 511 time--; 512 noTone(BUZZ); 513} 514//Win condition 515if(speed >= 300){ 516 lcd.setCursor(6,1); 517 lcd.print("WIN "); 518 speed = 500; 519 noTone(BUZZ); 520 speed+=1000; 521 time--; 522 noTone(BUZZ); 523} 524 525}else{ 526 //System is off 527speed = 0; 528time = 0; 529 digitalWrite(LED0, LOW); 530 digitalWrite(LED1, LOW); 531 digitalWrite(LED2, LOW); 532 digitalWrite(LED3, LOW); 533 digitalWrite(LED4, LOW); 534 digitalWrite(LED5, LOW); 535 digitalWrite(LED6, LOW); 536 digitalWrite(LED7, LOW); 537 lcd.setCursor(6,0); 538 lcd.print(" "); 539 lcd.setCursor(6,1); 540 lcd.print(" "); 541 542 gear = 1; 543noTone(BUZZ); 544} 545} 546
Downloadable files
Schematic
Schematic
Schematic
Schematic
Comments
Only logged in users can leave comments