/*This is a program for a Drag Race Simulation Game. Using 3 Ultrasonic Sensors (Gas, Brake, Clutch),
* 3 Buttons(ON/OFF, UP Shift, Down Shift), 8 LEDs (REV Inicator), and a 16x2 Liquid Crystal Display.
*
* Sonar Code From https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/
*
* Rest of Code Original
* Gabriel Ogden | Trinity University Engineering Science Design V | gogden@trinity.edu | December 2021
*/
//Initializing All variables needed
#include <LiquidCrystal.h>
int time = 0;
float speed = 0;
int speedDisplay = 0;
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
const int STARTBUTTON= 8;
const int UPSHIFT = 9;
const int DOWNSHIFT = 10;
int gear = 1;
bool ON = false;
int sbuttontime = 0;
int ubuttontime = 0;
float distanceClutch = 0;
float distanceBrake = 0;
float distanceGas = 0;
float duration, distance;
//Setting Pin assignments
int const LED0 = 22;
int const LED1 = 23;
int const LED2 = 24;
int const LED3 = 30;
int const LED4 = 26;
int const LED5 = 27;
int const LED6 = 28;
int const LED7 = 29;
int const BUZZ = 11;
int trigC = 33;
int echoC = 32;
int trigB = 39;
int echoB = 38;
int trigG = 41;
int echoG = 40;
void setup()
{
//Setting Our outputs and Inputs
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);
pinMode(LED7, OUTPUT);
pinMode(BUZZ, OUTPUT);
pinMode(STARTBUTTON, INPUT);
pinMode(UPSHIFT, INPUT);
pinMode(DOWNSHIFT, INPUT);
pinMode(trigC, OUTPUT);
digitalWrite(trigC, LOW);
pinMode(echoC, INPUT);
pinMode(trigB, OUTPUT);
digitalWrite(trigB, LOW);
pinMode(echoB, INPUT);
pinMode(trigG, OUTPUT);
digitalWrite(trigG, LOW);
pinMode(echoG, INPUT);
Serial.begin(9600);
//Setting the Resting screen Of LCD
lcd.begin(16, 2);
lcd.print("TIME");
lcd.setCursor(10,1);
lcd.print("MPH");
//Set starting Gear to 1st
gear = 1;
}
void loop()
{
//Cheching if button is pressed to turn on/off (delay time for checking to ensure user can comfortably use interface)
if(digitalRead(STARTBUTTON) == HIGH && sbuttontime > 100){
ON = !ON;
sbuttontime = 0;
}
sbuttontime++;
//If system is on
if(ON){
//Up shift and down shift checking (similar button delay system as with on/off to prevent misinputs)
if(digitalRead(UPSHIFT) == LOW && ubuttontime > 15 && gear < 6 && distanceClutch < 5 ){
gear++;
ubuttontime = 0;
}
ubuttontime++;
if(digitalRead(DOWNSHIFT) == LOW && ubuttontime > 15 && gear > 1 && distanceClutch < 5){
gear--;
ubuttontime = 0;
}
//Output current gear
lcd.setCursor(15,0);
lcd.print(gear);
//Making the buzzer noise proportional to the speed of car
if(speed>2 && speed <= 300){
tone(BUZZ,speed*5);
}else{
noTone(BUZZ);
}
//TIMER
lcd.setCursor(6,0);
lcd.print(time);
delay(100);
time++;
//display speed
speedDisplay = speed;
lcd.setCursor(10,1);
lcd.print("MPH");
lcd.setCursor(6,1);
lcd.print(speedDisplay);
//Gathering Ultrasonic Sensor distances for Clutch, Brake, Gas
digitalWrite(trigC, LOW);
delayMicroseconds(2);
digitalWrite(trigC, HIGH);
delayMicroseconds(10);
digitalWrite(trigC, LOW);
duration = pulseIn(echoC, HIGH);
distance = (duration*.0343)/2;
if(distance<=9 && distance>=3){
distanceGas = distance;
}
digitalWrite(trigB, LOW);
delayMicroseconds(2);
digitalWrite(trigB, HIGH);
delayMicroseconds(10);
digitalWrite(trigB, LOW);
duration = pulseIn(echoB, HIGH);
distance = (duration*.0343)/2;
if(distance<=9&&distance>=3){
distanceBrake = distance;
}
digitalWrite(trigG, LOW);
delayMicroseconds(2);
digitalWrite(trigG, HIGH);
delayMicroseconds(10);
digitalWrite(trigG, LOW);
duration = pulseIn(echoG, HIGH);
distance = (duration*.0343)/2;
if(distance<=9&&distance>=3){
distanceClutch = distance;
}
//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
if(gear == 1){
//setting top gear speed
int gearspeed = 50;
//This sends the car to the CRASHED screen if speed is too high when shifting down
if(speed > gearspeed+50){
speed=-500;
}
//Acceleration and Braking
if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5){
speed+= 10/distanceGas;
}else if(speed>1){
speed-= .1;
if(distanceBrake<5 && distanceClutch >=5){
speed-= 5/distanceBrake;
}
}
//Turning on LEDs to represent current speed in proportion to the gear's top speed
if(speed<=gearspeed*.125){
digitalWrite(LED0, LOW);
}else{
digitalWrite(LED0, HIGH);
}
if(speed<=gearspeed*.25){
digitalWrite(LED1, LOW);
}else{
digitalWrite(LED1, HIGH);
}
if(speed<=gearspeed*.35){
digitalWrite(LED2, LOW);
}else{
digitalWrite(LED2, HIGH);
}
if(speed<=gearspeed*.5){
digitalWrite(LED3, LOW);
}else{
digitalWrite(LED3, HIGH);
}
if(speed<=gearspeed*.7){
digitalWrite(LED4, LOW);
}else{
digitalWrite(LED4, HIGH);
}
if(speed<=gearspeed*.85){
digitalWrite(LED5, LOW);
}else{
digitalWrite(LED5, HIGH);
}
if(speed<=gearspeed*.95){
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}else{
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
}
//Repeat of Gear 1 with different Values
if(gear == 2){
int gearspeed = 100;
if(speed > gearspeed+50){
speed=-500;
}
if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 30){
speed+= 10/distanceGas;
}else if(speed>1){
speed-= .1;
if(distanceBrake<5 && distanceClutch >=5){
speed-= 5/distanceBrake;
}
}
if(speed<=gearspeed*.125){
digitalWrite(LED0, LOW);
}else{
digitalWrite(LED0, HIGH);
}
if(speed<=gearspeed*.25){
digitalWrite(LED1, LOW);
}else{
digitalWrite(LED1, HIGH);
}
if(speed<=gearspeed*.35){
digitalWrite(LED2, LOW);
}else{
digitalWrite(LED2, HIGH);
}
if(speed<=gearspeed*.5){
digitalWrite(LED3, LOW);
}else{
digitalWrite(LED3, HIGH);
}
if(speed<=gearspeed*.7){
digitalWrite(LED4, LOW);
}else{
digitalWrite(LED4, HIGH);
}
if(speed<=gearspeed*.85){
digitalWrite(LED5, LOW);
}else{
digitalWrite(LED5, HIGH);
}
if(speed<=gearspeed*.95){
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}else{
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
}
//Repeat of Gear 1 with different Values
if(gear == 3){
int gearspeed = 150;
if(speed > gearspeed+50){
speed=-500;
}
if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 80){
speed+= 10/distanceGas;
}else if(speed>1){
speed-= .1;
if(distanceBrake<5 && distanceClutch >=5){
speed-= 5/distanceBrake;
}
}
if(speed<=gearspeed*.125){
digitalWrite(LED0, LOW);
}else{
digitalWrite(LED0, HIGH);
}
if(speed<=gearspeed*.25){
digitalWrite(LED1, LOW);
}else{
digitalWrite(LED1, HIGH);
}
if(speed<=gearspeed*.35){
digitalWrite(LED2, LOW);
}else{
digitalWrite(LED2, HIGH);
}
if(speed<=gearspeed*.5){
digitalWrite(LED3, LOW);
}else{
digitalWrite(LED3, HIGH);
}
if(speed<=gearspeed*.7){
digitalWrite(LED4, LOW);
}else{
digitalWrite(LED4, HIGH);
}
if(speed<=gearspeed*.85){
digitalWrite(LED5, LOW);
}else{
digitalWrite(LED5, HIGH);
}
if(speed<=gearspeed*.95){
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}else{
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
}
//Repeat of Gear 1 with different Values
if(gear == 4){
int gearspeed = 200;
if(speed > gearspeed+50){
speed=-500;
}
if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5&& speed > 130){
speed+= 10/distanceGas;
}else if(speed>1){
speed-= .1;
if(distanceBrake<5 && distanceClutch >=5){
speed-= 5/distanceBrake;
}
}
if(speed<=gearspeed*.125){
digitalWrite(LED0, LOW);
}else{
digitalWrite(LED0, HIGH);
}
if(speed<=gearspeed*.25){
digitalWrite(LED1, LOW);
}else{
digitalWrite(LED1, HIGH);
}
if(speed<=gearspeed*.35){
digitalWrite(LED2, LOW);
}else{
digitalWrite(LED2, HIGH);
}
if(speed<=gearspeed*.5){
digitalWrite(LED3, LOW);
}else{
digitalWrite(LED3, HIGH);
}
if(speed<=gearspeed*.7){
digitalWrite(LED4, LOW);
}else{
digitalWrite(LED4, HIGH);
}
if(speed<=gearspeed*.85){
digitalWrite(LED5, LOW);
}else{
digitalWrite(LED5, HIGH);
}
if(speed<=gearspeed*.95){
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}else{
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
}
//Repeat of Gear 1 with different Values
if(gear == 5){
int gearspeed = 250;
if(speed > gearspeed+50){
speed=-500;
}
if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 180){
speed+= 10/distanceGas;
}else if(speed>1){
speed-= .1;
if(distanceBrake<5 && distanceClutch >=5){
speed-= 5/distanceBrake;
}
}
if(speed<=gearspeed*.125){
digitalWrite(LED0, LOW);
}else{
digitalWrite(LED0, HIGH);
}
if(speed<=gearspeed*.25){
digitalWrite(LED1, LOW);
}else{
digitalWrite(LED1, HIGH);
}
if(speed<=gearspeed*.35){
digitalWrite(LED2, LOW);
}else{
digitalWrite(LED2, HIGH);
}
if(speed<=gearspeed*.5){
digitalWrite(LED3, LOW);
}else{
digitalWrite(LED3, HIGH);
}
if(speed<=gearspeed*.7){
digitalWrite(LED4, LOW);
}else{
digitalWrite(LED4, HIGH);
}
if(speed<=gearspeed*.85){
digitalWrite(LED5, LOW);
}else{
digitalWrite(LED5, HIGH);
}
if(speed<=gearspeed*.95){
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}else{
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
}
//Repeat of Gear 1 with different Values
if(gear == 6){
int gearspeed = 300;
if (speed<=gearspeed && distanceGas < 6 && distanceClutch >= 5 && speed > 230){
speed+= 10/distanceGas;
}else if(speed>1){
speed-= .1;
if(distanceBrake<5 && distanceClutch >=5){
speed-= 5/distanceBrake;
}
}
if(speed<=gearspeed*.125){
digitalWrite(LED0, LOW);
}else{
digitalWrite(LED0, HIGH);
}
if(speed<=gearspeed*.25){
digitalWrite(LED1, LOW);
}else{
digitalWrite(LED1, HIGH);
}
if(speed<=gearspeed*.35){
digitalWrite(LED2, LOW);
}else{
digitalWrite(LED2, HIGH);
}
if(speed<=gearspeed*.5){
digitalWrite(LED3, LOW);
}else{
digitalWrite(LED3, HIGH);
}
if(speed<=gearspeed*.7){
digitalWrite(LED4, LOW);
}else{
digitalWrite(LED4, HIGH);
}
if(speed<=gearspeed*.85){
digitalWrite(LED5, LOW);
}else{
digitalWrite(LED5, HIGH);
}
if(speed<=gearspeed*.95){
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
}else{
digitalWrite(LED6, HIGH);
digitalWrite(LED7, HIGH);
}
}
//Crash condition. Speed only goes negative when manually set within the gear If loops
if(speed<-50){
lcd.setCursor(6,1);
lcd.print("CRASHED ");
speed = 500;
noTone(BUZZ);
speed-=1000;
time--;
noTone(BUZZ);
}
//Win condition
if(speed >= 300){
lcd.setCursor(6,1);
lcd.print("WIN ");
speed = 500;
noTone(BUZZ);
speed+=1000;
time--;
noTone(BUZZ);
}
}else{
//System is off
speed = 0;
time = 0;
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
lcd.setCursor(6,0);
lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(" ");
gear = 1;
noTone(BUZZ);
}
}
Comments