// C++ code
//--Shifter----
#define N
#define R 33
#define L 25
#define gearKnob 32
#define Throttle 34
#define gas 2
#define pushStart 6
#define joysticklevel 35
//these are the files we need to include for our display
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Bounce2.h>
#include <SPI.h>
#include <Wire.h>
#define screen_width 128
#define screen_height 64
Adafruit_SSD1306 display(screen_width, screen_height);
//need this to be a global
int speed;
float gasLevel;
float adjSpeed; //this is the speed after we adjust it. because if we use raw speed we cant account for the gears
char gearText[5][1+1] = {"P","R", "N", "D","L"};
void setupShifter(void);
void setupThrottle(void);
void intPushStart(void);
void checkpushStart(void);
void Screen(void);
void intScreen(void);//this will set up color, pins, etc.
void intGas(void);
void speedControl(void);
void joyStick(void);
void setup()
{
Serial.begin(9600);
setupShifter();
setupThrottle();
intScreen();
intGas();
intPushStart();
}
void loop()
{
// switching to free built in freertos
Screen();
speedControl();
int checkGear;
gasLevel = analogRead(gas);//need to complete the math on this GasLevel should be read as a percentage.
speed = analogRead(Throttle)/10; //this reads speed all we need to do is add math lol
if (getCurrentGear() ==1)
Serial.println("Car in Neutral");
else if (getCurrentGear() ==2)
Serial.println("Car in Reverse");
else if (getCurrentGear() ==3)
Serial.println("Car in Low");
else if (getCurrentGear() ==4)
Serial.println("Car is in Drive");
else
{
Serial.println("Car is in park");
}
}
/*
set up the shifter
*/
void setupShifter(void)
{
pinMode(gearKnob, INPUT);
pinMode(R, INPUT);
pinMode(L, INPUT);
}
//these are a pot inputs -----------------------------------------------------------
//pedal
void setupThrottle(void)
{
pinMode(Throttle, INPUT);
}
//gas
void intGas(void)
{
pinMode(Throttle, INPUT);
}
void intPushStart(void)
{
pinMode(pushStart, INPUT);
}
//starts screen addresses it to 0x3c
void intScreen(void)
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
}
void Screen(void)
{
//speed of car
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print(adjSpeed);
//fuel of car
display.setTextSize(2);
display.setCursor(0,50);
display.print(gasLevel);
//gear the car is in
display.setTextSize(2);
display.setCursor(0,25);
display.print(gearText[getCurrentGear()]);
display.display();
}
//set takes the data of current gear
int getCurrentGear()
{
int theGear;
int gearKnobValue = analogRead(gearKnob);
if (gearKnobValue<819)
theGear = 0;
else if(gearKnobValue<1638)
theGear = 1;
else if(gearKnobValue<2457)
theGear = 2;
else if(gearKnobValue<3276)
theGear = 3;
else
theGear = 4;
return theGear;
}
//we are using this to control the actual speed of the car independent of the gas peddle
void speedControl(void)
{
//going to use an els if tree do reduce process time.
if(getCurrentGear() == 3 && speed > 30)
{
adjSpeed = 30;
}
else if(getCurrentGear() == 1)
{
adjSpeed = 0;
}
else if(getCurrentGear() == 2 && speed > 50)
{
adjSpeed = 50;
}
else
{
adjSpeed = speed;
}
}
/*1. The gas peddle input is a potentiometer input with a voltage range of 0 – 5 volts
to the controller.
done
2. The controller interprets the gas peddle input as a request to set the vehicle’s
velocity in MPH where 5 volts is interpreted as 100 MPH.
need to add the math but as good as done
3. The fuel tank input is also a potentiometer with a 0-3.3 volts input.
need to switch it over to 3.3v
4. The fuel tank holds 18 Gallons of fuel when full, where 3.3 represents a full tank.
not added
5. The vehicle’s automatic transmission can handle the following,
a. Neutral – 0 MPH allowed
b. Low – up to 30 MPH allowed
c. Drive – up to 100 MHP allowed
d. Reverse – up to 50 MPH allowed
added
6. There is key start to the vehicle, represented by a momentary push button.
not added but i would like to replace it with an rfid in the future
7. Pressing the Start button a 2nd time on the fixture will cause the vehicle to
decelerate and stop.
not added
8. When the vehicle is started there is a display that shows
a. Amount of fuel in the vehicle
b. The velocity of the vehicle
c. The gear the transmission is in
the display is set up but have not yet added gears
9. If the controller detects a Stop while the vehicle is moving the display should
show a deceleration and a waring message that the vehicle was turned off while
in motion.
not added
10. The display should display a waring message if the fuel drops below ¼ of a tank.
not added
*/