Corvette C6 Simulation

by Biaver_Instructables in Circuits > Arduino

115 Views, 0 Favorites, 0 Comments

Corvette C6 Simulation

Screenshot 2024-01-22 125624.png

So do you want to make a driving simulator on an Arduino?

Supplies

Screenshot 2024-01-22 125759.png

You will need:

Arduino Uno R3 x1 PCF8574-based x1,

32 LCD 16 x 2 (I2C) x1

Pushbuttons x3

250kΩ Potentiometer x1

RGB LED (Anode) x1

Piezo Buzzers x1

330Ω resistor x3

1kΩ resistor x1

Breadboard x1

Wires

Breadboard and Arduino Placement

unnamed.jpg
PXL_20240118_001018903.MP.jpg

Place the Breadboard as shown.

Place the Arduino to the left of the breadboard with the USB facing away from you as shown.

Connecting the LCD

PXL_20240118_001048596.jpg
PXL_20240118_001117950.MP.jpg
PXL_20240118_004609805.jpg

Place the LCD as shown

Connect male-to-female jumper wires (if you have a female-to-female, add a small normal jumper wire as shown) as shown

Connect the SCL wire to the first pin on the digital pin side, connect the SDA wire to the second pin on the digital pin side, and connect power and ground to the power and ground terminals on the breadboard as shown.

Connecting the Inputs

PXL_20240118_004743732.MP.jpg
PXL_20240118_011058167.MP.jpg

Add two push buttons to the left side of the breadboard, then add a pushbutton and a potentiometer to the right side as shown.

Connect power and ground to each of the inputs. Use a 330Ω resistor and a small red jumper wire to connect the push buttons to the power and ground terminal. Use a long green wire to connect the middle pin of the potentiometer to the A3 pin on the Arduino. Finally use a small red and a small black jumper wire to connect the potentiometer to power and ground terminal as shown.

Connecting the Output

PXL_20240118_013621739.MP.jpg
PXL_20240118_013959169.jpg

Add a piezo buzzer to the right side and a RGB LED to the left side of the breadboard as shown.

The RGB LED you should be using is anode and so connect the common pin using a 1kΩ resistor to the power terminal. Then connect: red pin to 6, green pin to 5, and blue pin to 3. Connect the positive side of the buzzer to 9 and the negative side to the negative terminal as shown.

Connecting Power and Ground

PXL_20240118_014107723.MP.jpg

Connect the top and bottom power and ground terminals together using medium sized red and black jumper wires. Finally connect the bottom power and ground to 5V and GND pin on the Arduino with long red and a long black wire as shown.

Installing the Library

Screenshot 2024-01-22 173311.png

Go to your Arduino IDE, if you don't have it then download it from here Software | Arduino.

If you do have your IDE set up then install the <LiquidCrystal_I2C.h> library to your IDE.

Initialize the library and call it lcd like so, and then add the LCD hexadecimal address and its resolution of 16 x 2.

Initializing the Variables

Screenshot 2024-01-22 173340.png

Now Initialize these variables. const int variables are used to initialize the pins connected to the Arduino, float variables are used to calculate the math involved in the simulation, the array is to store the gears and gear ratios in the simulation.

Initializing the Sprites

New Project.jpg

Initialize the sprite that will be displayed on the screen.

A byte stores 8 bit sized information (in this case sprites).

Bytes: zeroK, oneK, twoK, threeK, and fourK display the bars on the tachometer we will be displaying.

Bytes: GE, and AR are shortening of the normal ASCII characters that can be displayed on the LCD.

Setting Up the Pins

Screenshot 2024-01-22 174258.png

In void setup, setup the pins using this code.

Setup the input pins as INPUT and the output pins as OUTPUT.

Since the RGB LED is common anode, you need to write to the pin a value of 255 instead of 0.

Initializing What Is Being Displayed to the LCD

Screenshot 2024-01-22 174420.png

Continue in void setup initialize the LCD, generate the custom sprites and assign them a index to call them later.

Then clear, turn on the backlight, and turn on the display of the LCD.

The final step is optional but create a start up screen to smoothly transition to the main screen.

Adding Analog Input Handler

Screenshot 2024-01-22 174557.png

