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 22:25:37
- ********* 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 the interpolation function
- /***** DEFINITION OF Software Serial *****/
- const uint8_t HC05_Bluetooth_TX_PIN = A0; // Bluetooth TX pin
- const uint8_t HC05_Bluetooth_RX_PIN = A1; // Bluetooth RX pin
- SoftwareSerial HC05(HC05_Bluetooth_TX_PIN, HC05_Bluetooth_RX_PIN);
- // Define other variables used in the code
- // Assuming these are declared globally as in the original code
- // For example:
- int ALDLTestPin = 2; // Example pin, replace with actual
- int DecodeDataOutputPin = 3; // Example pin, replace with actual
- int HexDataOutputPin = 4; // Example pin, replace with actual
- byte M1Cmd[4] = {0x01, 0x02, 0x03, 0x04}; // Example command, replace with actual
- byte Preamble[3] = {0x55, 0xAA, 0xFF}; // Example preamble
- byte ALDLbyte;
- int i, DataStreamIndex, ByteCount = 64; // Example value
- byte DataBytes[65];
- unsigned long PreambleTimer, StartTime, PreambleFound, SilenceFound, CommInit8192;
- float RPM, TPS, MAF, BLCELL, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime;
- int linecount = 16; // Example for hex output line count
- int bytecounter;
- // 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;
- float T_Diff;
- float TempRange;
- float 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;
- }
- void setup(void)
- {
- // put your setup code here, to run once:
- HC05.begin(9600);
- Serial.begin(115200); // Initialize Serial for debugging and output
- // Initialize pins
- pinMode(ALDLTestPin, INPUT); // ALDL activity detection pin
- pinMode(DecodeDataOutputPin, INPUT_PULLUP); // Decoded data output control
- pinMode(HexDataOutputPin, INPUT_PULLUP); // Hex data output control
- // Initialize variables
- i = 0; // Reset preamble index
- PreambleFound = false;
- SilenceFound = false;
- CommInit8192 = false;
- StartTime = 0;
- PreambleTimer = 0;
- bytecounter = 0;
- }
- void loop() {
- // Wait for silence period on the ALDL
- Serial.print("wait for silence ");
- SilenceFound = false; // Reset silence flag
- unsigned long StartTimeMicros = micros(); // First look for an active signal or a timeout - initialize timer
- // Should exit this loop on the start of a bit
- while ((micros() - StartTimeMicros) < 15000) // Wait for a 15 ms silent period
- {
- if (digitalRead(ALDLTestPin) == 0) // Any line activity resets the start time
- {
- StartTimeMicros = 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
- {
- 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
- {
- // Send Mode 1 command
- Serial.print(" M1 cmd ");
- i = 0; // use bytecounter to send the outgoing Mode1CMD sequence
- 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
- PreambleTimer = millis(); // Initialize timer to detect timeout
- while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false))
- {
- if (Serial1.available() > 0)
- {
- ALDLbyte = Serial1.read();
- if (ALDLbyte == Preamble[i])
- {
- i++; // Increment the preamble index
- if (i > 2) PreambleFound = true; // Preamble found
- }
- else
- {
- PreambleFound = false; // Reset preamble found flag
- i = 0; // Reset the preamble index
- }
- }
- }
- }
- // Read data stream
- DataStreamIndex = 1; // Start at index 1
- while (DataStreamIndex < 65) // Read 64 bytes (including checksum)
- {
- 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 over Bluetooth to Android
- // For simplicity, send parsed data as a comma-separated string
- // Parse data
- RPM = DataBytes[11] * 25; // Engine 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;
- O2mv = DataBytes[17] * 4.44;
- MAT = T_interpolate(DataBytes[30]);
- Runtime = (DataBytes[52] * 256 + DataBytes[53]);
- // Prepare string to send over Bluetooth
- String dataString = "";
- dataString += "RPM:" + String(RPM) + ",";
- dataString += "TPS:" + String(TPS) + ",";
- dataString += "MAF:" + String(MAF) + ",";
- dataString += "BLCELL:" + String(BLCELL) + ",";
- dataString += "BLM:" + String(BLM) + ",";
- dataString += "INTEGRATOR:" + String(INTEGRATOR) + ",";
- dataString += "InjPW:" + String(InjPW) + ",";
- dataString += "O2mv:" + String(O2mv) + ",";
- dataString += "MAT:" + String(MAT) + ",";
- dataString += "Runtime:" + String(Runtime);
- // Send over Bluetooth
- HC05.println(dataString);
- // Existing code for checksum verification and output
- if (digitalRead(DecodeDataOutputPin) == LOW)
- {
- // Decoded data output
- 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) ");
- }
- // Parse data (already done above)
- // Print parsed 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 readability
- unsigned long StartTime = millis();
- while (millis() < StartTime + 3000)
- {
- if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // Flush buffer
- }
- }
- else if (digitalRead(HexDataOutputPin) == LOW)
- {
- // Hex data output
- 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: ");
- }
- 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
- {
- // Raw binary stream
- Serial.write(0x80);
- Serial.write(0x95);
- Serial.write(0x01);
- for (int j = 1; j < ByteCount + 1; j++)
- {
- Serial.write(DataBytes[j]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment