Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Boris Strahilov
- ART 150 - Spring 2017
- The main library used is OBD.h which is provided
- by the creator of the OBD adaptor. It allows for
- reading in realtime data from the vehicle.
- */
- #include <Wire.h>
- #include <OBD.h>
- #include <LiquidCrystal.h>
- COBD obd; /* for Model A (UART version) */
- /*
- State will represent what information is curretly displayed
- 0 - RPM
- 1 - Engine reference toruqe (Nm)
- 2 - Engine Calculated Load %
- 3 - Intake Temp (C)
- 4 - Engine oil temp (C)
- */
- int state;
- //this changes between different modes of each state
- int mode;
- //this button will change which state
- int stateBtn = 3;
- //this button will change the current state's display;
- int modeBtn = 4;
- //this variable holds the various info from the car's obd
- int value;
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
- //the pins that are needed for the RGB LED
- int redPin = 7;
- int greenPin = 6;
- int bluePin = 5;
- void setup()
- {
- //set up each wire of the RGB pin as an output
- pinMode(redPin, OUTPUT);
- pinMode(greenPin, OUTPUT);
- pinMode(bluePin, OUTPUT);
- //set up as input pullup, so 0 will be pressed
- pinMode(stateBtn, INPUT_PULLUP);
- pinMode(modeBtn, INPUT_PULLUP);
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- //set default runtime, state, and mode to 0
- state = 0;
- mode = 0;
- //display string until connetion succeeds
- lcd.setCursor(0, 0);
- lcd.print("Connecting OBD..");
- delay(1000);
- // start communication with OBD-II adapter
- obd.begin();
- // initiate OBD-II connection until success
- while (!obd.init());
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Success!");
- delay(1000);
- welcomeScreen();
- }
- //white with tx green with rx
- //do a start up animation
- void welcomeScreen() {
- //first fill top line with bars
- for (int i = 0; i < 16; i++) {
- lcd.setCursor(i, 0);
- lcd.write(255);
- delay(50);
- }
- //next fill top line with bars
- for (int i = 0; i < 16; i++) {
- lcd.setCursor(15 - i, 1);
- lcd.write(255);
- delay(50);
- }
- //now remove blocks
- for (int i = 0; i < 16; i++) {
- //this is used to display things
- //from the right side because
- //having (15-i) in a switch statement doesnt work
- int reverse_i = 15 - i;
- lcd.setCursor(i, 0);
- //1st row
- //case for displaying "Welcome,"
- switch (i) {
- case 4:
- lcd.print("W");
- break;
- case 5:
- lcd.print("E");
- break;
- case 6:
- lcd.print("L");
- break;
- case 7:
- lcd.print("C");
- break;
- case 8:
- lcd.print("O");
- break;
- case 9:
- lcd.print("M");
- break;
- case 10:
- lcd.print("E");
- break;
- case 11:
- lcd.print(",");
- break;
- default:
- lcd.write(" ");
- break;
- };
- //2nd row
- lcd.setCursor(reverse_i, 1);
- //case for displaying "BORIS"
- switch (reverse_i) {
- case 6:
- lcd.print("B");
- break;
- case 7:
- lcd.print("O");
- break;
- case 8:
- lcd.print("R");
- break;
- case 9:
- lcd.print("I");
- break;
- case 10:
- lcd.print("S");
- break;
- default:
- lcd.print(" ");
- break;
- };
- delay(75);
- }
- delay(2000);
- }
- //convert celcius to fahrenheit
- float C_to_F(float celcius) {
- return (float) ( (float)( celcius * 1.8 ) + 32 );
- }
- //convert km/h to mph
- int KMH_to_MPH(int speed) {
- return (float)speed * 0.621371;
- }
- //this sets the color of the RGB LED
- //to the desired one using the standard rgb format
- void setColor(int red, int green, int blue) {
- #ifdef COMMON_ANODE
- red = 255 - red;
- green = 255 - green;
- blue = 255 - blue;
- #endif
- analogWrite(redPin, red);
- analogWrite(greenPin, green);
- analogWrite(bluePin, blue);
- }
- //main function which will get the data needed and
- // display it to the LCD
- void getData(int state, int mode) {
- //RPM
- if (state == 0) {
- //get data from car
- obd.readPID(PID_RPM, value);
- //set cursor to top left
- lcd.setCursor(0, 0);
- lcd.print("RPM:");
- lcd.setCursor(5, 0);
- lcd.print(value);
- //2nd line displays RPM as a percent from red line
- //which is 6500 for our testing
- float percentDecimal = (float)((float)value / (float)6500);
- //16 slots for second line so we gotta convert
- //the percent into a number from 1-16
- float numBarsF = 16 * percentDecimal; //get as float
- int numBars = numBarsF; //convert to int
- //actually print the bars
- for (int i = 0; i < numBars; i++) {
- lcd.setCursor(i, 1);
- lcd.write(255);
- }
- /*
- For the modes, there will be 3
- 0 - comfort (2000 RPM shifts)
- 1 - standard (3000 RPM shifts)
- 2 - race (redline shifts)
- */
- //comfort
- if (mode == 0) {
- //if rpm is within the range, the light
- //turns on with a green color
- if (value > 1800 && value < 2200) {
- setColor(219, 186, 19);
- } else {
- setColor(0, 0, 0);
- }
- lcd.setCursor(10, 0);
- lcd.print("COMF");
- }//end if mode = 0
- //standard
- if (mode == 1) {
- //if rpm is within the range, the light
- //turns on with a orange color
- if (value > 2800 && value < 3200) {
- setColor(219, 186, 19);
- } else {
- setColor(0, 0, 0);
- }
- lcd.setCursor(10, 0);
- lcd.print("STND");
- }//end if mode = 1
- //race
- if (mode == 2) {
- if (value > 5800) {
- setColor(255, 0, 0);
- } else {
- setColor(0, 0, 0);
- }
- lcd.setCursor(10, 0);
- lcd.print("RACE");
- }//end if mode = 2
- }//end RPM
- //Speed
- else if (state == 1) {
- //turn off LED
- setColor(0, 0, 0);
- //get data from car
- obd.readPID(PID_SPEED, value);
- //mode 0 is for km/h
- if (!mode) {
- //set cursor to top left
- lcd.setCursor(0, 0);
- lcd.print("SPEED:");
- //print actual value
- lcd.setCursor(0, 1);
- lcd.print(value);
- //print NM
- lcd.print(" KM/H");
- }
- //mode 1 is for mph
- else {
- //convert value
- value = KMH_to_MPH(value);
- //set cursor to top left
- lcd.setCursor(0, 0);
- lcd.print("SPEED:");
- //print actual value
- lcd.setCursor(0, 1);
- lcd.print(value);
- //print NM
- lcd.print(" MPH");
- }
- }//end speed
- //Engine Load
- else if (state == 2) {
- //turn off LED
- setColor(0, 0, 0);
- //get data from car
- obd.readPID(PID_ENGINE_LOAD, value);
- //set cursor to top left
- lcd.setCursor(0, 0);
- lcd.print("ENGINE LOAD:");
- //actual percent
- lcd.setCursor(13, 0);
- lcd.print(value);
- //print %
- lcd.print("%");
- //2nd line will be used as a visual representation
- //of the % amount once again
- float percentDecimal = (float)value / 100; //get as float
- float numBarsF = 16 * percentDecimal; //get as float
- int numBars = numBarsF; //convert to int
- //actually print the bars
- for (int i = 0; i < numBars; i++) {
- lcd.setCursor(i, 1);
- lcd.write(255);
- }
- }//end calc load
- //Intake Temp
- else if (state == 3) {
- //turn off LED
- setColor(0, 0, 0);
- //get data from car
- obd.readPID(PID_INTAKE_TEMP, value);
- //set cursor to top left
- lcd.setCursor(0, 0);
- lcd.print("INTAKE TEMP:");
- //default value read in is in celcius so
- //if mode=0 display it like so
- if (!mode) {
- lcd.setCursor(0, 1);
- lcd.print((float)value);
- //print C
- lcd.print(" C");
- lcd.write(223);
- } else {
- lcd.setCursor(0, 1);
- lcd.print(C_to_F((float)value));
- //print F
- lcd.print(" F");
- lcd.write(223);
- }
- }//end intake Temp
- //Oil Temp
- else if (state == 4) {
- //turn off LED
- setColor(0, 0, 0);
- //get data from car
- obd.readPID(PID_INTAKE_TEMP, value);
- //set cursor to top left
- lcd.setCursor(0, 0);
- lcd.print("OIL TEMP:");
- //default value read in is in celcius so
- //if mode=0 display it like so
- if (!mode) {
- lcd.setCursor(0, 1);
- lcd.print((float)value);
- //print C
- lcd.print(" C");
- lcd.write(223);
- } else {
- lcd.setCursor(0, 1);
- lcd.print(C_to_F((float)value));
- //print F
- lcd.print(" F");
- lcd.write(223);
- }
- }//end oil temp
- }//end getData
- void loop()
- {
- //if the state button is pressed
- if (digitalRead(stateBtn) == 0 ) {
- //increment state
- state++;
- //loop back from state 4 to 0
- if (state > 4)
- state = 0;
- //do a small delay
- delay(300);
- }
- //if mode button is pressed
- if (digitalRead(modeBtn) == 0 ) {
- //increment mode
- mode++;
- //loop back from mode 1 to 0
- if (mode > 1) {
- //if not RPM, then there are only 2 modes
- //otherwise for RPM, there are 3 modes
- if (state != 0)
- mode = 0;
- else {
- if (mode > 2)
- mode = 0;
- }
- }
- //do a small delay
- delay(300);
- }
- lcd.clear();
- getData(state, mode);
- delay(250);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement