pleasedontcode

Vehicle Diagnostics rev_16

Jul 11th, 2025
424
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 Diagnostics
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-07-11 22:03:20
  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 T_interpolate function
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. const uint8_t HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  35. const uint8_t HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  36. SoftwareSerial HC05_Bluetooth_Module_HC05_mySerial(HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_RX_A1, HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_TX_A0);
  37.  
  38. void setup(void)
  39. {
  40.   // Initialize Bluetooth serial communication
  41.   HC05_Bluetooth_Module_HC05_mySerial.begin(9600);
  42. }
  43.  
  44. void loop(void)
  45. {
  46.   // *** USER CODE START ***
  47.   // Variables for user code
  48.   const byte ALDLTestPin = 4; // Input used to listen for 160 BAUD ALDL activity before commanding 8192 BAUD
  49.   const byte DecodeDataOutputPin = 5; // Input pin for decoded data output control
  50.   const byte HexDataOutputPin = 6; // Input pin for hex data output control
  51.  
  52.   int ALDLbyte = 0; // One byte of ALDL data
  53.   int bytecounter = 0; // count bytes for line spacing in output
  54.   const int linecount = 64; // 32 bytes per line
  55.   byte M1Cmd[4] = {0x80, 0x56, 0x01, 0x29}; // Mode 1 command to start 8192 Mode 1 DataStream (80 56 01 29 HEX)
  56.   byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble from ECM indicates start of 8192 Mode 1 DataStream (80 95 01 HEX)
  57.   bool PreambleFound = false; // Reset preamble found flag
  58.   bool SilenceFound = false; // Flag to indicate ECM silence in 160 baud mode
  59.   bool CommInit8192 = false; // Flag for 8192 baud communication
  60.   const double SilenceWait = 15000; // Minimum silence period before transmitting Mode 1 command (ms)
  61.   unsigned long PreambleTimer; // Timeout timer for preamble
  62.   const int ByteCount = 64; // Number of data bytes following preamble
  63.   byte DataBytes[ByteCount]; // Array to hold serial data stream
  64.   int DataStreamIndex = 1; // Data stream byte index
  65.   int i = 0; // Preamble index counter
  66.   unsigned long StartTime; // Timer for measuring incoming data
  67.   unsigned int CheckTotal; // Total for checksum calculation
  68.   byte CheckSum; // Calculated checksum
  69.  
  70.   // Variables for decoded data
  71.   float RPM = 0;
  72.   float TPS = 0;
  73.   float MAF = 0;
  74.   float InjPW = 0;
  75.   float O2mv = 0;
  76.   int BLCELL = 0;
  77.   int BLM = 0;
  78.   int INTEGRATOR = 0;
  79.   float MAT = 0;
  80.   unsigned int Runtime = 0;
  81.  
  82.   // *** Wait for silence on ALDL line
  83.   Serial.print("wait for silence ");
  84.   SilenceFound = false;
  85.   StartTime = micros();
  86.   while ((micros() - StartTime) < 15000) // wait 15ms for silence
  87.   {
  88.     if (digitalRead(ALDLTestPin) == 0)
  89.     {
  90.       StartTime = micros(); // reset timer if activity detected
  91.     }
  92.   }
  93.   SilenceFound = true;
  94.  
  95.   while (SilenceFound)
  96.   {
  97.     // Send Mode 1 command over Bluetooth serial
  98.     PreambleFound = false;
  99.     while (!PreambleFound)
  100.     {
  101.       Serial.print(" M1 cmd ");
  102.       i = 0;
  103.       while (i < 4)
  104.       {
  105.         Serial1.write(M1Cmd[i]);
  106.         i++;
  107.       }
  108.       Serial.println(" Finding Preamble ");
  109.       i = 0;
  110.       PreambleTimer = millis();
  111.       while ((millis() - PreambleTimer) < 100 && !PreambleFound)
  112.       {
  113.         if (Serial1.available() > 0)
  114.         {
  115.           ALDLbyte = Serial1.read();
  116.           if (ALDLbyte == Preamble[i])
  117.           {
  118.             i++;
  119.             if (i > 2) PreambleFound = true;
  120.           }
  121.           else
  122.           {
  123.             PreambleFound = false;
  124.             i = 0;
  125.           }
  126.         }
  127.       }
  128.     }
  129.  
  130.     // Read data stream after preamble
  131.     DataStreamIndex = 1;
  132.     while (DataStreamIndex < 65)
  133.     {
  134.       if (Serial1.available() > 0)
  135.       {
  136.         DataBytes[DataStreamIndex] = Serial1.read();
  137.         DataStreamIndex++;
  138.       }
  139.     }
  140.  
  141.     // Calculate checksum
  142.     i = 1;
  143.     CheckTotal = 0x80 + 0x95 + 0x01; // sum preamble bytes
  144.     while (i < ByteCount)
  145.     {
  146.       CheckTotal += DataBytes[i];
  147.       i++;
  148.     }
  149.     CheckSum = 0x200 - CheckTotal; // two's complement
  150.  
  151.     // Send data to serial port based on control pins
  152.     if (digitalRead(DecodeDataOutputPin) == LOW)
  153.     {
  154.       // Output decoded data
  155.       Serial.print("New Data Stream received at ");
  156.       Serial.print(millis());
  157.       Serial.print(" Calc CHECKSUM: ");
  158.       Serial.print(CheckSum, HEX);
  159.       Serial.print(" Transmitted CHECKSUM: ");
  160.       Serial.print(DataBytes[63], HEX); // last byte
  161.       if (CheckSum == DataBytes[63])
  162.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  163.       else
  164.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  165.  
  166.       // Parse data bytes
  167.       RPM = DataBytes[11] * 25; // RPM
  168.       TPS = DataBytes[10] * 0.019608; // TPS volts
  169.       MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906; // MAF gm/sec
  170.       BLCELL = DataBytes[21];
  171.       BLM = DataBytes[20];
  172.       INTEGRATOR = DataBytes[22];
  173.       InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259; // ms
  174.       O2mv = DataBytes[17] * 4.44; // mV
  175.       MAT = T_interpolate(DataBytes[30]);
  176.       Runtime = (DataBytes[52] * 256) + DataBytes[53];
  177.  
  178.       // Display data
  179.       Serial.print("Engine Speed     : ");
  180.       Serial.print(RPM);
  181.       Serial.println(" RPM");
  182.       Serial.print("Throttle Position: ");
  183.       Serial.print(TPS);
  184.       Serial.println(" Volts");
  185.       Serial.print("Mass Air Flow    : ");
  186.       Serial.print(MAF);
  187.       Serial.println(" Grams/Sec");
  188.       Serial.print("Current BLM Cell: ");
  189.       Serial.print(BLCELL);
  190.       Serial.print(" BLM Value: ");
  191.       Serial.print(BLM);
  192.       Serial.print("  Current Fuel Integrator: ");
  193.       Serial.println(INTEGRATOR);
  194.       Serial.print("Injector Pulse   : ");
  195.       Serial.print(InjPW);
  196.       Serial.println(" Milliseconds");
  197.       Serial.print("O2 Sensor Voltage: ");
  198.       Serial.print(O2mv);
  199.       Serial.println(" Millivolts");
  200.       Serial.print("Intake Air Temp  : ");
  201.       Serial.print(MAT);
  202.       Serial.println(" Deg C");
  203.       Serial.print("Engine Run Time  : ");
  204.       Serial.print(Runtime);
  205.       Serial.println(" Seconds");
  206.  
  207.       // Delay for user reading
  208.       unsigned long StartTimeDelay = millis();
  209.       while (millis() < StartTimeDelay + 3000)
  210.       {
  211.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // flush buffer
  212.       }
  213.     }
  214.     else if (digitalRead(HexDataOutputPin) == LOW)
  215.     {
  216.       // Output hex data
  217.       Serial.print("New Data Stream received at ");
  218.       Serial.print(millis());
  219.       Serial.print(" Calc CHECKSUM: ");
  220.       Serial.print(CheckSum, HEX);
  221.       Serial.print(" Transmitted CHECKSUM: ");
  222.       Serial.print(DataBytes[63], HEX);
  223.       if (CheckSum == DataBytes[63])
  224.         Serial.println(" Checksum GOOD - Data as follows: ");
  225.       else
  226.         Serial.println(" Checksum *** ERROR *** -  Data as follows: ");
  227.  
  228.       int j = 1;
  229.       bytecounter = 0;
  230.       while (j < ByteCount + 1)
  231.       {
  232.         Serial.print(DataBytes[j], HEX);
  233.         j++;
  234.         bytecounter++;
  235.         if (bytecounter >= linecount)
  236.         {
  237.           bytecounter = 0;
  238.           Serial.println();
  239.         }
  240.         else
  241.         {
  242.           Serial.print(" ");
  243.         }
  244.       }
  245.       Serial.println();
  246.     }
  247.     else
  248.     {
  249.       // Send raw data stream over Bluetooth serial
  250.       Serial.write(0x80);
  251.       Serial.write(0x95);
  252.       Serial.write(0x01);
  253.       for (int j = 1; j < ByteCount + 1; j++)
  254.       {
  255.         Serial.write(DataBytes[j]);
  256.       }
  257.     }
  258.   }
  259. }
  260.  
  261. // Implementation of T_interpolate function
  262. float T_interpolate(byte DS_Temp)
  263. {
  264.   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};
  265.   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};
  266.   float T_Range, T_Diff, TempRange, Temperature;
  267.   int i = 0;
  268.   while (i < 38)
  269.   {
  270.     if (TempScale[i] > DS_Temp) break;
  271.     i++;
  272.   }
  273.   if (i > 0)
  274.   {
  275.     T_Range = TempScale[i] - TempScale[i - 1];
  276.     T_Diff = DS_Temp - TempScale[i - 1];
  277.     TempRange = TempValue[i] - TempValue[i - 1];
  278.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  279.   }
  280.   else
  281.   {
  282.     Temperature = TempValue[0];
  283.   }
  284.   return Temperature;
  285. }
  286. // *** USER CODE END ***
  287.  
  288. }
  289.  
Advertisement
Add Comment
Please, Sign In to add comment