mario6996

OBD2 arduino - to correct

Feb 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. // include the library code:<br>#include
  2. //Declare Serial Read variables
  3. #include <LiquidCrystal.h>
  4. int CmdCount=1;
  5. byte inData;
  6. char inChar;
  7. String BuildINString="";
  8. String DisplayString="";
  9. long DisplayValue;
  10. String SentMessage="";
  11. int ByteCount=0;
  12. long A;
  13. int B;
  14. int WorkingVal;
  15. String WorkingString="";
  16. //Declare Buttons
  17. int MenuID=0;
  18. // initialize the library with the numbers of the interface pins
  19. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  20. void setup()
  21. {
  22. // set up the LCD's number of columns and rows:
  23. lcd.begin(16, 2);
  24. // Print a message to the LCD.
  25. lcd.setCursor(0, 0);
  26. Bootup();//Simulate Bootup process doesnt have to be here this only makes it look cool
  27. Retry:
  28. lcd.setCursor(0, 0);
  29. lcd.print("Connecting...... ");
  30. lcd.setCursor(0, 1);
  31. lcd.print(" ");
  32. Serial.begin(38400);
  33.  
  34. delay(500);
  35. //Send a test message to see if the ELM327 Chip is responding
  36. SentMessage = "ATI";
  37. Serial.println("ATI");delay(500);ReadData();
  38. if (BuildINString.substring(1,7)=="ELM327")
  39. {
  40. lcd.setCursor(0, 0);
  41. lcd.print("Welcome... ");
  42. lcd.setCursor(0, 1);
  43. lcd.print("Connection OK ");
  44. delay(1500);
  45. }
  46. else
  47. {
  48. lcd.setCursor(0, 0);
  49. lcd.print("Error ");
  50. lcd.setCursor(0, 1);
  51. lcd.print("No Connection! ");
  52. delay(1500);
  53. goto Retry;
  54. }
  55.  
  56. delay(1500);
  57. }
  58. void loop() {
  59. int x;
  60. x = analogRead (0);
  61. //Serial.println(x);
  62. lcd.setCursor(10,1);
  63. if (x > 800 and x < 820){lcd.print ("Select ");}
  64. if (x > 620 and x < 630){if (MenuID>0){MenuID--;}delay(250);}//Left
  65. if (x > 400 and x < 415){lcd.print ("Down ");}
  66. if (x > 190 and x < 215){lcd.print ("Up ");}
  67. if (x > -10 and x < 10){if (MenuID<11){MenuID++;}delay(250);} //Right
  68. if (MenuID==0){lcd.setCursor(0, 0);lcd.print("01 Coolant Temp ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 05";Serial.println("01 05");delay(300);ReadData();}
  69. if (MenuID==1){lcd.setCursor(0, 0);lcd.print("02 IAT Temp ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 0F";Serial.println("01 0F");delay(300);ReadData();}
  70. if (MenuID==2){lcd.setCursor(0, 0);lcd.print("03 Ambient Temp ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 46";Serial.println("01 46");delay(300);ReadData();}
  71. if (MenuID==3){lcd.setCursor(0, 0);lcd.print("04 Throttle % ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 11";Serial.println("01 11");delay(300);ReadData();}
  72. if (MenuID==4){lcd.setCursor(0, 0);lcd.print("05 CAT 1 Temp ");lcd.setCursor(0, 1);lcd.print("Not Implemented ");}
  73. if (MenuID==5){lcd.setCursor(0, 0);lcd.print("06 CAT 2 Temp ");lcd.setCursor(0, 1);lcd.print("Not Implemented ");}
  74. if (MenuID==6){lcd.setCursor(0, 0);lcd.print("07 CAT 3 Temp ");lcd.setCursor(0, 1);lcd.print("Not Implemented ");}
  75. if (MenuID==7){lcd.setCursor(0, 0);lcd.print("08 CAT 4 Temp ");lcd.setCursor(0, 1);lcd.print("Not Implemented ");}
  76. if (MenuID==8){lcd.setCursor(0, 0);lcd.print("09 RPM ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 0C";Serial.println("01 0C");delay(300);ReadData();}
  77. if (MenuID==9){lcd.setCursor(0, 0);lcd.print("10 Vehicle Speed ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 0D";Serial.println("01 0D");delay(300);ReadData();}
  78. if (MenuID==10){lcd.setCursor(0, 0);lcd.print("11 Air Flow Rate ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 10";Serial.println("01 10");delay(300);ReadData();}
  79. if (MenuID==11){lcd.setCursor(0, 0);lcd.print("12 Barometric ");lcd.setCursor(0, 1);lcd.print(DisplayString);SentMessage = "01 33";Serial.println("01 33");delay(300);ReadData();}
  80. }
  81. //Read Data and act accordingly
  82. void ReadData()
  83. {
  84. BuildINString="";
  85. while(Serial.available() > 0)
  86. {
  87. inData=0;
  88. inChar=0;
  89. inData = Serial.read();
  90. inChar=char(inData);
  91. BuildINString = BuildINString + inChar;
  92. }
  93.  
  94. //if(BuildINString!=""){Serial.print(BuildINString);}
  95. BuildINString.replace(SentMessage,"");
  96. BuildINString.replace(">","");
  97. BuildINString.replace("OK","");
  98. BuildINString.replace("STOPPED","");
  99. BuildINString.replace("SEARCHING","");
  100. BuildINString.replace("NO DATA","");
  101. BuildINString.replace("?","");
  102. BuildINString.replace(",","");
  103. //Serial.print(BuildINString);
  104.  
  105. //Check which OBD Command was sent and calculate VALUE
  106. //Calculate RPM I.E Returned bytes wil be 41 0C 1B E0
  107. if (SentMessage=="01 0C")
  108. {
  109. WorkingString = BuildINString.substring(7,9);
  110. A = strtol(WorkingString.c_str(),NULL,0);
  111. WorkingString = BuildINString.substring(11,13);
  112. B = strtol(WorkingString.c_str(),NULL,0);
  113.  
  114. DisplayValue = ((A * 256)+B)/4;
  115. DisplayString = String(DisplayValue) + " rpm ";
  116. lcd.setCursor(0, 1);
  117. lcd.print(DisplayString);
  118. }
  119. //Calculate Vehicle speed I.E Returned bytes wil be 41 0C 1B E0
  120. if (SentMessage=="01 0D")
  121. {
  122. WorkingString = BuildINString.substring(7,9);
  123. A = strtol(WorkingString.c_str(),NULL,0);
  124. DisplayValue = A;
  125. DisplayString = String(DisplayValue) + " km/h ";
  126. lcd.setCursor(0, 1);
  127. lcd.print(DisplayString);
  128. }
  129.  
  130. //Coolant Temp
  131. if (SentMessage=="01 05")
  132. {
  133. WorkingString = BuildINString.substring(7,9);
  134. A = strtol(WorkingString.c_str(),NULL,0);
  135. DisplayValue = A;
  136. DisplayString = String(DisplayValue) + " C ";
  137. lcd.setCursor(0, 1);
  138. lcd.print(DisplayString);
  139. }
  140.  
  141. //IAT Temp
  142. if (SentMessage=="01 0F")
  143. {
  144. WorkingString = BuildINString.substring(7,9);
  145. A = strtol(WorkingString.c_str(),NULL,0);
  146. DisplayValue = A;
  147. DisplayString = String(DisplayValue) + " C ";
  148. lcd.setCursor(0, 1);
  149. lcd.print(DisplayString);
  150. }
  151.  
  152. //Air flow Rate
  153. if (SentMessage=="01 10")
  154. {
  155. WorkingString = BuildINString.substring(7,9);
  156. A = strtol(WorkingString.c_str(),NULL,0);
  157. WorkingString = BuildINString.substring(11,13);
  158. B = strtol(WorkingString.c_str(),NULL,0);
  159.  
  160. DisplayValue = ((A * 256)+B)/100;
  161. DisplayString = String(DisplayValue) + " g/s ";
  162. lcd.setCursor(0, 1);
  163. lcd.print(DisplayString);
  164. }
  165.  
  166. //Ambient Temp
  167. if (SentMessage=="01 46")
  168. {
  169. WorkingString = BuildINString.substring(7,9);
  170. A = strtol(WorkingString.c_str(),NULL,0);
  171. DisplayValue = A;
  172. DisplayString = String(DisplayValue) + " C ";
  173. lcd.setCursor(0, 1);
  174. lcd.print(DisplayString);
  175. }
  176.  
  177. //Throttle position
  178. if (SentMessage=="01 11")
  179. {
  180. WorkingString = BuildINString.substring(7,9);
  181. A = strtol(WorkingString.c_str(),NULL,0);
  182. DisplayValue = A;
  183. DisplayString = String(DisplayValue) + " % ";
  184. lcd.setCursor(0, 1);
  185. lcd.print(DisplayString);
  186. }
  187. //Barometric pressure
  188. if (SentMessage=="01 33")
  189. {
  190. WorkingString = BuildINString.substring(7,9);
  191. A = strtol(WorkingString.c_str(),NULL,0);
  192. DisplayValue = A;
  193. DisplayString = String(DisplayValue) + " kpa ";
  194. lcd.setCursor(0, 1);
  195. lcd.print(DisplayString);
  196. }
  197. }
  198. void Bootup()
  199. {
  200. lcd.print("WisperChip V2.00 ");
  201. for (int i=0; i <= 5; i++)
  202. {
  203. for (int j=1; j <= 4; j++)// -/|\-
  204. {
  205. if(j==1){lcd.setCursor(0, 1);lcd.print ("-");delay(200);}
  206. if(j==2){lcd.setCursor(0, 1);lcd.print ("/");delay(200);}
  207. if(j==3){lcd.setCursor(0, 1);lcd.print ("|");delay(200);}
  208. if(j==4){lcd.setCursor(0, 1);lcd.print ("\\");delay(200);}
  209. }
  210. }
  211.  
  212. delay(1000);
  213. }
Advertisement
Add Comment
Please, Sign In to add comment