pleasedontcode

Vehicle Data rev_05

Jul 11th, 2025
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 12.63 KB | None | 0 0
  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 21:06:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to android pc */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. // No additional libraries needed for basic serial communication
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float T_interpolate(byte DS_Temp);
  32.  
  33. void setup()
  34. {
  35.   // **** I/O configuration and setup first
  36.   pinMode(ALDLTestPin, INPUT);                                              // define ALDLTestPin as an input pin to listen for the 160 baud input data
  37.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);                                // User convenience pin.  Grounding this pin will send Decoded Data to the Serial Port
  38.   pinMode(HexDataOutputPin, INPUT_PULLUP);                                   // User convenience pin.  Grounding this pin will send HEX Data to the Serial Port
  39.  
  40.   // **** Now, start the serial functions
  41.   Serial.begin(115200);                                                     // Open serial monitoring port
  42.   Serial1.begin(8192);                                                      // Initialize Serial1 at 8192 baud for ALDL data
  43.   Serial2.begin(9600);                                                      // Initialize Serial2 at 9600 baud for Bluetooth communication
  44.   delay(1500);                                                              // delay for diagnostic print
  45.   Serial.println("Ready for data capture");
  46. }
  47.  
  48. void loop() {
  49.   // Wait for silence period on the ALDL
  50.   Serial.print("wait for silence ");
  51.   bool SilenceFound = false;                                                    // Reset silence flag
  52.   unsigned long StartTime= micros();                                              // First look for an active signal or a timeout - initialize timer
  53.   while ((micros() - StartTime) < 15000) {                                       // Wait for a 15 ms silent period
  54.     if (digitalRead(ALDLTestPin) == 0) {                                         // Any line activity resets the start time
  55.        StartTime= micros();                                                     // Timing starts over
  56.     }
  57.   }                                                                              // Exit after 15 ms of silence
  58.   SilenceFound = true;                                                          // Set the silence flag on exit
  59.  
  60.   while (SilenceFound == true) {                                                 // While silence found flag is set, continuously request and transmit Mode 1 data
  61.     bool PreambleFound = false;                                                  // Reset preamble found flag                                                            
  62.     while (PreambleFound == false) {                                              // First look at data until the preamble has been found will read data forever until a preamble is read
  63.       Serial.print(" M1 cmd ");
  64.       int i=0;                                                                      // use bytecounter to send the outgoing Mode1CMD sequence
  65.       while (i<4) {
  66.         Serial1.write(M1Cmd[i]);                                                    // sends 1 byte of the command sequence
  67.         i++;
  68.       }  
  69.       Serial.println(" Finding Preamble  ");  
  70.       i=0;                                                                      // Then reset byte counter and scan incoming data for the preamble
  71.       unsigned long PreambleTimer = millis();                                   // Initialize timer to detect timeout
  72.       while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false)) {  // First look at data for 100 ms or until the preamble has been found
  73.         if (Serial1.available() > 0) {                                            // Check for available data on the serial port
  74.           int ALDLbyte = Serial1.read();                                            // Read it and look for the preamble
  75.           if (ALDLbyte == Preamble[i]) {                                              // Look for matching byte of data preamble in serial stream
  76.             i++;                                                                      // Increment the preamble index and look for the next character
  77.             if (i > 2) PreambleFound = true;                                           // Once three characters are found, the preamble has been found, time to read in the data
  78.           } else {                                                                    // If there isn't match, start over looking from the beginning
  79.             PreambleFound = false;                                                    // Reset preamble found flag
  80.             i=0;                                                                      // Reset the preamble index flag          
  81.           } // End of Preamble check
  82.         } // End of Serial Available & read
  83.       } // End of the preamble finding routine either preamble found or timeout          
  84.       // Read a complete data stream packet
  85.       int DataStreamIndex = 1;                                                      // Once a valid preamble has been found set the data stream index to the first byte
  86.       byte DataBytes[65];                                                           // Array to hold the serial data stream
  87.       while (DataStreamIndex < 65) {                                                 // Read 64 bytes (including checksum)
  88.         if (Serial1.available() > 0) {                                                // Check for available data on the serial port
  89.           DataBytes[DataStreamIndex] = Serial1.read();                                // Read byte into array
  90.           DataStreamIndex++;                                                          // update the index
  91.         }
  92.       }
  93.       // Checksum calculation
  94.       int i=1;                                                                      // use bytecounter as an index
  95.       unsigned int CheckTotal = 0x80 + 0x95 + 0x01;                                   // sum of preamble bytes
  96.       for (i=1; i<65; i++) {                                                          // Add received bytes to checksum
  97.         CheckTotal += DataBytes[i];                                                   // add a byte
  98.       }
  99.       byte CheckSum = (byte)(0x200 - CheckTotal);                                       // Two's complement
  100.       // Verify checksum and process data
  101.       if (digitalRead(DecodeDataOutputPin) == LOW) {                                // Check decoded output bit
  102.         Serial.print("New Data Stream received at ");
  103.         Serial.print(millis());
  104.         Serial.print(" Calc CHECKSUM: ");
  105.         Serial.print(CheckSum, HEX);
  106.         Serial.print(" Transmitted CHECKSUM: ");
  107.         Serial.print(DataBytes[64], HEX);                                             // Last byte is ECM checksum
  108.         if (CheckSum == DataBytes[64]) {                                              // Checksum match
  109.           Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  110.         } else {
  111.           Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");                  
  112.         }
  113.         // Extract and display data
  114.         float RPM = DataBytes[12] * 25;                                               // Engine RPM
  115.         float TPS = DataBytes[11] * 0.019608;                                           // TPS volts
  116.         float MAF = ((DataBytes[37] * 256) + DataBytes[36]) * 0.003906;                // Mass Air Flow gm/sec
  117.         int BLCELL = DataBytes[22];                                                    // Block Learn Cell #
  118.         int BLM = DataBytes[21];                                                        // BLM value
  119.         int INTEGRATOR = DataBytes[23];                                                 // Fuel trim
  120.         float InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259;               // Injector pulse width
  121.         float O2mv = DataBytes[16] * 4.44;                                              // O2 sensor MV
  122.         float MAT = T_interpolate(DataBytes[29]);                                       // Intake Temp
  123.         unsigned int Runtime = (DataBytes[52] * 256) + DataBytes[53];                 // Engine runtime
  124.         // Send data over Bluetooth
  125.         String dataString = "RPM:" + String(RPM) +
  126.                             " TPS:" + String(TPS) +
  127.                             " MAF:" + String(MAF) +
  128.                             " BLCELL:" + String(BLCELL) +
  129.                             " BLM:" + String(BLM) +
  130.                             " INTEGRATOR:" + String(INTEGRATOR) +
  131.                             " InjPW:" + String(InjPW) +
  132.                             " O2mv:" + String(O2mv) +
  133.                             " MAT:" + String(MAT) +
  134.                             " Runtime:" + String(Runtime);
  135.         Serial2.println(dataString);
  136.         // Display data on serial monitor
  137.         Serial.print("Engine Speed     : ");
  138.         Serial.print(RPM);
  139.         Serial.println(" RPM");
  140.         Serial.print("Throttle Position: ");
  141.         Serial.print(TPS);
  142.         Serial.println(" Volts");
  143.         Serial.print("Mass Air Flow    : ");
  144.         Serial.print(MAF);
  145.         Serial.println(" Grams/Sec");
  146.         Serial.print("Current BLM Cell: ");
  147.         Serial.print(BLCELL);
  148.         Serial.print(" BLM Value: ");
  149.         Serial.print(BLM);
  150.         Serial.print("  Current Fuel Integrator: ");
  151.         Serial.println(INTEGRATOR);
  152.         Serial.print("Injector Pulse   : ");
  153.         Serial.print(InjPW);
  154.         Serial.println(" Milliseconds");
  155.         Serial.print("O2 Sensor Voltage: ");
  156.         Serial.print(O2mv);
  157.         Serial.println(" Millivolts");
  158.         Serial.print("Intake Air Temp  : ");
  159.         Serial.print(MAT);
  160.         Serial.println(" Deg C");
  161.         Serial.print("Engine Run Time  : ");
  162.         Serial.print(Runtime);
  163.         Serial.println(" Seconds");
  164.         // Delay for readability
  165.         unsigned long StartTime = millis();
  166.         while (millis() < StartTime + 3000) {
  167.           if (Serial1.available() > 0) Serial1.read(); // flush buffer
  168.         }
  169.       } else if (digitalRead(HexDataOutputPin) == LOW) {                         // Hex output mode
  170.         Serial.print("New Data Stream received at ");
  171.         Serial.print(millis());
  172.         Serial.print(" Calc CHECKSUM: ");
  173.         Serial.print(CheckSum, HEX);
  174.         Serial.print(" Transmitted CHECKSUM: ");
  175.         Serial.print(DataBytes[64], HEX);
  176.         if (CheckSum == DataBytes[64]) {
  177.           Serial.println(" Checksum GOOD - Data as follows: ");
  178.         } else {
  179.           Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  180.         }
  181.         // Hex dump of data bytes
  182.         int j=1; // Local Byte Count for message output
  183.         int bytecounter = 0; // For line formatting
  184.         const int linecount = 64; // 32 bytes per line
  185.         while (j <= 64) {
  186.           Serial.print(DataBytes[j], HEX);
  187.           j++;
  188.           bytecounter++;
  189.           if (bytecounter >= linecount) {
  190.             bytecounter=0;
  191.             Serial.println();
  192.           } else {
  193.             Serial.print(" ");
  194.           }
  195.         }
  196.         Serial.println();
  197.       } else { // Default raw binary output
  198.         Serial.write(0x80);
  199.         Serial.write(0x95);
  200.         Serial.write(0x01);
  201.         for (int j=1; j<=64; j++) {
  202.           Serial.write(DataBytes[j]);
  203.         }
  204.       }
  205.     }
  206.   }
  207. }
  208.  
  209. float T_interpolate(byte DS_Temp) {
  210.   // Interpolation table for temperature
  211.   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};
  212.   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};
  213.   float T_Range, T_Diff, TempRange, Temperature;
  214.   int i=0;
  215.   while (i<38) {
  216.     if (TempScale[i] > DS_Temp) break;
  217.     i++;
  218.   }
  219.   if (i>0) {
  220.     T_Range = TempScale[i] - TempScale[i-1];
  221.     T_Diff = DS_Temp - TempScale[i-1];
  222.     TempRange = TempValue[i] - TempValue[i-1];
  223.     Temperature = TempValue[i-1] + (T_Diff/T_Range)*TempRange;
  224.   } else {
  225.     Temperature = TempValue[0];
  226.   }
  227.   return Temperature;
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment