Advertisement
pleasedontcode

Vehicle Diagnostics rev_08

Jul 11th, 2025
111
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 21:24:37
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to tunerpro */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. float T_interpolate(byte DS_Temp); // Prototype for the interpolation function
  31.  
  32. void setup()
  33. {
  34.   // **** I/O configuration and setup first
  35.   pinMode(ALDLTestPin, INPUT);                                              // define D4 as an input pin to listen for the 160 baud input data
  36.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);                                // User convenience pin.  Grounding this pin will send Decoded Data to the Serial Port
  37.   pinMode(HexDataOutputPin, INPUT_PULLUP);                                   // User convenience pin.  Grounding this pin will send HEX Data to the Serial Port
  38.   // **** Now, start the serial functions
  39.   Serial.begin(115200);                                                     // Open serial monitoring port
  40.   Serial1.begin(8192);                                                      // Test the capability of esp 8222 to run at 8192 baud directly
  41.   delay(1500);                                                              // delay for diagnostic print
  42.   Serial.println("Ready for data capture");
  43.   // Initialize variables if needed
  44.   int i=0;                                                                    // Reset the preamble index flag
  45. }
  46.  
  47. void loop() {
  48.   // Forward Bluetooth data from Serial to Serial1 (TunerPro)
  49.   while (Serial.available() > 0) {
  50.     int incomingByte = Serial.read();
  51.     Serial1.write(incomingByte);
  52.   }
  53.  
  54.   // Wait for silence period on the ALDL
  55.   Serial.print("wait for silence ");
  56.   bool SilenceFound = false;                                                    // Reset silence flag
  57.   unsigned long StartTime= micros();                                              // First look for an active signal or a timeout - initialize timer
  58.   while ((micros() - StartTime) < 15000) {                                         // Wait for a 15 ms silent period
  59.     if (digitalRead(ALDLTestPin) == 0) {                                             // Any line activity resets the start time
  60.       StartTime= micros();                                                         // Timing starts over
  61.     }
  62.   }
  63.   SilenceFound = true;                                                              // Set the silence flag on exit
  64.  
  65.   while (SilenceFound == true) {                                                      // While silence found flag is set, continuously request and transmit Mode 1 data
  66.     bool PreambleFound = false;                                                         // Reset preamble found flag
  67.     while (PreambleFound == false) {                                                      // First look at data until the preamble has been found
  68.       Serial.print(" M1 cmd ");
  69.       byte M1Cmd[4] = {0x80, 0x56, 0x01, 0x29}; // Mode 1 command to start 8192 Mode 1 DataStream
  70.       for (int i=0; i<4; i++) {
  71.         Serial1.write(M1Cmd[i]); // Send command bytes
  72.       }
  73.       Serial.println(" Finding Preamble  ");
  74.       int i=0; // Preamble index
  75.       unsigned long PreambleTimer = millis(); // Initialize timer
  76.       while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false)) {
  77.         if (Serial1.available() > 0) {
  78.           int ALDLbyte = Serial1.read();
  79.           if (ALDLbyte == Preamble[i]) {
  80.             i++;
  81.             if (i > 2) PreambleFound = true; // Preamble found
  82.           } else {
  83.             PreambleFound = false;
  84.             i=0;
  85.           }
  86.         }
  87.       }
  88.     }
  89.     // Read data stream after preamble
  90.     const int ByteCount = 64;
  91.     byte DataBytes[ByteCount+1]; // 1-based indexing for clarity
  92.     int DataStreamIndex = 1;
  93.     while (DataStreamIndex <= ByteCount) {
  94.       if (Serial1.available() > 0) {
  95.         DataBytes[DataStreamIndex] = Serial1.read();
  96.         DataStreamIndex++;
  97.       }
  98.     }
  99.     // Calculate checksum
  100.     int i=1;
  101.     unsigned long CheckTotal = 0x80 + 0x95 + 0x01; // sum preamble bytes
  102.     while (i <= ByteCount) {
  103.       CheckTotal += DataBytes[i];
  104.       i++;
  105.     }
  106.     byte CheckSum = (byte)(0x200 - CheckTotal); // Two's complement
  107.     // Verify checksum and output data
  108.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  109.       Serial.print("New Data Stream received at ");
  110.       Serial.print(millis());
  111.       Serial.print(" Calc CHECKSUM: ");
  112.       Serial.print(CheckSum, HEX);
  113.       Serial.print(" Transmitted CHECKSUM: ");
  114.       Serial.print(DataBytes[ByteCount], HEX);
  115.       if (CheckSum == DataBytes[ByteCount]) {
  116.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  117.       } else {
  118.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  119.       }
  120.       // Decode data
  121.       float RPM = DataBytes[11] * 25; // Engine RPM
  122.       float TPS = DataBytes[10] * 0.019608; // TPS volts
  123.       float MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906; // MAF gm/sec
  124.       int BLCELL = DataBytes[21];
  125.       int BLM = DataBytes[20];
  126.       int INTEGRATOR = DataBytes[22];
  127.       float InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259; // ms
  128.       float O2mv = DataBytes[17] * 4.44; // mV
  129.       float MAT = T_interpolate(DataBytes[30]);
  130.       unsigned int Runtime = (DataBytes[52] * 256) + DataBytes[53];
  131.  
  132.       // Print decoded data
  133.       Serial.print("Engine Speed     : ");
  134.       Serial.print(RPM);
  135.       Serial.println(" RPM");
  136.       Serial.print("Throttle Position: ");
  137.       Serial.print(TPS);
  138.       Serial.println(" Volts");
  139.       Serial.print("Mass Air Flow    : ");
  140.       Serial.print(MAF);
  141.       Serial.println(" Grams/Sec");
  142.       Serial.print("Current BLM Cell: ");
  143.       Serial.print(BLCELL);
  144.       Serial.print(" BLM Value: ");
  145.       Serial.print(BLM);
  146.       Serial.print("  Current Fuel Integrator: ");
  147.       Serial.println(INTEGRATOR);
  148.       Serial.print("Injector Pulse   : ");
  149.       Serial.print(InjPW);
  150.       Serial.println(" Milliseconds");
  151.       Serial.print("O2 Sensor Voltage: ");
  152.       Serial.print(O2mv);
  153.       Serial.println(" Millivolts");
  154.       Serial.print("Intake Air Temp  : ");
  155.       Serial.print(MAT);
  156.       Serial.println(" Deg C");
  157.       Serial.print("Engine Run Time  : ");
  158.       Serial.print(Runtime);
  159.       Serial.println(" Seconds");
  160.       // Delay for user to read
  161.       unsigned long StartTime = millis();
  162.       while (millis() < StartTime + 3000) {
  163.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // flush buffer
  164.       }
  165.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  166.       // Hex output mode
  167.       Serial.print("New Data Stream received at ");
  168.       Serial.print(millis());
  169.       Serial.print(" Calc CHECKSUM: ");
  170.       Serial.print(CheckSum, HEX);
  171.       Serial.print(" Transmitted CHECKSUM: ");
  172.       Serial.print(DataBytes[ByteCount], HEX);
  173.       if (CheckSum == DataBytes[ByteCount]) {
  174.         Serial.println(" Checksum GOOD - Data as follows: ");
  175.       } else {
  176.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  177.       }
  178.       // Print hex data
  179.       int j=1;
  180.       int bytecounter=0;
  181.       while (j<=ByteCount) {
  182.         Serial.print(DataBytes[j], HEX);
  183.         Serial.print(" ");
  184.         j++;
  185.         bytecounter++;
  186.         if (bytecounter >= linecount) {
  187.           bytecounter=0;
  188.           Serial.println();
  189.         }
  190.       }
  191.       Serial.println();
  192.     } else {
  193.       // Raw binary data
  194.       Serial.write(0x80);
  195.       Serial.write(0x95);
  196.       Serial.write(0x01);
  197.       for (int j=1; j<=ByteCount; j++) {
  198.         Serial.write(DataBytes[j]);
  199.       }
  200.     }
  201.   }
  202. }
  203.  
  204. // Implementation of the interpolation function
  205. float T_interpolate(byte DS_Temp) {
  206.   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};
  207.   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};
  208.   float T_Range, T_Diff, TempRange, Temperature;
  209.   int i=0;
  210.   while (i<38) {
  211.     if (TempScale[i]> DS_Temp) break;
  212.     i++;
  213.   }
  214.   if (i>0) {
  215.     T_Range = TempScale[i] - TempScale[i-1];
  216.     T_Diff = DS_Temp - TempScale[i-1];
  217.     TempRange = TempValue[i] - TempValue[i-1];
  218.     Temperature = TempValue[i-1] + (T_Diff/T_Range)*TempRange;
  219.   } else {
  220.     Temperature = TempValue[0];
  221.   }
  222.   return Temperature;
  223. }
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement