pleasedontcode

Vehicle Data rev_06

Jul 11th, 2025
542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 15.09 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:11:46
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to aldldroid */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. // No additional libraries needed for Bluetooth serial communication
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float T_interpolate(byte DS_Temp);
  32.  
  33. /****** Bluetooth Serial Object ******/
  34. HardwareSerial BluetoothSerial(2); // Using Serial2 for Bluetooth communication
  35.  
  36. void setup()
  37. {
  38.   // **** I/O configuration and setup first
  39.   pinMode(ALDLTestPin, INPUT);                                              // define D4 as an input pin to listen for the 160 baud input data
  40.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);                                // User convenience pin.  Grounding this pin will send Decoded Data to the Serial Port
  41.   pinMode(HexDataOutputPin, INPUT_PULLUP);                                   // User convenience pin.  Grounding this pin will send HEX Data to the Serial Port
  42.  
  43.   // Initialize serial communications
  44.   Serial.begin(115200);                                                     // Open serial monitoring port
  45.   Serial1.begin(8192);                                                      // Initialize Serial1 at 8192 baud for ALDL data
  46.   BluetoothSerial.begin(9600);                                                // Initialize Bluetooth serial at 9600 baud
  47.   delay(1500);                                                              // delay for diagnostic print
  48.   Serial.println("Ready for data capture");
  49.   BluetoothSerial.println("Arduino Mega Bluetooth Data Stream Ready");
  50.  
  51.   // **** Initialize the variables, flags, etc
  52.   int i=0;                                                                  // Reset the preamble index flag
  53. }
  54.  
  55. void loop() {
  56.   // Wait for silence period on the ALDL
  57.   Serial.print("wait for silence ");
  58.   bool SilenceFound = false;                                                     // Reset silence flag
  59.   unsigned long StartTime= micros();                                              // First look for an active signal or a timeout - initialize timer
  60.   while ((micros() - StartTime) < 15000) {                                     // Wait for a 15 ms silent period
  61.     if (digitalRead(ALDLTestPin) == 0) {                                       // Any line activity resets the start time
  62.        StartTime= micros();                                                   // Timing starts over
  63.      }
  64.   }                                                            
  65.   SilenceFound = true;                                                      // Set the silence flag on exit
  66.  
  67.   while (SilenceFound == true) {                                              // While silence found flag is set, continuously request and transmit Mode 1 data
  68.     bool PreambleFound = false;                                                 // Reset preamble found flag                                                            
  69.     while (PreambleFound == false) {                                         // First look at data until the preamble has been found will read data forever until a preamble is read
  70.       Serial.print(" M1 cmd ");
  71.       int i=0;                                                                  // use bytecounter to send the outgoing Mode1CMD sequence
  72.       byte M1Cmd[4] = {0x80,0x56,0x01,0x29};                                    // Mode 1 command to start 8192 Mode 1 DataStream (80 56 01 29 HEX)
  73.       while (i<4) {
  74.         Serial1.write(M1Cmd[i]);                                            // sends 1 byte of the command sequence
  75.         i++;
  76.       }  
  77.       Serial.println(" Finding Preamble  ");  
  78.       i=0;                                                                  // Then reset byte counter and scan incoming data for the preamble
  79.       unsigned long PreambleTimer = millis();                                             // Initialize timer to detect timeout
  80.       while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false)) {                    // First look at data for 100 ms or until the preamble has been found will read data forever until a preamble is read
  81.         if (Serial1.available() > 0) {                                      // Check for available data on the serial port
  82.           int ALDLbyte = Serial1.read();                                      // Read it and look for the preamble
  83.           if (ALDLbyte == Preamble[i]) {                                   // Look for matching byte of data preamble in serial stream
  84.             i++;                                                        // Increment the preamble index and look for the next character
  85.             if (i>2) PreambleFound = true;                               // Once three characters are found, the preamble has been found, time to read in the data
  86.           }        
  87.           else {                                                            // If there isn't match, start over looking from the beginning
  88.             PreambleFound = false;                                      // Reset preamble found flag
  89.             i=0;                                                        // Reset the preamble index flag          
  90.           }                                                             // End of Preamble check
  91.         }                                                               // End of Serial Available & read
  92.       }                                                                  // End of the preamble finding routine either preamble found or timeout          
  93.     }
  94.     // Read complete data packet
  95.     int DataStreamIndex = 1;                                                  // Once a valid preamble has been found set the data stream index to the first byte
  96.     byte DataBytes[64];                                                // Array to hold the serial data stream
  97.     while (DataStreamIndex < 65) {                                          // and read data up to byte #63 + 1 byte Checksum = 64 Bytes total
  98.       if (Serial1.available() > 0) {                                    // Check for available data on the serial port
  99.         DataBytes[DataStreamIndex] = Serial1.read();                // And read bytes into the array as they come
  100.         DataStreamIndex++;                                          // update the index
  101.       }                                                             // end of read if
  102.     }                                                                   // End of datastream read while
  103.  
  104.     // Checksum calculation
  105.     int i=1;                                                                  // use bytecounter as an index
  106.     unsigned int CheckTotal = 0x80+0x95+0x01;                                          // Sum of preamble bytes
  107.     while (i< (64)) {                                               // Add received bytes to the Checksum
  108.       CheckTotal = CheckTotal + DataBytes[i];                           // add a byte
  109.       i++;
  110.     }  
  111.     byte CheckSum = 0x200 - CheckTotal;                                        // Two's complement the checksum
  112.     // Verify checksum
  113.     if (digitalRead(DecodeDataOutputPin) == LOW) {                          // Check decoded output bit
  114.       Serial.print("New Data Stream received at ");
  115.       Serial.print(millis());
  116.       Serial.print(" Calc CHECKSUM: ");
  117.       Serial.print(CheckSum, HEX);
  118.       Serial.print(" Transmitted CHECKSUM: ");
  119.       Serial.print(DataBytes[63], HEX);                          // Last byte (64th) transmitted is ECM's checksum
  120.       if (CheckSum == DataBytes[63]) {                             // Checksums match
  121.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  122.         // Send data over Bluetooth
  123.         BluetoothSerial.print("RPM:");
  124.         float RPM = DataBytes[11] * 25;                                    // Engine RPM
  125.         BluetoothSerial.print(RPM);
  126.         BluetoothSerial.print(",TPS:");
  127.         float TPS = DataBytes[10] * 0.019608;                               // TPS volts
  128.         BluetoothSerial.print(TPS);
  129.         BluetoothSerial.print(",MAF:");
  130.         float MAF = ((DataBytes[36] * 256) + (DataBytes[37])) * 0.003906;   // Mass Air Flow
  131.         BluetoothSerial.print(MAF);
  132.         BluetoothSerial.print(",BLCELL:");
  133.         int BLCELL = DataBytes[21];
  134.         BluetoothSerial.print(BLCELL);
  135.         BluetoothSerial.print(",BLM:");
  136.         int BLM = DataBytes[20];
  137.         BluetoothSerial.print(BLM);
  138.         BluetoothSerial.print(",INTEGRATOR:");
  139.         int INTEGRATOR = DataBytes[22];
  140.         BluetoothSerial.print(INTEGRATOR);
  141.         BluetoothSerial.print(",InjPW:");
  142.         float InjPW = ((DataBytes[45] * 256) + (DataBytes[46])) * 0.015259;
  143.         BluetoothSerial.print(InjPW);
  144.         BluetoothSerial.print(",O2mv:");
  145.         float O2mv = DataBytes[17] * 4.44;
  146.         BluetoothSerial.print(O2mv);
  147.         BluetoothSerial.print(",MAT:");
  148.         float MAT = T_interpolate(DataBytes[30]);
  149.         BluetoothSerial.print(MAT);
  150.         BluetoothSerial.print(",Runtime:");
  151.         unsigned int Runtime = (DataBytes[52] * 256 + DataBytes[53]);
  152.         BluetoothSerial.println(Runtime);
  153.       }
  154.       else {
  155.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");                  
  156.       }
  157.     }
  158.     else if (digitalRead(HexDataOutputPin) == LOW) {                        // Check hex output flag
  159.       Serial.print("New Data Stream received at ");
  160.       Serial.print(millis());
  161.       Serial.print(" Calc CHECKSUM: ");
  162.       Serial.print(CheckSum, HEX);
  163.       Serial.print(" Transmitted CHECKSUM: ");
  164.       Serial.print(DataBytes[63], HEX);                          //Last byte (64'th) transmitted is ECM's checksum
  165.       if (CheckSum == DataBytes[63]) {                             // Verify Checksums match
  166.         Serial.println(" Checksum GOOD - Data as follows: ");      
  167.       }
  168.       else Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  169.       int j=1; // Local Byte Count for message output  
  170.       int bytecounter = 0;                                                  // Used to create 32 byte lines of data
  171.       const int linecount = 64;                                             // 32 bytes per line
  172.       while (j<64 +1) {                                            // Printing loop from byte 1 to bytecount
  173.         Serial.print(DataBytes[j], HEX);                              // print the aldl data byte as hex
  174.         // Send over Bluetooth as well
  175.         BluetoothSerial.print(DataBytes[j], HEX);
  176.         j++;                                                          // Move on to the next byte
  177.         bytecounter++;                                                // Increment byte counter
  178.         if (bytecounter >= linecount) {                                 // check if time for new line
  179.           bytecounter = 0;                                          // Reset byte count for next line
  180.           Serial.println("");                                       // add a new line
  181.           BluetoothSerial.println("");
  182.         }
  183.         else {
  184.           Serial.print(" ");                                        // add a space
  185.           BluetoothSerial.print(" ");
  186.         }
  187.       }
  188.       Serial.println("");                                               // New Line At end of transmit
  189.       BluetoothSerial.println("");
  190.     } // End of Hex Output Loop
  191.     else {                                                                  // Default is to send the raw binary data stream to the Serial Port
  192.       // Send Raw Bytewise Data Stream to Serial Port
  193.       Serial.write(0x80);                                                // write the preamble bytes (80 95 01 HEX)
  194.       Serial.write(0x95);                                                    
  195.       Serial.write(0x01);                                                    
  196.       for (int j=1; j<65; j++) {                                // Printing loop from data byte 1 to bytecount, including checksum
  197.         Serial.write(DataBytes[j]);                                   // write the aldl data byte as a binary byte
  198.         // Send over Bluetooth as well
  199.         BluetoothSerial.write(DataBytes[j]);
  200.       }
  201.     } // End of Raw Data Stream transmission
  202.   } // Loop back for next Mode 1 Dump
  203. }
  204.  
  205. // Implementation of T_interpolate function
  206. float T_interpolate(byte DS_Temp) {                                         // Subroutine to interpolate MAT temperature from data stream
  207.   // Temperature scale for interpolating air temperature taken from the ADS file. There are 38 values in the table ranging from -40C for a value of 1 to 200 C for a value of 256
  208.   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};  // Data Values (38)
  209.   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}; // Temp Values (38)
  210.   float T_Range;                                                          // Interpolation range
  211.   float T_Diff;                                                           // Difference between input value and the bottom of the interpolation range
  212.   float TempRange;                                                        // Difference between points on the output value scale
  213.   float Temperature;                                                      // Interpolated Temperature value
  214.   int i=0;
  215.   while (i<38) {                                                            // Loop through increasing values to find start of the interpolation
  216.     if  (TempScale[i]> DS_Temp) break;                                  // Until a higher value is found - exit with i pointing to the higher value for interpolation
  217.     i++;
  218.   }
  219.   if (i>0) {                                                                // Figure out the linear interpolation range and difference along the scale
  220.     T_Range = TempScale[i] - TempScale[i-1];                            // Range between these points along the input scale (all calculated as a floating point)
  221.     T_Diff =  DS_Temp - TempScale[i-1];                                 // Difference between data and the lower point
  222.     TempRange = TempValue[i] - TempValue[i-1];                          // Difference between points along the output value scale  
  223.     Temperature =  TempValue[i-1] + (T_Diff/T_Range)*TempRange;         // Interpolated Temperature
  224.   }
  225.   else Temperature = TempValue[0];                                        // unless the input data is <= to the bottom of the scale and the minimum value is used
  226.  
  227.   return Temperature;                                                     // Return the interpolated temperature
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment