pleasedontcode

Vehicle Data rev_20

Jul 11th, 2025
422
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 22:29:37
  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); // Prototype for T_interpolate function
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. // Bluetooth HC05 module pins
  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 other pins and variables used in the code
  40. const int ALDLTestPin = 2; // Example pin, define as per your hardware
  41. const int DecodeDataOutputPin = 3; // Example pin, define as per your hardware
  42. const int HexDataOutputPin = 4; // Example pin, define as per your hardware
  43.  
  44. byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, define as per your protocol
  45. byte DataBytes[65]; // Buffer for data bytes
  46. const int ByteCount = 64; // Total number of data bytes expected
  47. int linecount = 16; // Number of bytes per line in hex output
  48. int bytecounter = 0; // Counter for hex output formatting
  49.  
  50. // Variables for parsed data
  51. unsigned long RPM;
  52. float TPS;
  53. float MAF;
  54. byte BLCELL;
  55. byte BLM;
  56. byte INTEGRATOR;
  57. float InjPW;
  58. float O2mv;
  59. float MAT;
  60. unsigned long Runtime;
  61.  
  62. // Flags and timers
  63. bool SilenceFound = false;
  64. bool PreambleFound = false;
  65. unsigned long PreambleTimer = 0;
  66. byte ALDLbyte = 0;
  67.  
  68. // Preamble pattern
  69. byte Preamble[3] = {0x80, 0x95, 0x01};
  70.  
  71. // Function prototypes
  72. float T_interpolate(byte DS_Temp);
  73.  
  74. void setup(void)
  75. {
  76.   // put your setup code here, to run once:
  77.   HC05_BluetoothModule_HC05_mySerial.begin(9600);
  78.   // Initialize serial communication for debugging/output
  79.   Serial.begin(115200);
  80.   // Initialize serial1 at 8192 baud for ALDL data
  81.   Serial1.begin(8192);
  82.   delay(1500);
  83.   Serial.println("Ready for data capture");
  84.   // Initialize I/O pins
  85.   pinMode(ALDLTestPin, INPUT);
  86.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);
  87.   pinMode(HexDataOutputPin, INPUT_PULLUP);
  88. }
  89.  
  90. void loop() {
  91.   // Wait for silence period on the ALDL
  92.   Serial.print("wait for silence ");
  93.   SilenceFound = false; // Reset silence flag
  94.   unsigned long StartTime = micros(); // Start timer
  95.   while ((micros() - StartTime) < 15000) { // Wait 15 ms silence
  96.     if (digitalRead(ALDLTestPin) == 0) { // Any activity resets timer
  97.       StartTime = micros();
  98.     }
  99.   }
  100.   SilenceFound = true; // Silence detected
  101.  
  102.   while (SilenceFound == true) { // Loop while silence is detected
  103.     PreambleFound = false; // Reset preamble flag
  104.     // Send Mode 1 command
  105.     Serial.print(" M1 cmd ");
  106.     int i = 0;
  107.     while (i < 4) {
  108.       Serial1.write(M1Cmd[i]);
  109.       i++;
  110.     }
  111.     Serial.println(" Finding Preamble  ");
  112.     i = 0; // Reset index for preamble detection
  113.     PreambleTimer = millis(); // Start timeout timer
  114.     while ((millis() - PreambleTimer) < 100 && PreambleFound == false) {
  115.       if (Serial1.available() > 0) {
  116.         ALDLbyte = Serial1.read();
  117.         if (ALDLbyte == Preamble[i]) {
  118.           i++;
  119.           if (i > 2) PreambleFound = true; // Preamble detected
  120.         } else {
  121.           PreambleFound = false;
  122.           i = 0; // Reset if mismatch
  123.         }
  124.       }
  125.     }
  126.     if (PreambleFound) {
  127.       // Read data bytes
  128.       int DataStreamIndex = 1;
  129.       while (DataStreamIndex < 65) {
  130.         if (Serial1.available() > 0) {
  131.           DataBytes[DataStreamIndex] = Serial1.read();
  132.           DataStreamIndex++;
  133.         }
  134.       }
  135.       // Calculate checksum
  136.       int i = 1;
  137.       unsigned long CheckTotal = 0x80 + 0x95 + 0x01; // Sum preamble bytes
  138.       while (i < ByteCount) {
  139.         CheckTotal += DataBytes[i];
  140.         i++;
  141.       }
  142.       byte CheckSum = (byte)(0x200 - CheckTotal);
  143.       // Verify checksum
  144.       if (digitalRead(DecodeDataOutputPin) == LOW) {
  145.         Serial.print("New Data Stream received at ");
  146.         Serial.print(millis());
  147.         Serial.print(" Calc CHECKSUM: ");
  148.         Serial.print(CheckSum, HEX);
  149.         Serial.print(" Transmitted CHECKSUM: ");
  150.         Serial.print(DataBytes[ByteCount], HEX);
  151.         if (CheckSum == DataBytes[ByteCount]) {
  152.           Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  153.         } else {
  154.           Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  155.         }
  156.         // Parse data bytes
  157.         RPM = DataBytes[11] * 25; // Engine RPM
  158.         TPS = DataBytes[10] * 0.019608; // TPS volts
  159.         MAF = ((DataBytes[36] * 256) + (DataBytes[37])) * 0.003906; // MAF gm/sec
  160.         BLCELL = DataBytes[21];
  161.         BLM = DataBytes[20];
  162.         INTEGRATOR = DataBytes[22];
  163.         InjPW = ((DataBytes[45] * 256) + (DataBytes[46])) * 0.015259;
  164.         O2mv = DataBytes[17] * 4.44;
  165.         MAT = T_interpolate(DataBytes[30]);
  166.         Runtime = (DataBytes[52] * 256 + DataBytes[53]);
  167.  
  168.         // Send data over Bluetooth to Android
  169.         HC05_BluetoothModule_HC05_mySerial.print("RPM:");
  170.         HC05_BluetoothModule_HC05_mySerial.print(RPM);
  171.         HC05_BluetoothModule_HC05_mySerial.print(",TPS:");
  172.         HC05_BluetoothModule_HC05_mySerial.print(TPS);
  173.         HC05_BluetoothModule_HC05_mySerial.print(",MAF:");
  174.         HC05_BluetoothModule_HC05_mySerial.print(MAF);
  175.         HC05_BluetoothModule_HC05_mySerial.print(",BLCELL:");
  176.         HC05_BluetoothModule_HC05_mySerial.print(BLCELL);
  177.         HC05_BluetoothModule_HC05_mySerial.print(",BLM:");
  178.         HC05_BluetoothModule_HC05_mySerial.print(BLM);
  179.         HC05_BluetoothModule_HC05_mySerial.print(",INTEGRATOR:");
  180.         HC05_BluetoothModule_HC05_mySerial.print(INTEGRATOR);
  181.         HC05_BluetoothModule_HC05_mySerial.print(",InjPW:");
  182.         HC05_BluetoothModule_HC05_mySerial.print(InjPW);
  183.         HC05_BluetoothModule_HC05_mySerial.print(",O2mv:");
  184.         HC05_BluetoothModule_HC05_mySerial.print(O2mv);
  185.         HC05_BluetoothModule_HC05_mySerial.print(",MAT:");
  186.         HC05_BluetoothModule_HC05_mySerial.print(MAT);
  187.         HC05_BluetoothModule_HC05_mySerial.print(",Runtime:");
  188.         HC05_BluetoothModule_HC05_mySerial.println(Runtime);
  189.  
  190.         // Display parsed data
  191.         Serial.print("Engine Speed     : ");
  192.         Serial.print(RPM);
  193.         Serial.println(" RPM");
  194.         Serial.print("Throttle Position: ");
  195.         Serial.print(TPS);
  196.         Serial.println(" Volts");
  197.         Serial.print("Mass Air Flow    : ");
  198.         Serial.print(MAF);
  199.         Serial.println(" Grams/Sec");
  200.         Serial.print("Current BLM Cell: ");
  201.         Serial.print(BLCELL);
  202.         Serial.print(" BLM Value: ");
  203.         Serial.print(BLM);
  204.         Serial.print("  Current Fuel Integrator: ");
  205.         Serial.println(INTEGRATOR);
  206.         Serial.print("Injector Pulse   : ");
  207.         Serial.print(InjPW);
  208.         Serial.println(" Milliseconds");
  209.         Serial.print("O2 Sensor Voltage: ");
  210.         Serial.print(O2mv);
  211.         Serial.println(" Millivolts");
  212.         Serial.print("Intake Air Temp  : ");
  213.         Serial.print(MAT);
  214.         Serial.println(" Deg C");
  215.         Serial.print("Engine Run Time  : ");
  216.         Serial.print(Runtime);
  217.         Serial.println(" Seconds");
  218.         // Delay for readability
  219.         unsigned long StartTimeDelay = millis();
  220.         while (millis() - StartTimeDelay < 3000) {
  221.           if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // Flush buffer
  222.         }
  223.       } else if (digitalRead(HexDataOutputPin) == LOW) {
  224.         // Send hex data
  225.         Serial.print("New Data Stream received at ");
  226.         Serial.print(millis());
  227.         Serial.print(" Calc CHECKSUM: ");
  228.         Serial.print(CheckSum, HEX);
  229.         Serial.print(" Transmitted CHECKSUM: ");
  230.         Serial.print(DataBytes[ByteCount], HEX);
  231.         if (CheckSum == DataBytes[ByteCount]) {
  232.           Serial.println(" Checksum GOOD - Data as follows: ");
  233.         } else {
  234.           Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  235.         }
  236.         int j = 1;
  237.         bytebytecounter = 0;
  238.         while (j < ByteCount + 1) {
  239.           Serial.print(DataBytes[j], HEX);
  240.           j++;
  241.           bytecounter++;
  242.           if (bytecounter >= linecount) {
  243.             bytecounter = 0;
  244.             Serial.println();
  245.           } else {
  246.             Serial.print(" ");
  247.           }
  248.         }
  249.         Serial.println();
  250.       } else {
  251.         // Send raw data
  252.         Serial.write(0x80);
  253.         Serial.write(0x95);
  254.         Serial.write(0x01);
  255.         for (int j = 1; j < ByteCount + 1; j++) {
  256.           Serial.write(DataBytes[j]);
  257.         }
  258.       }
  259.     }
  260.   }
  261. }
  262.  
  263. // Implementation of T_interpolate function
  264. float T_interpolate(byte DS_Temp) {
  265.   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};
  266.   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};
  267.   float T_Range, T_Diff, TempRange, Temperature;
  268.   int i = 0;
  269.   while (i < 38) {
  270.     if (TempScale[i] > DS_Temp) break;
  271.     i++;
  272.   }
  273.   if (i > 0) {
  274.     T_Range = TempScale[i] - TempScale[i - 1];
  275.     T_Diff = DS_Temp - TempScale[i - 1];
  276.     TempRange = TempValue[i] - TempValue[i - 1];
  277.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  278.   } else {
  279.     Temperature = TempValue[0];
  280.   }
  281.   return Temperature;
  282. }
  283.  
Advertisement
Add Comment
Please, Sign In to add comment