pleasedontcode

Vehicle Data rev_17

Jul 11th, 2025
393
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:16:56
  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 temperature interpolation function
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. // Bluetooth module HC05 connected to pins A0 (TX) and A1 (RX)
  35. const uint8_t HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  36. const uint8_t HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  37. SoftwareSerial HC05_Bluetooth_Module_HC05_mySerial(HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_RX_A1, HC05_Bluetooth_Module_HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_TX_A0);
  38.  
  39. void setup(void)
  40. {
  41.   // Initialize Bluetooth serial communication at 9600 baud
  42.   HC05_Bluetooth_Module_HC05_mySerial.begin(9600);
  43. }
  44.  
  45. void loop(void)
  46. {
  47.   // Variable declarations for data handling
  48.   const byte ALDLTestPin = 4; // Input pin for ALDL activity detection
  49.   const byte DecodeDataOutputPin = 5; // Pin to output decoded data
  50.   const byte HexDataOutputPin = 6; // Pin to output hex data
  51.   int ALDLbyte = 0; // Byte of ALDL data
  52.   int bytecounter = 0; // Byte counter for formatting output
  53.   const int linecount = 64; // Bytes per line for hex output
  54.   byte M1Cmd[4] = {0x80, 0x56, 0x01, 0x29}; // Mode 1 command
  55.   byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble indicating start of data stream
  56.   bool PreambleFound = false;
  57.   bool SilenceFound = false;
  58.   bool CommInit8192 = false;
  59.   const double SilenceWait = 15000; // Silence wait in microseconds
  60.   unsigned long PreambleTimer;
  61.   const int ByteCount = 64; // Total bytes in data packet including checksum
  62.   byte DataBytes[ByteCount + 1]; // Data buffer (indexing from 1 for convenience)
  63.   int DataStreamIndex = 1;
  64.   int i = 0;
  65.   unsigned long StartTime;
  66.   unsigned int CheckTotal;
  67.   byte CheckSum;
  68.  
  69.   // Variables for decoded data
  70.   float RPM, TPS, MAF, InjPW, O2mv, MAT;
  71.   int BLCELL, BLM, INTEGRATOR;
  72.   unsigned int Runtime;
  73.  
  74.   // Wait for silence on ALDL line
  75.   Serial.print("wait for silence ");
  76.   SilenceFound = false;
  77.   StartTime = micros();
  78.   while ((micros() - StartTime) < 15000)
  79.   {
  80.     if (digitalRead(ALDLTestPin) == 0)
  81.     {
  82.       StartTime = micros();
  83.     }
  84.   }
  85.   SilenceFound = true;
  86.  
  87.   // Main loop: continuously request and process data
  88.   while (SilenceFound)
  89.   {
  90.     PreambleFound = false;
  91.  
  92.     // Send Mode 1 command and look for preamble
  93.     while (!PreambleFound)
  94.     {
  95.       Serial.print(" M1 cmd ");
  96.       for (i = 0; i < 4; i++)
  97.       {
  98.         Serial1.write(M1Cmd[i]);
  99.       }
  100.       Serial.println(" Finding Preamble  ");
  101.       i = 0;
  102.       PreambleTimer = millis();
  103.       while ((millis() - PreambleTimer) < 100 && !PreambleFound)
  104.       {
  105.         if (Serial1.available() > 0)
  106.         {
  107.           ALDLbyte = Serial1.read();
  108.           if (ALDLbyte == Preamble[i])
  109.           {
  110.             i++;
  111.             if (i > 2)
  112.               PreambleFound = true;
  113.           }
  114.           else
  115.           {
  116.             PreambleFound = false;
  117.             i = 0;
  118.           }
  119.         }
  120.       }
  121.     }
  122.  
  123.     // Read complete data packet
  124.     DataStreamIndex = 1;
  125.     while (DataStreamIndex <= ByteCount)
  126.     {
  127.       if (Serial1.available() > 0)
  128.       {
  129.         DataBytes[DataStreamIndex] = Serial1.read();
  130.         DataStreamIndex++;
  131.       }
  132.     }
  133.  
  134.     // Calculate checksum
  135.     i = 1;
  136.     CheckTotal = 0x80 + 0x95 + 0x01;
  137.     while (i < ByteCount)
  138.     {
  139.       CheckTotal += DataBytes[i];
  140.       i++;
  141.     }
  142.     CheckSum = 0x200 - CheckTotal;
  143.  
  144.     // Send data to Android via Serial
  145.     if (digitalRead(DecodeDataOutputPin) == LOW)
  146.     {
  147.       Serial.print("New Data Stream received at ");
  148.       Serial.print(millis());
  149.       Serial.print(" Calc CHECKSUM: ");
  150.       Serial.print(CheckSum, HEX);
  151.       Serial.print(" Transmitted CHECKSUM: ");
  152.       Serial.print(DataBytes[ByteCount], HEX);
  153.       if (CheckSum == DataBytes[ByteCount])
  154.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  155.       else
  156.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  157.  
  158.       // Parse data
  159.       RPM = DataBytes[11] * 25;
  160.       TPS = DataBytes[10] * 0.019608;
  161.       MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906;
  162.       BLCELL = DataBytes[21];
  163.       BLM = DataBytes[20];
  164.       INTEGRATOR = DataBytes[22];
  165.       InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259;
  166.       O2mv = DataBytes[17] * 4.44;
  167.       MAT = T_interpolate(DataBytes[30]);
  168.       Runtime = (DataBytes[52] * 256 + DataBytes[53]);
  169.  
  170.       // Output parsed data
  171.       Serial.print("Engine Speed     : ");
  172.       Serial.print(RPM);
  173.       Serial.println(" RPM");
  174.       Serial.print("Throttle Position: ");
  175.       Serial.print(TPS);
  176.       Serial.println(" Volts");
  177.       Serial.print("Mass Air Flow    : ");
  178.       Serial.print(MAF);
  179.       Serial.println(" Grams/Sec");
  180.       Serial.print("Current BLM Cell: ");
  181.       Serial.print(BLCELL);
  182.       Serial.print(" BLM Value: ");
  183.       Serial.print(BLM);
  184.       Serial.print("  Current Fuel Integrator: ");
  185.       Serial.println(INTEGRATOR);
  186.       Serial.print("Injector Pulse   : ");
  187.       Serial.print(InjPW);
  188.       Serial.println(" Milliseconds");
  189.       Serial.print("O2 Sensor Voltage: ");
  190.       Serial.print(O2mv);
  191.       Serial.println(" Millivolts");
  192.       Serial.print("Intake Air Temp  : ");
  193.       Serial.print(MAT);
  194.       Serial.println(" Deg C");
  195.       Serial.print("Engine Run Time  : ");
  196.       Serial.print(Runtime);
  197.       Serial.println(" Seconds");
  198.  
  199.       // Delay for readability and buffer purge
  200.       unsigned long StartTimeDelay = millis();
  201.       while (millis() < StartTimeDelay + 3000)
  202.       {
  203.         if (Serial1.available() > 0)
  204.           ALDLbyte = Serial1.read(); // Flush buffer
  205.       }
  206.     }
  207.     else if (digitalRead(HexDataOutputPin) == LOW)
  208.     {
  209.       // Send hex data
  210.       Serial.print("New Data Stream received at ");
  211.       Serial.print(millis());
  212.       Serial.print(" Calc CHECKSUM: ");
  213.       Serial.print(CheckSum, HEX);
  214.       Serial.print(" Transmitted CHECKSUM: ");
  215.       Serial.print(DataBytes[ByteCount], HEX);
  216.       if (CheckSum == DataBytes[ByteCount])
  217.         Serial.println(" Checksum GOOD - Data as follows: ");
  218.       else
  219.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  220.  
  221.       int j = 1;
  222.       bytecounter = 0;
  223.       while (j <= ByteCount)
  224.       {
  225.         Serial.print(DataBytes[j], HEX);
  226.         j++;
  227.         bytecounter++;
  228.         if (bytecounter >= linecount)
  229.         {
  230.           bytecounter = 0;
  231.           Serial.println();
  232.         }
  233.         else
  234.         {
  235.           Serial.print(" ");
  236.         }
  237.       }
  238.       Serial.println();
  239.     }
  240.     else
  241.     {
  242.       // Raw binary stream
  243.       Serial.write(0x80);
  244.       Serial.write(0x95);
  245.       Serial.write(0x01);
  246.       for (int j = 1; j <= ByteCount; j++)
  247.       {
  248.         Serial.write(DataBytes[j]);
  249.       }
  250.     }
  251.   }
  252. }
  253.  
  254. // Subroutine to interpolate MAT temperature
  255. float T_interpolate(byte DS_Temp)
  256. {
  257.   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};
  258.   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};
  259.   float T_Range, T_Diff, TempRange, Temperature;
  260.   int i = 0;
  261.   while (i < 38)
  262.   {
  263.     if (TempScale[i] > DS_Temp)
  264.       break;
  265.     i++;
  266.   }
  267.   if (i > 0)
  268.   {
  269.     T_Range = TempScale[i] - TempScale[i - 1];
  270.     T_Diff = DS_Temp - TempScale[i - 1];
  271.     TempRange = TempValue[i] - TempValue[i - 1];
  272.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  273.   }
  274.   else
  275.   {
  276.     Temperature = TempValue[0];
  277.   }
  278.   return Temperature;
  279. }
  280.  
Advertisement
Add Comment
Please, Sign In to add comment