Advertisement
Guest User

carDiagnostics

a guest
Apr 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.36 KB | None | 0 0
  1. /*
  2.    Boris Strahilov
  3.    ART 150 - Spring 2017
  4.  
  5.    The main library used is OBD.h which is provided
  6.    by the creator of the OBD adaptor. It allows for
  7.    reading in realtime data from the vehicle.
  8. */
  9. #include <Wire.h>
  10. #include <OBD.h>
  11. #include <LiquidCrystal.h>
  12.  
  13. COBD obd; /* for Model A (UART version) */
  14.  
  15. /*
  16.    State will represent what information is curretly displayed
  17.     0 - RPM
  18.     1 - Engine reference toruqe (Nm)
  19.     2 - Engine Calculated Load %
  20.     3 - Intake Temp (C)
  21.     4 - Engine oil temp (C)
  22. */
  23. int state;
  24.  
  25. //this changes between different modes of each state
  26. int mode;
  27.  
  28. //this button will change which state
  29. int stateBtn = 3;
  30.  
  31. //this button will change the current state's display;
  32. int modeBtn = 4;
  33.  
  34. //this variable holds the various info from the car's obd
  35. int value;
  36.  
  37. // initialize the library with the numbers of the interface pins
  38. LiquidCrystal lcd(13, 12, 11, 10, 9, 8);
  39.  
  40. //the pins that are needed for the RGB LED
  41. int redPin = 7;
  42. int greenPin = 6;
  43. int bluePin = 5;
  44.  
  45. void setup()
  46. {
  47.   //set up each wire of the RGB pin as an output
  48.   pinMode(redPin, OUTPUT);
  49.   pinMode(greenPin, OUTPUT);
  50.   pinMode(bluePin, OUTPUT);
  51.  
  52.   //set up as input pullup, so 0 will be pressed
  53.   pinMode(stateBtn, INPUT_PULLUP);
  54.   pinMode(modeBtn, INPUT_PULLUP);
  55.  
  56.   // set up the LCD's number of columns and rows:
  57.   lcd.begin(16, 2);
  58.  
  59.   //set default runtime, state, and mode to 0
  60.   state = 0;
  61.   mode = 0;
  62.  
  63.   //display string until connetion succeeds
  64.   lcd.setCursor(0, 0);
  65.   lcd.print("Connecting OBD..");
  66.   delay(1000);
  67.  
  68.   // start communication with OBD-II adapter
  69.   obd.begin();
  70.   // initiate OBD-II connection until success
  71.   while (!obd.init());
  72.  
  73.   lcd.clear();
  74.   lcd.setCursor(0, 0);
  75.   lcd.print("Success!");
  76.  
  77.   delay(1000);
  78.   welcomeScreen();
  79. }
  80. //white with tx green with rx
  81.  
  82. //do a start up animation
  83. void welcomeScreen() {
  84.  
  85.   //first fill top line with bars
  86.   for (int i = 0; i < 16; i++) {
  87.     lcd.setCursor(i, 0);
  88.     lcd.write(255);
  89.     delay(50);
  90.   }
  91.  
  92.   //next fill top line with bars
  93.   for (int i = 0; i < 16; i++) {
  94.     lcd.setCursor(15 - i, 1);
  95.     lcd.write(255);
  96.     delay(50);
  97.   }
  98.  
  99.   //now remove blocks
  100.   for (int i = 0; i < 16; i++) {
  101.  
  102.     //this is used to display things
  103.     //from the right side because
  104.     //having (15-i) in a switch statement doesnt work
  105.     int reverse_i = 15 - i;
  106.  
  107.     lcd.setCursor(i, 0);
  108.  
  109.     //1st row
  110.     //case for displaying "Welcome,"
  111.     switch (i) {
  112.       case 4:
  113.         lcd.print("W");
  114.         break;
  115.       case 5:
  116.         lcd.print("E");
  117.         break;
  118.       case 6:
  119.         lcd.print("L");
  120.         break;
  121.       case 7:
  122.         lcd.print("C");
  123.         break;
  124.       case 8:
  125.         lcd.print("O");
  126.         break;
  127.       case 9:
  128.         lcd.print("M");
  129.         break;
  130.       case 10:
  131.         lcd.print("E");
  132.         break;
  133.       case 11:
  134.         lcd.print(",");
  135.         break;
  136.       default:
  137.         lcd.write(" ");
  138.         break;
  139.     };
  140.  
  141.     //2nd row
  142.     lcd.setCursor(reverse_i, 1);
  143.     //case for displaying "BORIS"
  144.     switch (reverse_i) {
  145.       case 6:
  146.         lcd.print("B");
  147.         break;
  148.       case 7:
  149.         lcd.print("O");
  150.         break;
  151.       case 8:
  152.         lcd.print("R");
  153.         break;
  154.       case 9:
  155.         lcd.print("I");
  156.         break;
  157.       case 10:
  158.         lcd.print("S");
  159.         break;
  160.       default:
  161.         lcd.print(" ");
  162.         break;
  163.     };
  164.     delay(75);
  165.   }
  166.  
  167.   delay(2000);
  168. }
  169.  
  170. //convert celcius to fahrenheit
  171. float C_to_F(float celcius) {
  172.   return (float) ( (float)( celcius * 1.8 ) + 32  );
  173. }
  174.  
  175. //convert km/h to mph
  176. int KMH_to_MPH(int speed) {
  177.   return (float)speed * 0.621371;
  178. }
  179.  
  180. //this sets the color of the RGB LED
  181. //to the desired one using the standard rgb format
  182. void setColor(int red, int green, int blue) {
  183. #ifdef COMMON_ANODE
  184.   red = 255 - red;
  185.   green = 255 - green;
  186.   blue = 255 - blue;
  187. #endif
  188.   analogWrite(redPin, red);
  189.   analogWrite(greenPin, green);
  190.   analogWrite(bluePin, blue);
  191. }
  192.  
  193.  
  194. //main function which will get the data needed and
  195. // display it to the LCD
  196. void getData(int state, int mode) {
  197.   //RPM
  198.   if (state == 0) {
  199.     //get data from car
  200.     obd.readPID(PID_RPM, value);
  201.  
  202.     //set cursor to top left
  203.     lcd.setCursor(0, 0);
  204.     lcd.print("RPM:");
  205.  
  206.     lcd.setCursor(5, 0);
  207.     lcd.print(value);
  208.  
  209.     //2nd line displays RPM as a percent from red line
  210.     //which is 6500 for our testing
  211.     float percentDecimal = (float)((float)value / (float)6500);
  212.  
  213.     //16 slots for second line so we gotta convert
  214.     //the percent into a number from 1-16
  215.     float numBarsF = 16 * percentDecimal; //get as float
  216.     int numBars = numBarsF; //convert to int
  217.  
  218.     //actually print the bars
  219.     for (int i = 0; i < numBars; i++) {
  220.       lcd.setCursor(i, 1);
  221.       lcd.write(255);
  222.     }
  223.  
  224.     /*
  225.         For the modes, there will be 3
  226.         0 - comfort (2000 RPM shifts)
  227.         1 - standard (3000 RPM shifts)
  228.         2 - race (redline shifts)
  229.     */
  230.  
  231.     //comfort
  232.     if (mode == 0) {
  233.       //if rpm is within the range, the light
  234.       //turns on with a green color
  235.       if (value > 1800 && value < 2200) {
  236.         setColor(219, 186, 19);
  237.       } else {
  238.         setColor(0, 0, 0);
  239.       }
  240.  
  241.       lcd.setCursor(10, 0);
  242.       lcd.print("COMF");
  243.     }//end if mode = 0
  244.     //standard
  245.     if (mode == 1) {
  246.       //if rpm is within the range, the light
  247.       //turns on with a orange color
  248.       if (value > 2800 && value < 3200) {
  249.         setColor(219, 186, 19);
  250.       } else {
  251.         setColor(0, 0, 0);
  252.       }
  253.  
  254.       lcd.setCursor(10, 0);
  255.       lcd.print("STND");
  256.     }//end if mode = 1
  257.     //race
  258.     if (mode == 2) {
  259.  
  260.       if (value > 5800) {
  261.         setColor(255, 0, 0);
  262.       } else {
  263.         setColor(0, 0, 0);
  264.       }
  265.  
  266.       lcd.setCursor(10, 0);
  267.       lcd.print("RACE");
  268.     }//end if mode = 2
  269.  
  270.  
  271.  
  272.   }//end RPM
  273.  
  274.   //Speed
  275.   else if (state == 1) {
  276.  
  277.     //turn off LED
  278.     setColor(0, 0, 0);
  279.  
  280.     //get data from car
  281.     obd.readPID(PID_SPEED, value);
  282.  
  283.     //mode 0 is for km/h
  284.     if (!mode) {
  285.       //set cursor to top left
  286.       lcd.setCursor(0, 0);
  287.       lcd.print("SPEED:");
  288.  
  289.       //print actual value
  290.       lcd.setCursor(0, 1);
  291.       lcd.print(value);
  292.  
  293.       //print NM
  294.       lcd.print(" KM/H");
  295.     }
  296.  
  297.     //mode 1 is for mph
  298.     else {
  299.  
  300.       //convert value
  301.       value = KMH_to_MPH(value);
  302.  
  303.       //set cursor to top left
  304.       lcd.setCursor(0, 0);
  305.       lcd.print("SPEED:");
  306.  
  307.       //print actual value
  308.       lcd.setCursor(0, 1);
  309.       lcd.print(value);
  310.  
  311.       //print NM
  312.       lcd.print(" MPH");
  313.     }
  314.  
  315.  
  316.   }//end speed
  317.  
  318.   //Engine  Load
  319.   else if (state == 2) {
  320.     //turn off LED
  321.     setColor(0, 0, 0);
  322.  
  323.     //get data from car
  324.     obd.readPID(PID_ENGINE_LOAD, value);
  325.  
  326.     //set cursor to top left
  327.     lcd.setCursor(0, 0);
  328.     lcd.print("ENGINE LOAD:");
  329.  
  330.     //actual percent
  331.     lcd.setCursor(13, 0);
  332.     lcd.print(value);
  333.  
  334.     //print %
  335.     lcd.print("%");
  336.  
  337.     //2nd line will be used as a visual representation
  338.     //of the % amount once again
  339.     float percentDecimal = (float)value / 100; //get as float
  340.     float numBarsF = 16 * percentDecimal; //get as float
  341.     int numBars = numBarsF; //convert to int
  342.  
  343.     //actually print the bars
  344.     for (int i = 0; i < numBars; i++) {
  345.       lcd.setCursor(i, 1);
  346.       lcd.write(255);
  347.     }
  348.  
  349.  
  350.   }//end calc load
  351.  
  352.   //Intake Temp
  353.   else if (state == 3) {
  354.     //turn off LED
  355.     setColor(0, 0, 0);
  356.  
  357.     //get data from car
  358.     obd.readPID(PID_INTAKE_TEMP, value);
  359.  
  360.     //set cursor to top left
  361.     lcd.setCursor(0, 0);
  362.     lcd.print("INTAKE TEMP:");
  363.  
  364.     //default value read in is in celcius so
  365.     //if mode=0 display it like so
  366.     if (!mode) {
  367.       lcd.setCursor(0, 1);
  368.       lcd.print((float)value);
  369.  
  370.       //print C
  371.       lcd.print(" C");
  372.       lcd.write(223);
  373.     } else {
  374.       lcd.setCursor(0, 1);
  375.       lcd.print(C_to_F((float)value));
  376.  
  377.       //print F
  378.       lcd.print(" F");
  379.       lcd.write(223);
  380.     }
  381.   }//end intake Temp
  382.  
  383.   //Oil Temp
  384.   else if (state == 4) {
  385.  
  386.     //turn off LED
  387.     setColor(0, 0, 0);
  388.  
  389.     //get data from car
  390.     obd.readPID(PID_INTAKE_TEMP, value);
  391.  
  392.     //set cursor to top left
  393.     lcd.setCursor(0, 0);
  394.     lcd.print("OIL TEMP:");
  395.  
  396.     //default value read in is in celcius so
  397.     //if mode=0 display it like so
  398.     if (!mode) {
  399.       lcd.setCursor(0, 1);
  400.       lcd.print((float)value);
  401.  
  402.       //print C
  403.       lcd.print(" C");
  404.       lcd.write(223);
  405.  
  406.     } else {
  407.       lcd.setCursor(0, 1);
  408.       lcd.print(C_to_F((float)value));
  409.  
  410.       //print F
  411.       lcd.print(" F");
  412.       lcd.write(223);
  413.  
  414.     }
  415.   }//end oil temp
  416.  
  417. }//end getData
  418.  
  419. void loop()
  420. {
  421.  
  422.   //if the state button is pressed
  423.   if (digitalRead(stateBtn) == 0 ) {
  424.     //increment state
  425.     state++;
  426.  
  427.     //loop back from state 4 to 0
  428.     if (state > 4)
  429.       state = 0;
  430.  
  431.     //do a small delay
  432.     delay(300);
  433.   }
  434.  
  435.   //if mode button is pressed
  436.   if (digitalRead(modeBtn) == 0 ) {
  437.     //increment mode
  438.     mode++;
  439.  
  440.     //loop back from mode 1 to 0
  441.     if (mode > 1) {
  442.  
  443.       //if not RPM, then there are only 2 modes
  444.       //otherwise for RPM, there are 3 modes
  445.       if (state != 0)
  446.         mode = 0;
  447.       else {
  448.         if (mode > 2)
  449.           mode = 0;
  450.       }
  451.  
  452.     }
  453.     //do a small delay
  454.     delay(300);
  455.   }
  456.  
  457.   lcd.clear();
  458.   getData(state, mode);
  459.  
  460.   delay(250);
  461.  
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement