Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Vehicle Data
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-07-11 21:11:46
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* send data from bluetooth to aldldroid */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // No additional libraries needed for Bluetooth serial communication
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- float T_interpolate(byte DS_Temp);
- /****** Bluetooth Serial Object ******/
- HardwareSerial BluetoothSerial(2); // Using Serial2 for Bluetooth communication
- void setup()
- {
- // **** I/O configuration and setup first
- pinMode(ALDLTestPin, INPUT); // define D4 as an input pin to listen for the 160 baud input data
- pinMode(DecodeDataOutputPin, INPUT_PULLUP); // User convenience pin. Grounding this pin will send Decoded Data to the Serial Port
- pinMode(HexDataOutputPin, INPUT_PULLUP); // User convenience pin. Grounding this pin will send HEX Data to the Serial Port
- // Initialize serial communications
- Serial.begin(115200); // Open serial monitoring port
- Serial1.begin(8192); // Initialize Serial1 at 8192 baud for ALDL data
- BluetoothSerial.begin(9600); // Initialize Bluetooth serial at 9600 baud
- delay(1500); // delay for diagnostic print
- Serial.println("Ready for data capture");
- BluetoothSerial.println("Arduino Mega Bluetooth Data Stream Ready");
- // **** Initialize the variables, flags, etc
- int i=0; // Reset the preamble index flag
- }
- void loop() {
- // Wait for silence period on the ALDL
- Serial.print("wait for silence ");
- bool SilenceFound = false; // Reset silence flag
- unsigned long StartTime= micros(); // First look for an active signal or a timeout - initialize timer
- while ((micros() - StartTime) < 15000) { // Wait for a 15 ms silent period
- if (digitalRead(ALDLTestPin) == 0) { // Any line activity resets the start time
- StartTime= micros(); // Timing starts over
- }
- }
- SilenceFound = true; // Set the silence flag on exit
- while (SilenceFound == true) { // While silence found flag is set, continuously request and transmit Mode 1 data
- bool PreambleFound = false; // Reset preamble found flag
- while (PreambleFound == false) { // First look at data until the preamble has been found will read data forever until a preamble is read
- Serial.print(" M1 cmd ");
- int i=0; // use bytecounter to send the outgoing Mode1CMD sequence
- byte M1Cmd[4] = {0x80,0x56,0x01,0x29}; // Mode 1 command to start 8192 Mode 1 DataStream (80 56 01 29 HEX)
- while (i<4) {
- Serial1.write(M1Cmd[i]); // sends 1 byte of the command sequence
- i++;
- }
- Serial.println(" Finding Preamble ");
- i=0; // Then reset byte counter and scan incoming data for the preamble
- unsigned long PreambleTimer = millis(); // Initialize timer to detect timeout
- 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
- if (Serial1.available() > 0) { // Check for available data on the serial port
- int ALDLbyte = Serial1.read(); // Read it and look for the preamble
- if (ALDLbyte == Preamble[i]) { // Look for matching byte of data preamble in serial stream
- i++; // Increment the preamble index and look for the next character
- if (i>2) PreambleFound = true; // Once three characters are found, the preamble has been found, time to read in the data
- }
- else { // If there isn't match, start over looking from the beginning
- PreambleFound = false; // Reset preamble found flag
- i=0; // Reset the preamble index flag
- } // End of Preamble check
- } // End of Serial Available & read
- } // End of the preamble finding routine either preamble found or timeout
- }
- // Read complete data packet
- int DataStreamIndex = 1; // Once a valid preamble has been found set the data stream index to the first byte
- byte DataBytes[64]; // Array to hold the serial data stream
- while (DataStreamIndex < 65) { // and read data up to byte #63 + 1 byte Checksum = 64 Bytes total
- if (Serial1.available() > 0) { // Check for available data on the serial port
- DataBytes[DataStreamIndex] = Serial1.read(); // And read bytes into the array as they come
- DataStreamIndex++; // update the index
- } // end of read if
- } // End of datastream read while
- // Checksum calculation
- int i=1; // use bytecounter as an index
- unsigned int CheckTotal = 0x80+0x95+0x01; // Sum of preamble bytes
- while (i< (64)) { // Add received bytes to the Checksum
- CheckTotal = CheckTotal + DataBytes[i]; // add a byte
- i++;
- }
- byte CheckSum = 0x200 - CheckTotal; // Two's complement the checksum
- // Verify checksum
- if (digitalRead(DecodeDataOutputPin) == LOW) { // Check decoded output bit
- Serial.print("New Data Stream received at ");
- Serial.print(millis());
- Serial.print(" Calc CHECKSUM: ");
- Serial.print(CheckSum, HEX);
- Serial.print(" Transmitted CHECKSUM: ");
- Serial.print(DataBytes[63], HEX); // Last byte (64th) transmitted is ECM's checksum
- if (CheckSum == DataBytes[63]) { // Checksums match
- Serial.println(" Checksum GOOD - Decoded Data as follows: (Page 1) ");
- // Send data over Bluetooth
- BluetoothSerial.print("RPM:");
- float RPM = DataBytes[11] * 25; // Engine RPM
- BluetoothSerial.print(RPM);
- BluetoothSerial.print(",TPS:");
- float TPS = DataBytes[10] * 0.019608; // TPS volts
- BluetoothSerial.print(TPS);
- BluetoothSerial.print(",MAF:");
- float MAF = ((DataBytes[36] * 256) + (DataBytes[37])) * 0.003906; // Mass Air Flow
- BluetoothSerial.print(MAF);
- BluetoothSerial.print(",BLCELL:");
- int BLCELL = DataBytes[21];
- BluetoothSerial.print(BLCELL);
- BluetoothSerial.print(",BLM:");
- int BLM = DataBytes[20];
- BluetoothSerial.print(BLM);
- BluetoothSerial.print(",INTEGRATOR:");
- int INTEGRATOR = DataBytes[22];
- BluetoothSerial.print(INTEGRATOR);
- BluetoothSerial.print(",InjPW:");
- float InjPW = ((DataBytes[45] * 256) + (DataBytes[46])) * 0.015259;
- BluetoothSerial.print(InjPW);
- BluetoothSerial.print(",O2mv:");
- float O2mv = DataBytes[17] * 4.44;
- BluetoothSerial.print(O2mv);
- BluetoothSerial.print(",MAT:");
- float MAT = T_interpolate(DataBytes[30]);
- BluetoothSerial.print(MAT);
- BluetoothSerial.print(",Runtime:");
- unsigned int Runtime = (DataBytes[52] * 256 + DataBytes[53]);
- BluetoothSerial.println(Runtime);
- }
- else {
- Serial.println(" Checksum *** ERROR *** - Decoded Data as follows: (Page 1) ");
- }
- }
- else if (digitalRead(HexDataOutputPin) == LOW) { // Check hex output flag
- Serial.print("New Data Stream received at ");
- Serial.print(millis());
- Serial.print(" Calc CHECKSUM: ");
- Serial.print(CheckSum, HEX);
- Serial.print(" Transmitted CHECKSUM: ");
- Serial.print(DataBytes[63], HEX); //Last byte (64'th) transmitted is ECM's checksum
- if (CheckSum == DataBytes[63]) { // Verify Checksums match
- Serial.println(" Checksum GOOD - Data as follows: ");
- }
- else Serial.println("Checksum *** ERROR *** - Data as follows: ");
- int j=1; // Local Byte Count for message output
- int bytecounter = 0; // Used to create 32 byte lines of data
- const int linecount = 64; // 32 bytes per line
- while (j<64 +1) { // Printing loop from byte 1 to bytecount
- Serial.print(DataBytes[j], HEX); // print the aldl data byte as hex
- // Send over Bluetooth as well
- BluetoothSerial.print(DataBytes[j], HEX);
- j++; // Move on to the next byte
- bytecounter++; // Increment byte counter
- if (bytecounter >= linecount) { // check if time for new line
- bytecounter = 0; // Reset byte count for next line
- Serial.println(""); // add a new line
- BluetoothSerial.println("");
- }
- else {
- Serial.print(" "); // add a space
- BluetoothSerial.print(" ");
- }
- }
- Serial.println(""); // New Line At end of transmit
- BluetoothSerial.println("");
- } // End of Hex Output Loop
- else { // Default is to send the raw binary data stream to the Serial Port
- // Send Raw Bytewise Data Stream to Serial Port
- Serial.write(0x80); // write the preamble bytes (80 95 01 HEX)
- Serial.write(0x95);
- Serial.write(0x01);
- for (int j=1; j<65; j++) { // Printing loop from data byte 1 to bytecount, including checksum
- Serial.write(DataBytes[j]); // write the aldl data byte as a binary byte
- // Send over Bluetooth as well
- BluetoothSerial.write(DataBytes[j]);
- }
- } // End of Raw Data Stream transmission
- } // Loop back for next Mode 1 Dump
- }
- // Implementation of T_interpolate function
- float T_interpolate(byte DS_Temp) { // Subroutine to interpolate MAT temperature from data stream
- // 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
- 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)
- 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)
- float T_Range; // Interpolation range
- float T_Diff; // Difference between input value and the bottom of the interpolation range
- float TempRange; // Difference between points on the output value scale
- float Temperature; // Interpolated Temperature value
- int i=0;
- while (i<38) { // Loop through increasing values to find start of the interpolation
- if (TempScale[i]> DS_Temp) break; // Until a higher value is found - exit with i pointing to the higher value for interpolation
- i++;
- }
- if (i>0) { // Figure out the linear interpolation range and difference along the scale
- T_Range = TempScale[i] - TempScale[i-1]; // Range between these points along the input scale (all calculated as a floating point)
- T_Diff = DS_Temp - TempScale[i-1]; // Difference between data and the lower point
- TempRange = TempValue[i] - TempValue[i-1]; // Difference between points along the output value scale
- Temperature = TempValue[i-1] + (T_Diff/T_Range)*TempRange; // Interpolated Temperature
- }
- else Temperature = TempValue[0]; // unless the input data is <= to the bottom of the scale and the minimum value is used
- return Temperature; // Return the interpolated temperature
- }
Advertisement
Add Comment
Please, Sign In to add comment