Advertisement
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 21:24:37
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* send data from bluetooth to tunerpro */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- float T_interpolate(byte DS_Temp); // Prototype for the interpolation function
- 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
- // **** Now, start the serial functions
- Serial.begin(115200); // Open serial monitoring port
- Serial1.begin(8192); // Test the capability of esp 8222 to run at 8192 baud directly
- delay(1500); // delay for diagnostic print
- Serial.println("Ready for data capture");
- // Initialize variables if needed
- int i=0; // Reset the preamble index flag
- }
- void loop() {
- // Forward Bluetooth data from Serial to Serial1 (TunerPro)
- while (Serial.available() > 0) {
- int incomingByte = Serial.read();
- Serial1.write(incomingByte);
- }
- // 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
- Serial.print(" M1 cmd ");
- byte M1Cmd[4] = {0x80, 0x56, 0x01, 0x29}; // Mode 1 command to start 8192 Mode 1 DataStream
- for (int i=0; i<4; i++) {
- Serial1.write(M1Cmd[i]); // Send command bytes
- }
- Serial.println(" Finding Preamble ");
- int i=0; // Preamble index
- unsigned long PreambleTimer = millis(); // Initialize timer
- while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false)) {
- if (Serial1.available() > 0) {
- int ALDLbyte = Serial1.read();
- if (ALDLbyte == Preamble[i]) {
- i++;
- if (i > 2) PreambleFound = true; // Preamble found
- } else {
- PreambleFound = false;
- i=0;
- }
- }
- }
- }
- // Read data stream after preamble
- const int ByteCount = 64;
- byte DataBytes[ByteCount+1]; // 1-based indexing for clarity
- int DataStreamIndex = 1;
- while (DataStreamIndex <= ByteCount) {
- if (Serial1.available() > 0) {
- DataBytes[DataStreamIndex] = Serial1.read();
- DataStreamIndex++;
- }
- }
- // Calculate checksum
- int i=1;
- unsigned long CheckTotal = 0x80 + 0x95 + 0x01; // sum preamble bytes
- while (i <= ByteCount) {
- CheckTotal += DataBytes[i];
- i++;
- }
- byte CheckSum = (byte)(0x200 - CheckTotal); // Two's complement
- // Verify checksum and output data
- if (digitalRead(DecodeDataOutputPin) == LOW) {
- 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[ByteCount], HEX);
- if (CheckSum == DataBytes[ByteCount]) {
- Serial.println(" Checksum GOOD - Decoded Data as follows: (Page 1) ");
- } else {
- Serial.println(" Checksum *** ERROR *** - Decoded Data as follows: (Page 1) ");
- }
- // Decode data
- float RPM = DataBytes[11] * 25; // Engine RPM
- float TPS = DataBytes[10] * 0.019608; // TPS volts
- float MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906; // MAF gm/sec
- int BLCELL = DataBytes[21];
- int BLM = DataBytes[20];
- int INTEGRATOR = DataBytes[22];
- float InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259; // ms
- float O2mv = DataBytes[17] * 4.44; // mV
- float MAT = T_interpolate(DataBytes[30]);
- unsigned int Runtime = (DataBytes[52] * 256) + DataBytes[53];
- // Print decoded 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 to read
- unsigned long StartTime = millis();
- while (millis() < StartTime + 3000) {
- if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // flush buffer
- }
- } else if (digitalRead(HexDataOutputPin) == LOW) {
- // Hex output mode
- 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[ByteCount], HEX);
- if (CheckSum == DataBytes[ByteCount]) {
- Serial.println(" Checksum GOOD - Data as follows: ");
- } else {
- Serial.println("Checksum *** ERROR *** - Data as follows: ");
- }
- // Print hex data
- int j=1;
- int bytecounter=0;
- while (j<=ByteCount) {
- Serial.print(DataBytes[j], HEX);
- Serial.print(" ");
- j++;
- bytecounter++;
- if (bytecounter >= linecount) {
- bytecounter=0;
- Serial.println();
- }
- }
- Serial.println();
- } else {
- // Raw binary data
- Serial.write(0x80);
- Serial.write(0x95);
- Serial.write(0x01);
- for (int j=1; j<=ByteCount; j++) {
- Serial.write(DataBytes[j]);
- }
- }
- }
- }
- // Implementation of the interpolation 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement