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 Diagnostics
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-07-11 22:03:20
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* send data from bluetooth to android */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- float T_interpolate(byte DS_Temp); // Added prototype for T_interpolate function
- /***** DEFINITION OF Software Serial *****/
- const uint8_t HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
- const uint8_t HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
- SoftwareSerial HC05_Bluetooth_Module_HC05_mySerial(HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_RX_A1, HC05_Bluetooth_Module_HC05_mySerial_PIN_SERIAL_TX_A0);
- void setup(void)
- {
- // Initialize Bluetooth serial communication
- HC05_Bluetooth_Module_HC05_mySerial.begin(9600);
- }
- void loop(void)
- {
- // *** USER CODE START ***
- // Variables for user code
- const byte ALDLTestPin = 4; // Input used to listen for 160 BAUD ALDL activity before commanding 8192 BAUD
- const byte DecodeDataOutputPin = 5; // Input pin for decoded data output control
- const byte HexDataOutputPin = 6; // Input pin for hex data output control
- int ALDLbyte = 0; // One byte of ALDL data
- int bytecounter = 0; // count bytes for line spacing in output
- const int linecount = 64; // 32 bytes per line
- byte M1Cmd[4] = {0x80, 0x56, 0x01, 0x29}; // Mode 1 command to start 8192 Mode 1 DataStream (80 56 01 29 HEX)
- byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble from ECM indicates start of 8192 Mode 1 DataStream (80 95 01 HEX)
- bool PreambleFound = false; // Reset preamble found flag
- bool SilenceFound = false; // Flag to indicate ECM silence in 160 baud mode
- bool CommInit8192 = false; // Flag for 8192 baud communication
- const double SilenceWait = 15000; // Minimum silence period before transmitting Mode 1 command (ms)
- unsigned long PreambleTimer; // Timeout timer for preamble
- const int ByteCount = 64; // Number of data bytes following preamble
- byte DataBytes[ByteCount]; // Array to hold serial data stream
- int DataStreamIndex = 1; // Data stream byte index
- int i = 0; // Preamble index counter
- unsigned long StartTime; // Timer for measuring incoming data
- unsigned int CheckTotal; // Total for checksum calculation
- byte CheckSum; // Calculated checksum
- // Variables for decoded data
- float RPM = 0;
- float TPS = 0;
- float MAF = 0;
- float InjPW = 0;
- float O2mv = 0;
- int BLCELL = 0;
- int BLM = 0;
- int INTEGRATOR = 0;
- float MAT = 0;
- unsigned int Runtime = 0;
- // *** Wait for silence on ALDL line
- Serial.print("wait for silence ");
- SilenceFound = false;
- StartTime = micros();
- while ((micros() - StartTime) < 15000) // wait 15ms for silence
- {
- if (digitalRead(ALDLTestPin) == 0)
- {
- StartTime = micros(); // reset timer if activity detected
- }
- }
- SilenceFound = true;
- while (SilenceFound)
- {
- // Send Mode 1 command over Bluetooth serial
- PreambleFound = false;
- while (!PreambleFound)
- {
- Serial.print(" M1 cmd ");
- i = 0;
- while (i < 4)
- {
- Serial1.write(M1Cmd[i]);
- i++;
- }
- Serial.println(" Finding Preamble ");
- i = 0;
- PreambleTimer = millis();
- while ((millis() - PreambleTimer) < 100 && !PreambleFound)
- {
- if (Serial1.available() > 0)
- {
- ALDLbyte = Serial1.read();
- if (ALDLbyte == Preamble[i])
- {
- i++;
- if (i > 2) PreambleFound = true;
- }
- else
- {
- PreambleFound = false;
- i = 0;
- }
- }
- }
- }
- // Read data stream after preamble
- DataStreamIndex = 1;
- while (DataStreamIndex < 65)
- {
- if (Serial1.available() > 0)
- {
- DataBytes[DataStreamIndex] = Serial1.read();
- DataStreamIndex++;
- }
- }
- // Calculate checksum
- i = 1;
- CheckTotal = 0x80 + 0x95 + 0x01; // sum preamble bytes
- while (i < ByteCount)
- {
- CheckTotal += DataBytes[i];
- i++;
- }
- CheckSum = 0x200 - CheckTotal; // two's complement
- // Send data to serial port based on control pins
- if (digitalRead(DecodeDataOutputPin) == LOW)
- {
- // Output decoded data
- 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
- if (CheckSum == DataBytes[63])
- Serial.println(" Checksum GOOD - Decoded Data as follows: (Page 1) ");
- else
- Serial.println(" Checksum *** ERROR *** - Decoded Data as follows: (Page 1) ");
- // Parse data bytes
- RPM = DataBytes[11] * 25; // RPM
- TPS = DataBytes[10] * 0.019608; // TPS volts
- MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906; // MAF gm/sec
- BLCELL = DataBytes[21];
- BLM = DataBytes[20];
- INTEGRATOR = DataBytes[22];
- InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259; // ms
- O2mv = DataBytes[17] * 4.44; // mV
- MAT = T_interpolate(DataBytes[30]);
- Runtime = (DataBytes[52] * 256) + DataBytes[53];
- // Display data
- Serial.print("Engine Speed : ");
- Serial.print(RPM);
- Serial.println(" RPM");
- Serial.print("Throttle Position: ");
- Serial.print(TPS);
- Serial.println(" Volts");
- Serial.print("Mass Air Flow : ");
- Serial.print(MAF);
- Serial.println(" Grams/Sec");
- Serial.print("Current BLM Cell: ");
- Serial.print(BLCELL);
- Serial.print(" BLM Value: ");
- Serial.print(BLM);
- Serial.print(" Current Fuel Integrator: ");
- Serial.println(INTEGRATOR);
- Serial.print("Injector Pulse : ");
- Serial.print(InjPW);
- Serial.println(" Milliseconds");
- Serial.print("O2 Sensor Voltage: ");
- Serial.print(O2mv);
- Serial.println(" Millivolts");
- Serial.print("Intake Air Temp : ");
- Serial.print(MAT);
- Serial.println(" Deg C");
- Serial.print("Engine Run Time : ");
- Serial.print(Runtime);
- Serial.println(" Seconds");
- // Delay for user reading
- unsigned long StartTimeDelay = millis();
- while (millis() < StartTimeDelay + 3000)
- {
- if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // flush buffer
- }
- }
- else if (digitalRead(HexDataOutputPin) == LOW)
- {
- // Output hex data
- 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);
- if (CheckSum == DataBytes[63])
- Serial.println(" Checksum GOOD - Data as follows: ");
- else
- Serial.println(" Checksum *** ERROR *** - Data as follows: ");
- int j = 1;
- bytecounter = 0;
- while (j < ByteCount + 1)
- {
- Serial.print(DataBytes[j], HEX);
- j++;
- bytecounter++;
- if (bytecounter >= linecount)
- {
- bytecounter = 0;
- Serial.println();
- }
- else
- {
- Serial.print(" ");
- }
- }
- Serial.println();
- }
- else
- {
- // Send raw data stream over Bluetooth serial
- Serial.write(0x80);
- Serial.write(0x95);
- Serial.write(0x01);
- for (int j = 1; j < ByteCount + 1; j++)
- {
- Serial.write(DataBytes[j]);
- }
- }
- }
- }
- // Implementation of T_interpolate function
- float T_interpolate(byte DS_Temp)
- {
- 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};
- 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};
- float T_Range, T_Diff, TempRange, Temperature;
- int i = 0;
- while (i < 38)
- {
- if (TempScale[i] > DS_Temp) break;
- i++;
- }
- if (i > 0)
- {
- T_Range = TempScale[i] - TempScale[i - 1];
- T_Diff = DS_Temp - TempScale[i - 1];
- TempRange = TempValue[i] - TempValue[i - 1];
- Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
- }
- else
- {
- Temperature = TempValue[0];
- }
- return Temperature;
- }
- // *** USER CODE END ***
- }
Advertisement
Add Comment
Please, Sign In to add comment