mario6996

OBD2 Arduino - Adam Korkus

Feb 20th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1.  
  2. // include the library code:
  3.  
  4. #include <Wire.h>
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. byte inData;
  14. char inChar;
  15. String BuildINString="";
  16. String DisplayString="";
  17. String WorkingString="";
  18. long DisplayValue;
  19. long A;
  20. int B;
  21. unsigned long startMillis1; //some global variables available anywhere in the program
  22. unsigned long currentMillis1;
  23. unsigned long startMillis2; //some global variables available anywhere in the program
  24. unsigned long currentMillis2;
  25. const unsigned long period1 = 10000; //the value is a number of milliseconds
  26. const unsigned long period2 = 30000; //the value is a number of milliseconds
  27. bool warmup = true;
  28.  
  29.  
  30. void setup() {
  31. startMillis1 = millis(); //initial start time
  32. startMillis2 = millis(); //initial start time
  33.  
  34. //Display Welcome Message
  35.  
  36. delay(3000);
  37.  
  38.  
  39. //********************************************************************
  40. //Added new code to check if ELM327 responded
  41. Retry:
  42.  
  43.  
  44.  
  45.  
  46.  
  47. //Set up Serial communication at 9600 baud
  48. Serial.begin(9600); //initialize Serial
  49.  
  50. Serial.println("ATZ");
  51.  
  52. delay(2000);
  53. ReadData();
  54.  
  55.  
  56. // If used substring(1,4)=="ATZ" needed a space before ATZ in Serial Monitor and it did not work
  57. if (BuildINString.substring(1,3)=="TZ") // MIATA RESPONSE TO ATZ IS ATZ[[[ELM327 V1.5 OR AT LEAST THAT IS WHAT ARDUINO HAS IN THE BUFFER
  58. {
  59.  
  60. //lcd.print(BuildINString); //Echo response to screen "Welcome ELM327"
  61.  
  62. delay(1500);
  63.  
  64. }
  65. else
  66. {
  67.  
  68. delay(1500);
  69.  
  70. goto Retry;
  71. }
  72.  
  73. //*****************************************************************
  74.  
  75. //Added this from the test Code
  76.  
  77.  
  78.  
  79. Serial.println("0100"); // Works with This only
  80. lcd.setCursor(0, 0);
  81. lcd.print("Initialzing....."); //Initialize BUS //lcd.print("0100 Sent");
  82. delay(4000);
  83. ReadData();
  84. lcd.setCursor(0, 0); //Added 12-10-2016
  85. lcd.print("Initialized....."); //Added 12-10-2016
  86. delay(1000);
  87. lcd.clear();
  88.  
  89. Serial.println("0105");
  90. delay(250);
  91. ReadData();
  92. WorkingString = BuildINString.substring(11,13);
  93. A = strtol(WorkingString.c_str(),NULL,16); //convert hex to decimnal
  94. A = A-40;
  95. if (A>80) {warmup = true;} else {warmup = false;}
  96.  
  97. }
  98.  
  99. void loop() {
  100. BuildINString = "";
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. currentMillis1 = millis(); //get the current "time" (actually the number of milliseconds since the program started)
  110. if (currentMillis1 - startMillis1 >= period1) //test whether the period has elapsed
  111. {
  112. ReadTemp();
  113. startMillis1 = currentMillis1; //IMPORTANT to save the start time of the current LED state.
  114. }
  115. currentMillis2 = millis(); //get the current "time" (actually the number of milliseconds since the program started)
  116. if (currentMillis2 - startMillis2 >= period2) //test whether the period has elapsed
  117. {
  118. ReadVoltage();
  119. startMillis2 = currentMillis2; //IMPORTANT to save the start time of the current LED state.
  120. }
  121. ReadRPM();
  122. ReadSpeed();
  123.  
  124.  
  125.  
  126. }
  127.  
  128. }
  129.  
  130.  
  131. //FUNKCJE FUNKCJE FUNKCJE
  132. void ReadData()
  133. {
  134. BuildINString="";
  135. while(Serial.available() > 0)
  136. {
  137. inData=0;
  138. inChar=0;
  139. inData = Serial.read();
  140. inChar=char(inData);
  141. BuildINString = BuildINString + inChar;
  142. }
  143. }
  144.  
  145. void ReadTemp()
  146. {
  147. Serial.println("0105");
  148. delay(250);
  149. ReadData();
  150. WorkingString = BuildINString.substring(11,13);
  151. A = strtol(WorkingString.c_str(),NULL,16); //convert hex to decimnal
  152. DisplayValue = A;
  153. DisplayString = String(DisplayValue - 40) + " C"; // Subtract 40 from decimal to get the right temperature
  154. lcd.setCursor(11, 1);
  155. lcd.print(DisplayString);
  156. }
  157.  
  158. void ReadVoltage()
  159. {
  160. Serial.println("AT RV");
  161. delay(250);
  162. ReadData();
  163. WorkingString = BuildINString.substring(6,11);
  164. DisplayString = WorkingString;
  165. lcd.setCursor(11, 0);
  166. lcd.print(DisplayString);
  167. }
  168.  
  169. void ReadRPM()
  170. {
  171. Serial.println("010C");
  172. delay(250);
  173. ReadData();
  174.  
  175. WorkingString = BuildINString.substring(11,13);
  176. A = strtol(WorkingString.c_str(),NULL,16);
  177. WorkingString = BuildINString.substring(14,16);
  178. B = strtol(WorkingString.c_str(),NULL,16);
  179.  
  180. DisplayValue = ((A * 256)+B)/4;
  181. DisplayString = String(DisplayValue) + " RPM ";
  182. lcd.setCursor(0, 0);
  183. lcd.print(DisplayString);
  184. }
  185.  
  186. void ReadSpeed()
  187. {
  188. Serial.println("010D");
  189. delay(250);
  190. ReadData();
  191. WorkingString = BuildINString.substring(11,13);
  192. A = strtol(WorkingString.c_str(),NULL,16);
  193. DisplayValue = A;
  194. DisplayString = String(DisplayValue) + "km/h ";
  195. lcd.setCursor(0, 1);
  196. lcd.print(DisplayString);
  197. }
Advertisement
Add Comment
Please, Sign In to add comment