In void loop write these 3 line of code to handle the analog input going into the Arduino.

Map the raw analog input from 0 - 1023 to 500 - 7000. Scale these values depending on how much input is read from the Arduino and what gear you are currently in.

Write a similar line of code to the light going to the RGB LED but with it mapping between 0 - 255.

Calculating RPM

Screenshot 2024-01-22 174654.png

If the RPM is less than throttle input then increase RPM by throttle scalar, analogWrite to the green pin a value of the difference of 255 and throttle scalar.

If the RPM is greater than throttle input then equate RPM to throttle input and analogWrite to the green pin a value of 0.

All values are the opposite if RPM is first checked to be less than throttle input.

Check for Gear Shifting

Screenshot 2024-01-22 174722.png

If the pin gear up is HIGH then increase i index number by one; analogWrite to the blue pin 0; set RPM to: (RPM x gear previous i index number) / gear current i index number; delay for 500 ms; and finally analogWrite to the blue pin 255.

Flip the addition and subtraction signs for if the pin gear down is HIGH.

If the RPM is greater than 7000, then set it to 7000.

Calculating Max Velocity

Screenshot 2024-01-22 174853.png

If gear is neutral (gear 0) then max velocity equates to 0.

Else then max velocity equates to ((RPM x 60 x 20.3 x 3.141526) / (the current gear ratio x 3.15)) / 63360.

Calculating Torque Output

Screenshot 2024-01-22 174903.png

Torque is defined by this quadratic equation: (-8 * 10^-6) * x^2 + (-6.88 * 10^-2) * x + 162.1.

Torque at wheels is defined as: Torque x current gear ratio x 3.15.

Force at wheel is defined as Force at wheels / ((20.3 / 2) / 12)

Check for Acceleration

Screenshot 2024-01-22 174915.png

If the Force at wheels is greater than 2400 or velocity is non-zero and current gear is not neutral, then acceleration equates to ((Force at wheels / 4319) * 16.1) / current gear.

Else acceleration equates to 0.

Calculating Velocity

Screenshot 2024-01-22 174926.png

This code is similar to Step 12 since they both use the same principles and logic.

Check for Brakes

Screenshot 2024-01-22 174949.png

If the brakes is HIGH then: brake force equates to -10, velocity decreases by brake force, RPM decreases by brake force x 100, and finally analogWrite to red pin a value of 0.

If velocity is less than 0, then velocity equates to 0

If RPM is less than 500, then velocity equates to 500.

Else brake force equates to 0 and analogWrite to red pin a value of 255.

Calculating the Pitch of the Buzzer

Screenshot 2024-01-22 174956.png

Map pitch to RPM (values between 500 - 7000) between the values of 40 - 200

Send tone to the buzzer at pin buzzer, set pitch to pitch variable, and play the tone for 100 ms.

Displaying the Gear

Screenshot 2024-01-22 175023.png

Set the LCD cursor to position (0, 1).

If current gear equates to 0, then print to the LCD "N".

Else print to the LCD the current gear.

Displaying the Velocity

Screenshot 2024-01-22 175042.png

If velocity is less than or equates to 9, then set cursor to (13, 1); print " "; and finally print the integer value of RPM.

Else if velocity is less than or equates to 99 and is greater than or equates to 10, then set cursor to (13, 1); print " "; and finally print the integer value of RPM

Else if velocity is greater than or equates to 100, then set cursor to (13, 1); print the integer value of RPM.

Displaying the Tachometer

Screenshot 2024-01-22 175145.png
Screenshot 2024-01-22 175214.png
Screenshot 2024-01-22 175227.png
Screenshot 2024-01-22 175239.png
Screenshot 2024-01-22 175300.png
Screenshot 2024-01-22 175317.png
Screenshot 2024-01-22 175331.png
Screenshot 2024-01-22 175404.png

This is the hardest part of this Instructable. Consider each if statement as a single frame and the LCD is cycling through each frame as it is being run. Each if statement discreetly and literally prints (or I should say writes) each custom character that needs to be displayed at a certain time and position. Write the code appropriately.

And You're Done!

Screenshot 2024-01-22 125624.png

If you managed to finished the Instructable up to this point, then you should have a fully working Driving Simulation