pleasedontcode

Vehicle Data rev_25

Jul 12th, 2025
158
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-12 21:10:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to android app */
  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. const uint8_t HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  35. const uint8_t HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  36. SoftwareSerial HC05_BluetoothModule_HC05_mySerial(HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_RX_A1, HC05_BluetoothModule_HC05_BluetoothModule_HC05_mySerial_PIN_SERIAL_TX_A0);
  37.  
  38. // Define pins for ALDL activity and data output controls
  39. const int ALDLTestPin = 2; // Example pin, set accordingly
  40. const int DecodeDataOutputPin = 3; // Example pin, set accordingly
  41. const int HexDataOutputPin = 4; // Example pin, set accordingly
  42.  
  43. // Define Mode 1 command array (example, replace with actual command)
  44. const byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Placeholder command
  45.  
  46. // Preamble bytes (example, replace with actual preamble)
  47. const byte Preamble[3] = {0x80, 0x95, 0x01};
  48.  
  49. void setup(void)
  50. {
  51.   // Initialize serial ports
  52.   Serial.begin(115200); // For debugging and output
  53.   Serial1.begin(8192);  // ALDL data at 8192 baud
  54.  
  55.   // Initialize pins
  56.   pinMode(ALDLTestPin, INPUT); // Listen for ALDL activity
  57.   pinMode(DecodeDataOutputPin, INPUT_PULLUP); // Decoded data output control
  58.   pinMode(HexDataOutputPin, INPUT_PULLUP); // Hex data output control
  59.  
  60.   // Initialize Bluetooth serial
  61.   HC05_BluetoothModule_HC05_mySerial.begin(9600);
  62.   delay(1500);
  63.   Serial.println("Ready for data capture");
  64. }
  65.  
  66. void loop() {
  67.   // Wait for silence period on the ALDL
  68.   Serial.print("wait for silence ");
  69.   bool SilenceFound = false; // Reset silence flag
  70.   unsigned long StartTime = micros(); // Start timer
  71.   while ((micros() - StartTime) < 15000) // Wait for 15 ms silence
  72.   {
  73.     if (digitalRead(ALDLTestPin) == 0)
  74.     {
  75.       StartTime = micros(); // Reset timer if activity detected
  76.     }
  77.   }
  78.   SilenceFound = true; // Silence detected
  79.  
  80.   while (SilenceFound == true)
  81.   {
  82.     bool PreambleFound = false; // Reset preamble flag
  83.     // Send Mode 1 command repeatedly
  84.     while (PreambleFound == false)
  85.     {
  86.       Serial.print(" M1 cmd ");
  87.       int i = 0;
  88.       while (i < 4)
  89.       {
  90.         Serial1.write(M1Cmd[i]);
  91.         i++;
  92.       }
  93.       Serial.println(" Finding Preamble  ");
  94.       i = 0;
  95.       unsigned long PreambleTimer = millis();
  96.       while ((millis() - PreambleTimer) < 100 && PreambleFound == false)
  97.       {
  98.         if (Serial1.available() > 0)
  99.         {
  100.           int ALDLbyte = Serial1.read();
  101.           if (ALDLbyte == Preamble[i])
  102.           {
  103.             i++;
  104.             if (i > 2) PreambleFound = true;
  105.           }
  106.           else
  107.           {
  108.             PreambleFound = false;
  109.             i = 0;
  110.           }
  111.         }
  112.       }
  113.     }
  114.     // Read data stream after preamble
  115.     int DataStreamIndex = 1;
  116.     byte DataBytes[65]; // 64 bytes + checksum
  117.     while (DataStreamIndex < 65)
  118.     {
  119.       if (Serial1.available() > 0)
  120.       {
  121.         DataBytes[DataStreamIndex] = Serial1.read();
  122.         DataStreamIndex++;
  123.       }
  124.     }
  125.     // Calculate checksum
  126.     int i = 1;
  127.     unsigned int CheckTotal = 0x80 + 0x95 + 0x01; // Sum preamble bytes
  128.     while (i < 64)
  129.     {
  130.       CheckTotal += DataBytes[i];
  131.       i++;
  132.     }
  133.     byte CheckSum = 0x200 - CheckTotal; // Two's complement
  134.     // Verify checksum
  135.     if (digitalRead(DecodeDataOutputPin) == LOW)
  136.     {
  137.       Serial.print("New Data Stream received at ");
  138.       Serial.print(millis());
  139.       Serial.print(" Calc CHECKSUM: ");
  140.       Serial.print(CheckSum, HEX);
  141.       Serial.print(" Transmitted CHECKSUM: ");
  142.       Serial.print(DataBytes[64], HEX);
  143.       if (CheckSum == DataBytes[64])
  144.       {
  145.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  146.       }
  147.       else
  148.       {
  149.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  150.       }
  151.       // Extract data
  152.       float RPM = DataBytes[11] * 25;
  153.       float TPS = DataBytes[10] * 0.019608;
  154.       float MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906;
  155.       int BLCELL = DataBytes[21];
  156.       int BLM = DataBytes[20];
  157.       int INTEGRATOR = DataBytes[22];
  158.       float InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259;
  159.       float O2mv = DataBytes[17] * 4.44;
  160.       float MAT = T_interpolate(DataBytes[30]);
  161.       unsigned int Runtime = (DataBytes[52] * 256) + DataBytes[53];
  162.  
  163.       // Prepare data string to send over Bluetooth
  164.       String dataString = "";
  165.       dataString += "Engine Speed: " + String(RPM) + " RPM\n";
  166.       dataString += "Throttle Position: " + String(TPS) + " V\n";
  167.       dataString += "Mass Air Flow: " + String(MAF) + " g/sec\n";
  168.       dataString += "Current BLM Cell: " + String(BLCELL) + " BLM Value: " + String(BLM) + "\n";
  169.       dataString += "Fuel Injector Pulse: " + String(InjPW) + " ms\n";
  170.       dataString += "O2 Sensor Voltage: " + String(O2mv) + " mV\n";
  171.       dataString += "Intake Air Temp: " + String(MAT) + " °C\n";
  172.       dataString += "Engine Runtime: " + String(Runtime) + " sec\n";
  173.  
  174.       // Send data over Bluetooth
  175.       HC05_BluetoothModule_HC05_mySerial.println(dataString);
  176.  
  177.       // Print data to Serial for debugging
  178.       Serial.print("Engine Speed     : ");
  179.       Serial.print(RPM);
  180.       Serial.println(" RPM");
  181.       Serial.print("Throttle Position: ");
  182.       Serial.print(TPS);
  183.       Serial.println(" Volts");
  184.       Serial.print("Mass Air Flow    : ");
  185.       Serial.print(MAF);
  186.       Serial.println(" Grams/Sec");
  187.       Serial.print("Current BLM Cell: ");
  188.       Serial.print(BLCELL);
  189.       Serial.print(" BLM Value: ");
  190.       Serial.print(BLM);
  191.       Serial.print("  Current Fuel Integrator: ");
  192.       Serial.println(INTEGRATOR);
  193.       Serial.print("Injector Pulse   : ");
  194.       Serial.print(InjPW);
  195.       Serial.println(" Milliseconds");
  196.       Serial.print("O2 Sensor Voltage: ");
  197.       Serial.print(O2mv);
  198.       Serial.println(" Millivolts");
  199.       Serial.print("Intake Air Temp  : ");
  200.       Serial.print(MAT);
  201.       Serial.println(" Deg C");
  202.       Serial.print("Engine Run Time  : ");
  203.       Serial.print(Runtime);
  204.       Serial.println(" Seconds");
  205.       // Delay for readability
  206.       unsigned long StartTimeDelay = millis();
  207.       while (millis() < StartTimeDelay + 3000)
  208.       {
  209.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // Flush buffer
  210.       }
  211.     }
  212.     else if (digitalRead(HexDataOutputPin) == LOW)
  213.     {
  214.       Serial.print("New Data Stream received at ");
  215.       Serial.print(millis());
  216.       Serial.print(" Calc CHECKSUM: ");
  217.       Serial.print(CheckSum, HEX);
  218.       Serial.print(" Transmitted CHECKSUM: ");
  219.       Serial.print(DataBytes[64], HEX);
  220.       if (CheckSum == DataBytes[64])
  221.       {
  222.         Serial.println(" Checksum GOOD - Data as follows: ");
  223.       }
  224.       else
  225.       {
  226.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  227.       }
  228.       // Hex dump
  229.       int j = 1;
  230.       int bytecounter = 0;
  231.       while (j <= 64)
  232.       {
  233.         Serial.print(DataBytes[j], HEX);
  234.         Serial.print(" ");
  235.         j++;
  236.         bytecounter++;
  237.         if (bytecounter >= 32)
  238.         {
  239.           bytecounter = 0;
  240.           Serial.println();
  241.         }
  242.       }
  243.       Serial.println();
  244.     }
  245.     else
  246.     {
  247.       // Raw binary stream
  248.       Serial.write(0x80);
  249.       Serial.write(0x95);
  250.       Serial.write(0x01);
  251.       for (int j = 1; j <= 64; j++)
  252.       {
  253.         Serial.write(DataBytes[j]);
  254.       }
  255.     }
  256.   }
  257. }
  258.  
  259. // Implementation of T_interpolate function
  260. float T_interpolate(byte DS_Temp)
  261. {
  262.   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};
  263.   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};
  264.   float T_Range, T_Diff, TempRange, Temperature;
  265.   int i = 0;
  266.   while (i < 38)
  267.   {
  268.     if (TempScale[i] > DS_Temp) break;
  269.     i++;
  270.   }
  271.   if (i > 0)
  272.   {
  273.     T_Range = TempScale[i] - TempScale[i - 1];
  274.     T_Diff = DS_Temp - TempScale[i - 1];
  275.     TempRange = TempValue[i] - TempValue[i - 1];
  276.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  277.   }
  278.   else
  279.   {
  280.     Temperature = TempValue[0];
  281.   }
  282.   return Temperature;
  283. }
  284.  
Advertisement
Add Comment
Please, Sign In to add comment