pleasedontcode

Vehicle Data rev_19

Jul 11th, 2025
421
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:25: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); // Added prototype for the interpolation function
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. const uint8_t HC05_Bluetooth_TX_PIN = A0; // Bluetooth TX pin
  35. const uint8_t HC05_Bluetooth_RX_PIN = A1; // Bluetooth RX pin
  36. SoftwareSerial HC05(HC05_Bluetooth_TX_PIN, HC05_Bluetooth_RX_PIN);
  37.  
  38. // Define other variables used in the code
  39. // Assuming these are declared globally as in the original code
  40. // For example:
  41. int ALDLTestPin = 2; // Example pin, replace with actual
  42. int DecodeDataOutputPin = 3; // Example pin, replace with actual
  43. int HexDataOutputPin = 4; // Example pin, replace with actual
  44. byte M1Cmd[4] = {0x01, 0x02, 0x03, 0x04}; // Example command, replace with actual
  45. byte Preamble[3] = {0x55, 0xAA, 0xFF}; // Example preamble
  46. byte ALDLbyte;
  47. int i, DataStreamIndex, ByteCount = 64; // Example value
  48. byte DataBytes[65];
  49. unsigned long PreambleTimer, StartTime, PreambleFound, SilenceFound, CommInit8192;
  50. float RPM, TPS, MAF, BLCELL, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime;
  51. int linecount = 16; // Example for hex output line count
  52. int bytecounter;
  53.  
  54. // Interpolation function
  55. float T_interpolate(byte DS_Temp)
  56. {
  57.   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};
  58.   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};
  59.   float T_Range;
  60.   float T_Diff;
  61.   float TempRange;
  62.   float Temperature;
  63.   int i = 0;
  64.   while (i < 38)
  65.   {
  66.     if (TempScale[i] > DS_Temp) break;
  67.     i++;
  68.   }
  69.   if (i > 0)
  70.   {
  71.     T_Range = TempScale[i] - TempScale[i - 1];
  72.     T_Diff = DS_Temp - TempScale[i - 1];
  73.     TempRange = TempValue[i] - TempValue[i - 1];
  74.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  75.   }
  76.   else
  77.   {
  78.     Temperature = TempValue[0];
  79.   }
  80.   return Temperature;
  81. }
  82.  
  83. void setup(void)
  84. {
  85.   // put your setup code here, to run once:
  86.   HC05.begin(9600);
  87.   Serial.begin(115200); // Initialize Serial for debugging and output
  88.   // Initialize pins
  89.   pinMode(ALDLTestPin, INPUT); // ALDL activity detection pin
  90.   pinMode(DecodeDataOutputPin, INPUT_PULLUP); // Decoded data output control
  91.   pinMode(HexDataOutputPin, INPUT_PULLUP); // Hex data output control
  92.  
  93.   // Initialize variables
  94.   i = 0; // Reset preamble index
  95.   PreambleFound = false;
  96.   SilenceFound = false;
  97.   CommInit8192 = false;
  98.   StartTime = 0;
  99.   PreambleTimer = 0;
  100.   bytecounter = 0;
  101. }
  102.  
  103. void loop() {
  104.   // Wait for silence period on the ALDL
  105.   Serial.print("wait for silence ");
  106.   SilenceFound = false; // Reset silence flag
  107.   unsigned long StartTimeMicros = micros(); // First look for an active signal or a timeout - initialize timer
  108.   // Should exit this loop on the start of a bit
  109.   while ((micros() - StartTimeMicros) < 15000) // Wait for a 15 ms silent period
  110.   {
  111.     if (digitalRead(ALDLTestPin) == 0) // Any line activity resets the start time
  112.     {
  113.       StartTimeMicros = micros(); // Timing starts over
  114.     }
  115.   }
  116.   SilenceFound = true; // Set the silence flag on exit
  117.  
  118.   while (SilenceFound == true) // While silence found flag is set, continuously request and transmit Mode 1 data
  119.   {
  120.     PreambleFound = false; // Reset preamble found flag
  121.  
  122.     while (PreambleFound == false) // First look at data until the preamble has been found will read data forever until a preamble is read
  123.     {
  124.       // Send Mode 1 command
  125.       Serial.print(" M1 cmd ");
  126.       i = 0; // use bytecounter to send the outgoing Mode1CMD sequence
  127.       while (i < 4)
  128.       {
  129.         Serial1.write(M1Cmd[i]); // sends 1 byte of the command sequence
  130.         i++;
  131.       }
  132.       Serial.println(" Finding Preamble  ");
  133.       i = 0; // Then reset byte counter and scan incoming data for the preamble
  134.       PreambleTimer = millis(); // Initialize timer to detect timeout
  135.       while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false))
  136.       {
  137.         if (Serial1.available() > 0)
  138.         {
  139.           ALDLbyte = Serial1.read();
  140.           if (ALDLbyte == Preamble[i])
  141.           {
  142.             i++; // Increment the preamble index
  143.             if (i > 2) PreambleFound = true; // Preamble found
  144.           }
  145.           else
  146.           {
  147.             PreambleFound = false; // Reset preamble found flag
  148.             i = 0; // Reset the preamble index
  149.           }
  150.         }
  151.       }
  152.     }
  153.     // Read data stream
  154.     DataStreamIndex = 1; // Start at index 1
  155.     while (DataStreamIndex < 65) // Read 64 bytes (including checksum)
  156.     {
  157.       if (Serial1.available() > 0)
  158.       {
  159.         DataBytes[DataStreamIndex] = Serial1.read();
  160.         DataStreamIndex++;
  161.       }
  162.     }
  163.     // Calculate checksum
  164.     i = 1;
  165.     CheckTotal = 0x80 + 0x95 + 0x01; // Sum preamble bytes
  166.     while (i < ByteCount)
  167.     {
  168.       CheckTotal += DataBytes[i];
  169.       i++;
  170.     }
  171.     CheckSum = 0x200 - CheckTotal; // Two's complement
  172.  
  173.     // Send data over Bluetooth to Android
  174.     // For simplicity, send parsed data as a comma-separated string
  175.     // Parse data
  176.     RPM = DataBytes[11] * 25; // Engine RPM
  177.     TPS = DataBytes[10] * 0.019608; // TPS volts
  178.     MAF = ((DataBytes[36] * 256) + (DataBytes[37])) * 0.003906; // MAF gm/sec
  179.     BLCELL = DataBytes[21];
  180.     BLM = DataBytes[20];
  181.     INTEGRATOR = DataBytes[22];
  182.     InjPW = ((DataBytes[45] * 256) + (DataBytes[46])) * 0.015259;
  183.     O2mv = DataBytes[17] * 4.44;
  184.     MAT = T_interpolate(DataBytes[30]);
  185.     Runtime = (DataBytes[52] * 256 + DataBytes[53]);
  186.  
  187.     // Prepare string to send over Bluetooth
  188.     String dataString = "";
  189.     dataString += "RPM:" + String(RPM) + ",";
  190.     dataString += "TPS:" + String(TPS) + ",";
  191.     dataString += "MAF:" + String(MAF) + ",";
  192.     dataString += "BLCELL:" + String(BLCELL) + ",";
  193.     dataString += "BLM:" + String(BLM) + ",";
  194.     dataString += "INTEGRATOR:" + String(INTEGRATOR) + ",";
  195.     dataString += "InjPW:" + String(InjPW) + ",";
  196.     dataString += "O2mv:" + String(O2mv) + ",";
  197.     dataString += "MAT:" + String(MAT) + ",";
  198.     dataString += "Runtime:" + String(Runtime);
  199.  
  200.     // Send over Bluetooth
  201.     HC05.println(dataString);
  202.  
  203.     // Existing code for checksum verification and output
  204.     if (digitalRead(DecodeDataOutputPin) == LOW)
  205.     {
  206.       // Decoded data output
  207.       Serial.print("New Data Stream received at ");
  208.       Serial.print(millis());
  209.       Serial.print(" Calc CHECKSUM: ");
  210.       Serial.print(CheckSum, HEX);
  211.       Serial.print(" Transmitted CHECKSUM: ");
  212.       Serial.print(DataBytes[ByteCount], HEX);
  213.       if (CheckSum == DataBytes[ByteCount])
  214.       {
  215.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  216.       }
  217.       else
  218.       {
  219.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  220.       }
  221.       // Parse data (already done above)
  222.       // Print parsed data
  223.       Serial.print("Engine Speed     : ");
  224.       Serial.print(RPM);
  225.       Serial.println(" RPM");
  226.       Serial.print("Throttle Position: ");
  227.       Serial.print(TPS);
  228.       Serial.println(" Volts");
  229.       Serial.print("Mass Air Flow    : ");
  230.       Serial.print(MAF);
  231.       Serial.println(" Grams/Sec");
  232.       Serial.print("Current BLM Cell: ");
  233.       Serial.print(BLCELL);
  234.       Serial.print(" BLM Value: ");
  235.       Serial.print(BLM);
  236.       Serial.print("  Current Fuel Integrator: ");
  237.       Serial.println(INTEGRATOR);
  238.       Serial.print("Injector Pulse   : ");
  239.       Serial.print(InjPW);
  240.       Serial.println(" Milliseconds");
  241.       Serial.print("O2 Sensor Voltage: ");
  242.       Serial.print(O2mv);
  243.       Serial.println(" Millivolts");
  244.       Serial.print("Intake Air Temp  : ");
  245.       Serial.print(MAT);
  246.       Serial.println(" Deg C");
  247.       Serial.print("Engine Run Time  : ");
  248.       Serial.print(Runtime);
  249.       Serial.println(" Seconds");
  250.       // Delay for readability
  251.       unsigned long StartTime = millis();
  252.       while (millis() < StartTime + 3000)
  253.       {
  254.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // Flush buffer
  255.       }
  256.     }
  257.     else if (digitalRead(HexDataOutputPin) == LOW)
  258.     {
  259.       // Hex data output
  260.       Serial.print("New Data Stream received at ");
  261.       Serial.print(millis());
  262.       Serial.print(" Calc CHECKSUM: ");
  263.       Serial.print(CheckSum, HEX);
  264.       Serial.print(" Transmitted CHECKSUM: ");
  265.       Serial.print(DataBytes[ByteCount], HEX);
  266.       if (CheckSum == DataBytes[ByteCount])
  267.       {
  268.         Serial.println(" Checksum GOOD - Data as follows: ");
  269.       }
  270.       else
  271.       {
  272.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  273.       }
  274.       int j = 1;
  275.       bytecounter = 0;
  276.       while (j < ByteCount + 1)
  277.       {
  278.         Serial.print(DataBytes[j], HEX);
  279.         j++;
  280.         bytecounter++;
  281.         if (bytecounter >= linecount)
  282.         {
  283.           bytecounter = 0;
  284.           Serial.println();
  285.         }
  286.         else
  287.         {
  288.           Serial.print(" ");
  289.         }
  290.       }
  291.       Serial.println();
  292.     }
  293.     else
  294.     {
  295.       // Raw binary stream
  296.       Serial.write(0x80);
  297.       Serial.write(0x95);
  298.       Serial.write(0x01);
  299.       for (int j = 1; j < ByteCount + 1; j++)
  300.       {
  301.         Serial.write(DataBytes[j]);
  302.       }
  303.     }
  304.   }
  305. }
  306.  
Advertisement
Add Comment
Please, Sign In to add comment