Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. char rxData[20];
  2. char rxIndex=0;
  3. boolean carON = false;
  4. int vehicleRPM = 0;
  5. int vehicleRunTime = 0;
  6. int vehicleSpeed = 0;
  7.  
  8.  
  9.  
  10. void onStart(){ //Startup Process
  11.  
  12. } //Startup END
  13.  
  14. void getResponse(void){
  15. char inChar=0;
  16. int missed = 0;
  17. //Keep reading characters until we get a carriage return
  18. while(inChar != '\r'){
  19. //If a character comes in on the serial port, we need to act on it.
  20. if(Serial1.available() > 0){
  21. //Start by checking if we've received the end of message character ('\r').
  22. if(Serial1.peek() == '\r'){
  23. //Clear the Serial buffer
  24. inChar=Serial1.read();
  25. //Put the end of string character on our data string
  26. rxData[rxIndex]='\0';
  27. //Reset the buffer index so that the next character goes back at the beginning of the string.
  28. rxIndex=0;
  29. }
  30. //If we didn't get the end of message character, just add the new character to the string.
  31. else{
  32. //Get the new character from the Serial port.
  33. inChar = Serial1.read();
  34. //Add the new character to the string, and increment the index variable.
  35. rxData[rxIndex++]=inChar;
  36. }
  37. }else{
  38. missed++;
  39. if(missed>500){
  40. if(rxData[0] == 'S' && rxData[1] == 'E' && rxData[2] == 'A' && rxData[3] == 'R' && rxData[4] == 'C' && rxData[5] == 'H' && rxData[6] == 'I' && rxData[7] == 'N'){
  41. inChar='\r';
  42. }else if(rxData[0] == 'S' && rxData[1] == 'T' && rxData[2] == 'O' && rxData[3] == 'P' && rxData[4] == 'P' && rxData[5] == 'E' && rxData[6] == 'D'){
  43. inChar='\r';
  44. }else{
  45. inChar='\r';
  46. rxData[0] = 'E';
  47. rxData[1] = 'L';
  48. rxData[2] = 'M';
  49. }
  50. }
  51. }
  52. delay(3);
  53. }
  54. //Serial.println(rxData);
  55. }
  56. void getResponseFAST(void){
  57. char inChar=0;
  58. //Serial.println("Checking");
  59. //Keep reading characters until we get a carriage return
  60. while(inChar != '\r'){
  61. //If a character comes in on the serial port, we need to act on it.
  62. if(Serial1.available() > 0){
  63. //Start by checking if we've received the end of message character ('\r').
  64. if(Serial1.peek() == '\r'){
  65. //Clear the Serial buffer
  66. inChar=Serial1.read();
  67. //Put the end of string character on our data string
  68. rxData[rxIndex]='\0';
  69. //Reset the buffer index so that the next character goes back at the beginning of the string.
  70. rxIndex=0;
  71. }
  72. //If we didn't get the end of message character, just add the new character to the string.
  73. else{
  74. //Get the new character from the Serial port.
  75. inChar = Serial1.read();
  76. //Add the new character to the string, and increment the index variable.
  77. rxData[rxIndex++]=inChar;
  78. }
  79. }
  80. delay(5);
  81. }
  82. }
  83. void onCarTurnedOn(){
  84. Serial.println("ON");
  85. carON = true;
  86. allLEDOFFFADE();
  87. HeadHalosRedOn = false;
  88. FogHalosRedOn = false;
  89. HeadHalosWhiteOn = true;
  90. FogHalosWhiteOn = true;
  91.  
  92. int bright = 0;
  93. int bright2 = 0;
  94. int i = 0;
  95. while(bright<=255){
  96. analogWrite(headlightHaloREDPIN, bright);
  97. analogWrite(foglightHaloREDPIN, bright);
  98. bright++;
  99. delay(5);
  100. }
  101. bright = 255;
  102. while(bright2<255){
  103. if(bright>=0){
  104. analogWrite(headlightHaloREDPIN, bright);
  105. analogWrite(foglightHaloREDPIN, bright);
  106. bright--;
  107. }
  108. if(bright2<255 && bright< 30){
  109. analogWrite(headlightHaloWHITEPIN, bright2);
  110. analogWrite(foglightHaloWHITEPIN, bright2);
  111. bright2++;
  112. }
  113. delay(5);
  114. }
  115. refresh();
  116. Serial1.flush();
  117. //setupOBD();
  118. }
  119. void onCarTurnedOff(){
  120. Serial.println("OFF");
  121. carON = false;
  122. vehicleRPM = 0;
  123. //Store state of old settings
  124. BRIGHTNESS_HALOS2 = BRIGHTNESS_HALOS;
  125. BRIGHTNESS_INTERIOR2 = BRIGHTNESS_INTERIOR;
  126. BRIGHTNESS_FOGS2 = BRIGHTNESS_FOGS;
  127. HeadHalosRedOn2 = HeadHalosRedOn;
  128. FogHalosRedOn2 = FogHalosRedOn;
  129. HeadHalosWhiteOn2 = HeadHalosWhiteOn;
  130. FogHalosWhiteOn2 = FogHalosWhiteOn;
  131. InteriorOn2 = InteriorOn;
  132. FogsOn2 = FogsOn;
  133.  
  134. allLEDOFFFADE(); //Make all lights off
  135. allBool(false); //Make all lights off
  136.  
  137. turnON(headlightHaloREDPIN, BRIGHTNESS_HALOS); //Turn On
  138. turnON(foglightHaloREDPIN, BRIGHTNESS_HALOS); //Turn On
  139. delay(10);
  140. turnOFF(headlightHaloREDPIN, BRIGHTNESS_HALOS); //turn off
  141. turnOFF(foglightHaloREDPIN, BRIGHTNESS_HALOS); //Turn On
  142.  
  143.  
  144. checkForChange(); //Update user new values
  145. }
  146.  
  147. void setZeroRPM(){
  148. rxData[0] = '4';
  149. rxData[1] = '1';
  150. rxData[2] = ' ';
  151. rxData[3] = '0';
  152. rxData[4] = 'C';
  153. rxData[5] = ' ';
  154. rxData[6] = '0';
  155. rxData[7] = '0';
  156. rxData[8] = ' ';
  157. rxData[9] = '0';
  158. rxData[10] = '0';
  159. }
  160. void getRPM(){
  161. while(Serial1.available()>0) Serial1.read(); //Clear Serial buffer
  162. delay(10);
  163. Serial1.print("010C\r");
  164. delay(80);
  165. int wait = 0;
  166. while(Serial1.available() == 0 && wait < 5000){ wait++; delay(1);}
  167. //Get the response from the OBD-II-UART board
  168. getResponse();
  169. //Serial.println("Send 1");
  170.  
  171. getResponse();
  172. //Serial.println("Send 2");
  173.  
  174. //Serial.println("Getting");
  175. if(rxData[0] == '4' && rxData[1] == '1' && rxData[3] == '0' && rxData[4] == 'C'){
  176. vehicleRPM = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
  177. //Print the rpm data to debug serial window
  178. Serial.print("RPM:");
  179. Serial.println(vehicleRPM);
  180. }else if(rxData[0] == 'E' && rxData[1] == 'L' && rxData[2] == 'M' && rxData[3] != '3' && rxData[4] != '2' && rxData[5] != '7'){ onCarTurnedOff(); }
  181.  
  182.  
  183. while(Serial1.available()>0) Serial1.read(); //Clear Serial buffer
  184. }
  185. void getRunTime(){
  186. Serial.flush();
  187. delay(10);
  188. Serial1.print("011f\r");
  189. delay(20);
  190. //Get the response from the OBD-II-UART board
  191. do{
  192. getResponse();
  193. }while(rxData[0] == 'S' && rxData[1] == 'E' && rxData[2] == 'A' && rxData[3] == 'R' && rxData[4] == 'C' && rxData[5] == 'H' && rxData[6] == 'I' && rxData[7] == 'N' && Serial.available() == 0);
  194. //Serial.println("Called");
  195. getResponse();
  196. //Serial.println("Called");
  197. if(rxData[0] == '4' && rxData[1] == '1' && rxData[3] == '1' && rxData[4] == 'F'){
  198. vehicleRunTime = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
  199. //Print the rpm data to debug serial window
  200. Serial.print("RunTime: ");
  201. Serial.println(vehicleRunTime);
  202. }else if(rxData[0] == 'E' && rxData[1] == 'L' && rxData[2] == 'M' && rxData[3] != '3' && rxData[4] != '2' && rxData[5] != '7'){ onCarTurnedOff(); }
  203. }
  204. int updateVehicleSpeed(){
  205. //Delete any data that may be in the serial port before we begin.
  206. Serial.flush();
  207. delay(30);
  208. //Query the OBD-II-UART for the Vehicle Speed
  209. Serial1.println("010D\r");
  210. delay(20);
  211. //Get the response from the OBD-II-UART board
  212. do{
  213. getResponse();
  214. }while(rxData[0] == 'S' && rxData[1] == 'E' && rxData[2] == 'A' && rxData[3] == 'R' && rxData[4] == 'C' && rxData[5] == 'H' && rxData[6] == 'I' && rxData[7] == 'N' && Serial.available() == 0);
  215. //Serial.println("Called");
  216. getResponse();
  217. //Serial.println("Called");
  218. if(rxData[0] == '4' && rxData[1] == '1' && rxData[3] == '0' && rxData[4] == 'D'){
  219. vehicleSpeed = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
  220. //Print the rpm data to debug serial window
  221. Serial.print("Speed:");
  222. Serial.println(vehicleSpeed);
  223. }else if(rxData[0] == 'E' && rxData[1] == 'L' && rxData[2] == 'M'){ onCarTurnedOff(); }
  224. }
  225.  
  226.  
  227. //FAST
  228.  
  229.  
  230. void getRPMFast(){
  231. Serial1.println("010c\r");
  232. delay(20);
  233. //Get the response from the OBD-II-UART board
  234. getResponseFAST();
  235. getResponseFAST();
  236. //Serial.println("Called");
  237. if(rxData[0] == '4' && rxData[1] == '1' && rxData[3] == '0' && rxData[4] == 'C'){
  238. int newRPM = ((strtol(&rxData[6],0,16)*256)+strtol(&rxData[9],0,16))/4;
  239. if(newRPM >= 0 && newRPM <7000)
  240. vehicleRPM = newRPM;
  241. //Print the rpm data to debug serial window
  242. Serial.print("RPM:");
  243. Serial.print(vehicleRPM);
  244. Serial.println(" ");
  245. }
  246. }
  247.  
  248.  
  249. /*
  250. //Keep On Bottom
  251. void doVehicleIsOffCheck(){
  252. vehicleRPM = updateVehicleRPM();
  253. if(rebootedLast <= 100) rebootedLast++;
  254. if(vehicleRPM >= DEFAULTOnRPM && !FULLVEHICLEOFFCHECK){
  255. Serial.println("Vehicle turned on");
  256. HeadHalosWhiteOn = true;
  257. FogHalosWhiteOn = true;
  258. if(rebootedLast > 100){
  259. Serial.println("Rebooting OBD");
  260. Serial1.print("ATWS\r");
  261. delay(1000);
  262. Serial1.print("0100\r");
  263. delay(1000);
  264. Serial1.flush();
  265. resetOBDArray();
  266. }
  267. //matchHalosWithRPM(true, true);
  268. }else{
  269. if(rebootedLast >= 15) resetOBDArray();
  270. }
  271. delay(20);
  272. }
  273.  
  274. void doRegularVehicleCheck(){
  275. vehicleRPM = updateVehicleRPM();
  276. if(rebootedLast <= 100) rebootedLast++;
  277. if(vehicleRPM <= DEFAULTOnRPM && FULLVEHICLEOFFCHECK){
  278. //resetDevice();
  279. allBool(false);
  280. allLEDOFF();
  281. Serial.println("Vehicle turned off");
  282. //resetOBDArray();
  283. }
  284. delay(20);
  285. }
  286. */
  287.  
  288. void myOBDLoop(){
  289. if(!carON){
  290. if(Serial1.available()>1){
  291. getResponse();
  292. if(rxData[0] == 'E' && rxData[1] == 'L' && rxData[2] == 'M' && rxData[3] == '3' && rxData[4] == '2' && rxData[5] == '7'){
  293. onCarTurnedOn();
  294. }else if(rxData[0] == 'S' && rxData[1] == 'E' && rxData[2] == 'A' && rxData[3] == 'R' && rxData[4] == 'C' && rxData[5] == 'H' && rxData[6] == 'I' && rxData[7] == 'N'){
  295. onCarTurnedOn();
  296. }
  297. }
  298. }else{
  299. getRPM();
  300. }
  301. delay(50);
  302. } //LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement