pleasedontcode

Vehicle Data rev_15

Jul 11th, 2025
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Vehicle Data
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-07-11 21:59:29
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to android */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SoftwareSerial.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float T_interpolate(byte DS_Temp);
  32. void sendDataToAndroid(float RPM, float TPS, float MAF, int BLCELL, int BLM, int INTEGRATOR, float InjPW, float O2mv, float MAT, unsigned int Runtime);
  33.  
  34. /***** DEFINITION OF Software Serial *****/
  35. const uint8_t HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  36. const uint8_t HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  37. SoftwareSerial HC05_BluetoothModule_HC05_mySerial(HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_RX_A1, HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_TX_A0);
  38.  
  39. // Define pins and variables used in the code
  40. const int ALDLTestPin = 2; // Example pin, set accordingly
  41. const int DecodeDataOutputPin = 3; // Example pin, set accordingly
  42. const int HexDataOutputPin = 4; // Example pin, set accordingly
  43. const byte M1Cmd[4] = {0x01, 0x02, 0x03, 0x04}; // Example command, set accordingly
  44. const int ByteCount = 64; // Example byte count, set accordingly
  45. const int linecount = 16; // Example line count for hex output
  46. int DataBytes[65]; // Buffer for data bytes
  47. bool SilenceFound = false;
  48. bool PreambleFound = false;
  49. unsigned long PreambleTimer = 0;
  50. unsigned long PreambleTimerStart = 0;
  51. int ALDLbyte = 0;
  52. int bytecounter = 0;
  53.  
  54. // Placeholder variables for undefined identifiers
  55. int linecount = 16; // For hex output line length
  56.  
  57. void setup(void)
  58. {
  59.   // put your setup code here, to run once:
  60.   HC05_BluetoothModule_HC05_mySerial.begin(9600);
  61.   Serial.begin(115200); // Initialize Serial for debugging/output
  62.   pinMode(ALDLTestPin, INPUT); // ALDL activity listen pin
  63.   pinMode(DecodeDataOutputPin, INPUT_PULLUP); // Decoded data output control
  64.   pinMode(HexDataOutputPin, INPUT_PULLUP); // Hex data output control
  65.   delay(1500);
  66.   Serial.println("Ready for data capture");
  67. }
  68.  
  69. void loop() {
  70.   // Wait for silence period on the ALDL
  71.   Serial.print("wait for silence ");
  72.   SilenceFound = false; // Reset silence flag
  73.   unsigned long StartTime = micros(); // Initialize timer
  74.   while ((micros() - StartTime) < 15000) { // Wait for 15 ms silence
  75.     if (digitalRead(ALDLTestPin) == 0) { // Any line activity resets timer
  76.       StartTime = micros();
  77.     }
  78.   }
  79.   SilenceFound = true; // Silence detected
  80.  
  81.   while (SilenceFound == true) { // While silence detected, request and process data
  82.     PreambleFound = false; // Reset preamble flag
  83.  
  84.     // Send Mode 1 command to request data
  85.     while (PreambleFound == false) {
  86.       Serial.print(" M1 cmd ");
  87.       for (int i = 0; i < 4; i++) {
  88.         Serial1.write(M1Cmd[i]);
  89.       }
  90.       Serial.println(" Finding Preamble  ");
  91.       int i = 0;
  92.       PreambleTimer = millis();
  93.       while ((millis() - PreambleTimer) < 100 && PreambleFound == false) {
  94.         if (Serial1.available() > 0) {
  95.           ALDLbyte = Serial1.read();
  96.           if (ALDLbyte == Preamble[i]) {
  97.             i++;
  98.             if (i > 2) PreambleFound = true;
  99.           } else {
  100.             PreambleFound = false;
  101.             i = 0;
  102.           }
  103.         }
  104.       }
  105.     }
  106.  
  107.     // Read data stream after preamble
  108.     int DataStreamIndex = 1;
  109.     while (DataStreamIndex < 65) {
  110.       if (Serial1.available() > 0) {
  111.         DataBytes[DataStreamIndex] = Serial1.read();
  112.         DataStreamIndex++;
  113.       }
  114.     }
  115.  
  116.     // Calculate checksum
  117.     int i = 1;
  118.     unsigned long CheckTotal = 0x80 + 0x95 + 0x01; // Sum preamble bytes
  119.     while (i < ByteCount) {
  120.       CheckTotal += DataBytes[i];
  121.       i++;
  122.     }
  123.     byte CheckSum = 0x200 - CheckTotal; // Two's complement
  124.  
  125.     // Verify checksum and output data
  126.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  127.       Serial.print("New Data Stream received at ");
  128.       Serial.print(millis());
  129.       Serial.print(" Calc CHECKSUM: ");
  130.       Serial.print(CheckSum, HEX);
  131.       Serial.print(" Transmitted CHECKSUM: ");
  132.       Serial.print(DataBytes[ByteCount], HEX);
  133.       if (CheckSum == DataBytes[ByteCount]) {
  134.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  135.       } else {
  136.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  137.       }
  138.  
  139.       // Extract and convert data
  140.       float RPM = DataBytes[11] * 25; // Engine RPM
  141.       float TPS = DataBytes[10] * 0.019608; // Throttle position in volts
  142.       float MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906; // Mass Air Flow
  143.       int BLCELL = DataBytes[21]; // Block learn cell
  144.       int BLM = DataBytes[20]; // Block learn value
  145.       int INTEGRATOR = DataBytes[22]; // Fuel trim
  146.       float InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259; // Injector pulse width
  147.       float O2mv = DataBytes[17] * 4.44; // O2 sensor in mV
  148.       float MAT = T_interpolate(DataBytes[30]); // Intake temp
  149.       unsigned int Runtime = (DataBytes[52] * 256) + DataBytes[53]; // Runtime seconds
  150.  
  151.       // Send data over Bluetooth to Android
  152.       sendDataToAndroid(RPM, TPS, MAF, BLCELL, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime);
  153.  
  154.       // Print data locally for debugging
  155.       Serial.print("Engine Speed     : ");
  156.       Serial.print(RPM);
  157.       Serial.println(" RPM");
  158.       Serial.print("Throttle Position: ");
  159.       Serial.print(TPS);
  160.       Serial.println(" Volts");
  161.       Serial.print("Mass Air Flow    : ");
  162.       Serial.print(MAF);
  163.       Serial.println(" Grams/Sec");
  164.       Serial.print("Current BLM Cell: ");
  165.       Serial.print(BLCELL);
  166.       Serial.print(" BLM Value: ");
  167.       Serial.print(BLM);
  168.       Serial.print("  Current Fuel Integrator: ");
  169.       Serial.println(INTEGRATOR);
  170.       Serial.print("Injector Pulse   : ");
  171.       Serial.print(InjPW);
  172.       Serial.println(" Milliseconds");
  173.       Serial.print("O2 Sensor Voltage: ");
  174.       Serial.print(O2mv);
  175.       Serial.println(" Millivolts");
  176.       Serial.print("Intake Air Temp  : ");
  177.       Serial.print(MAT);
  178.       Serial.println(" Deg C");
  179.       Serial.print("Engine Run Time  : ");
  180.       Serial.print(Runtime);
  181.       Serial.println(" Seconds");
  182.  
  183.       // Delay for readability
  184.       unsigned long StartTimeDelay = millis();
  185.       while (millis() < StartTimeDelay + 3000) {
  186.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // Flush buffer
  187.       }
  188.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  189.       // Output hex data
  190.       Serial.print("New Data Stream received at ");
  191.       Serial.print(millis());
  192.       Serial.print(" Calc CHECKSUM: ");
  193.       Serial.print(CheckSum, HEX);
  194.       Serial.print(" Transmitted CHECKSUM: ");
  195.       Serial.print(DataBytes[ByteCount], HEX);
  196.       if (CheckSum == DataBytes[ByteCount]) {
  197.         Serial.println(" Checksum GOOD - Data as follows: ");
  198.       } else {
  199.         Serial.println(" Checksum *** ERROR *** -  Data as follows: ");
  200.       }
  201.  
  202.       // Print hex data
  203.       int j = 1;
  204.       bytebytecounter = 0;
  205.       while (j < ByteCount + 1) {
  206.         Serial.print(DataBytes[j], HEX);
  207.         j++;
  208.         bytecounter++;
  209.         if (bytecounter >= linecount) {
  210.           bytecounter = 0;
  211.           Serial.println();
  212.         } else {
  213.           Serial.print(" ");
  214.         }
  215.       }
  216.       Serial.println();
  217.     } else {
  218.       // Send raw data stream
  219.       Serial.write(0x80);
  220.       Serial.write(0x95);
  221.       Serial.write(0x01);
  222.       for (int j = 1; j < ByteCount + 1; j++) {
  223.         Serial.write(DataBytes[j]);
  224.       }
  225.     }
  226.   }
  227. }
  228.  
  229. // Implementation of T_interpolate function
  230. float T_interpolate(byte DS_Temp) {
  231.   const float TempScale[38] = {1,5,6,9,11,15,19,25,31,38,47,57,67,79,91,104,117,130,142,154,164,175,184,192,200,206,212,217,222,226,230,233,235,238,240,242,244,256};
  232.   const float TempValue[38] = {-40,-30,-25,-20,-15,-10,-5,0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,200};
  233.   float T_Range, T_Diff, TempRange, Temperature;
  234.   int i = 0;
  235.   while (i < 38) {
  236.     if (TempScale[i] > DS_Temp) break;
  237.     i++;
  238.   }
  239.   if (i > 0) {
  240.     T_Range = TempScale[i] - TempScale[i - 1];
  241.     T_Diff = DS_Temp - TempScale[i - 1];
  242.     TempRange = TempValue[i] - TempValue[i - 1];
  243.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  244.   } else {
  245.     Temperature = TempValue[0];
  246.   }
  247.   return Temperature;
  248. }
  249.  
  250. // Function to send data over Bluetooth to Android
  251. void sendDataToAndroid(float RPM, float TPS, float MAF, int BLCELL, int BLM, int INTEGRATOR, float InjPW, float O2mv, float MAT, unsigned int Runtime) {
  252.   // Format data as a comma-separated string
  253.   String dataString = String(RPM, 0) + "," +
  254.                       String(TPS, 3) + "," +
  255.                       String(MAF, 3) + "," +
  256.                       String(BLCELL) + "," +
  257.                       String(BLM) + "," +
  258.                       String(INTEGRATOR) + "," +
  259.                       String(InjPW, 3) + "," +
  260.                       String(O2mv, 1) + "," +
  261.                       String(MAT, 1) + "," +
  262.                       String(Runtime);
  263.   // Send over Bluetooth
  264.   HC05_BluetoothModule_HC05_mySerial.println(dataString);
  265. }
  266.  
Advertisement
Add Comment
Please, Sign In to add comment