mario6996

obd2 mazda arduino - to correct

Feb 20th, 2020
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1.  
  2.  
  3. /*
  4. 12-12-2016
  5. Final Final Final Tested again on 12-12-2016 Miata Cold With ELM327 Just Connected and Connected all the time. Works Both ways.
  6.  
  7.  
  8. Added 4000 delay after 0100
  9. *
  10. Added if statement to check if over 100 to flash display but it doesn't work correctly. Coomented out
  11. Final Final Code. Works on Miata.
  12. Does a check first to see if it is connected to ELM327 by looking for the ATZ..... received signature
  13. after issuing an ATZ command. Once bluetooth is connected than we get back ATZ[[[ELM327 v1.5
  14. To parse the ATZ needed to look for only TZ by using substring(1,3)=="TZ". Both Serial Monitor and ELM327 work without a space in the beginning.
  15.  
  16.  
  17.  
  18. 1.f
  19.  
  20. if (BuildINString.substring(1,3)=="TZ") ENTER WITH A SPACE 1ST CHAR DOESN'T MATTER
  21.  
  22. Add a check if ELM327 returned
  23. (1,4)=="ATZ") Serial Monitor put a space in front _ATZ????? Test Works OK but not in car
  24. // MIATA RESPONSE TO ATZ IS "ATZ[[[ELM327 V1.5" OR AT LEAST THAT IS WHAT ARDUINO HAS IN THE BUFFER
  25.  
  26. 12-8-2016
  27. Result = A++ Works Great got back correct reading
  28. Miata Final 1.c
  29. ATZ ATZ 0100 105, increased message to 3 seconds
  30.  
  31. 12-7-2016
  32. Back to original code still wrong display bach -35 C
  33. Commented last code not to blink
  34. Original Code Got back correct value with Blinking
  35.  
  36. Updated 12-7-2016
  37. tests OK in Serial Monitor
  38. Test OK in Miata
  39. WorkingString = BuildINString.substring(11,14); //0105[41 05 32 // Correct value reading back 10 degrees.
  40. 32(h) is 50(d) -40 is 10
  41.  
  42. WITH THIS SET UP
  43. wait 5 seconds between ignition otherwise received 0105[Lines] STOPPED[Lines][Lines]>
  44. After 5 seconds ON Received 0105[Lines]41 05 32 [Lines][Lines]
  45. */
  46.  
  47. // include the library code:
  48. #include <LiquidCrystal.h> //NEW!!!!!!!!!
  49. #include <Wire.h> //NEW!!!!!!!!!!!!
  50. // initialize the library with the numbers of the interface pins
  51. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
  52.  
  53.  
  54. byte inData;
  55. char inChar;
  56. String BuildINString="";
  57. String DisplayString="";
  58. String WorkingString="";
  59. long DisplayValue;
  60. long A;
  61.  
  62.  
  63.  
  64. void setup() {
  65. // put your setup code here, to run once:
  66.  
  67. // set up the LCD's number of columns and rows:
  68. lcd.begin(16, 2);
  69.  
  70. //Display Welcome Message
  71. lcd.setCursor(0, 0);
  72. lcd.print("2004 Mazda Miata");
  73. lcd.setCursor(0, 1);
  74. lcd.print("Welcome, Sofia ");
  75. delay(3000);
  76. lcd.clear();
  77.  
  78. //********************************************************************
  79. //Added new code to check if ELM327 responded
  80. Retry:
  81. lcd.setCursor(0, 0);
  82. lcd.print(" ");
  83. lcd.setCursor(0, 1);
  84. lcd.print("Connecting...... ");
  85.  
  86.  
  87.  
  88.  
  89. //Set up Serial communication at 9600 baud
  90. Serial.begin(9600); //initialize Serial
  91.  
  92. Serial.println("ATZ");
  93. lcd.setCursor(0, 0);
  94. lcd.print("ELM327 TZ ");
  95. delay(2000);
  96. ReadData();
  97.  
  98.  
  99. // If used substring(1,4)=="ATZ" needed a space before ATZ in Serial Monitor and it did not work
  100. 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
  101. {
  102. lcd.clear();
  103. lcd.setCursor(0, 0);
  104. lcd.print("Welcome");
  105. lcd.setCursor(9, 0);
  106. //lcd.print(BuildINString); //Echo response to screen "Welcome ELM327"
  107. lcd.setCursor(0, 1);
  108. lcd.print("Connection OK ");
  109. delay(1500);
  110. lcd.clear();
  111. }
  112. else
  113. {
  114. lcd.setCursor(0, 0);
  115. lcd.print("Error ");
  116. lcd.setCursor(0, 1);
  117. lcd.print("No Connection! ");
  118. delay(1500);
  119. lcd.clear();
  120. goto Retry;
  121. }
  122.  
  123. //*****************************************************************
  124.  
  125. //Added this from the test Code
  126.  
  127.  
  128.  
  129. Serial.println("0100"); // Works with This only
  130. lcd.setCursor(0, 0);
  131. lcd.print("Initialzing....."); //Initialize BUS //lcd.print("0100 Sent");
  132. delay(4000);
  133. ReadData();
  134. lcd.setCursor(0, 0); //Added 12-10-2016
  135. lcd.print("Initialized....."); //Added 12-10-2016
  136. delay(1000);
  137. lcd.clear();
  138.  
  139.  
  140.  
  141. }
  142.  
  143. void loop() {
  144.  
  145. // Added to display Coolant Temp
  146. lcd.setCursor(0, 0);
  147. lcd.print("Coolant Temp ");
  148.  
  149.  
  150. //resets the received string to NULL Without it it repeated last string.
  151. BuildINString = "";
  152.  
  153. Serial.println("0105"); // Send Coolant PID request 0105
  154. //Serial.flush(); //Not sure if it's needed*********************************************
  155. delay(1000);
  156.  
  157. // Receive complete string from the serial buffer
  158. ReadData(); //replaced below code
  159.  
  160.  
  161. // Parse the received string, retrieve only the hex value for temperature Example: 32 is the 11 and 12th character
  162. // 0105[41 05 32 Correct value reading back 10 degrees. (11,13 or even 11,14 works)
  163. WorkingString = BuildINString.substring(11,13);
  164.  
  165. A = strtol(WorkingString.c_str(),NULL,16); //convert hex to decimnal
  166.  
  167. DisplayValue = A;
  168. DisplayString = String(DisplayValue - 40) + " C "; // Subtract 40 from decimal to get the right temperature
  169. lcd.setCursor(0, 1);
  170. lcd.print(DisplayString);
  171. delay(500);
  172.  
  173. //Check if if over 100 C ******************************************************
  174. int B;
  175. B = DisplayString.toInt(); //Convert String to Integer .toInt()
  176.  
  177.  
  178. if (B >= 100){ //Check if if over 100 C
  179.  
  180. // ------- Quick 3 blinks of backlight -------------
  181. for(int i = 0; i< 3; i++)
  182. {
  183. lcd.display(); //For I2C use lcd.backlight
  184. delay(250);
  185. lcd.noDisplay(); //For I2C use lcd.noBacklight
  186. delay(250);
  187. }
  188. lcd.display(); // finish with backlight on //For I2C use lcd.backlight
  189. }
  190.  
  191. }
  192.  
  193. //Read Data Function ***********************************************************
  194. void ReadData()
  195. {
  196. BuildINString="";
  197. while(Serial.available() > 0)
  198. {
  199. inData=0;
  200. inChar=0;
  201. inData = Serial.read();
  202. inChar=char(inData);
  203. BuildINString = BuildINString + inChar;
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment