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:19:51
- ********* 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);
- /***** DEFINITION OF Software Serial *****/
- // Define Bluetooth module pins
- const uint8_t HC05_BluetoothModule_TX_PIN = A0; // Bluetooth TX pin connected to Arduino Mega A0
- const uint8_t HC05_BluetoothModule_RX_PIN = A1; // Bluetooth RX pin connected to Arduino Mega A1
- // Instantiate SoftwareSerial for Bluetooth communication
- SoftwareSerial HC05(HC05_BluetoothModule_TX_PIN, HC05_BluetoothModule_RX_PIN); // Initialize SoftwareSerial for Bluetooth communication
- // Define other pins used in the code (assuming they are declared elsewhere in your full code)
- const uint8_t ALDLTestPin = 2; // Example pin, replace with actual pin if different
- const uint8_t DecodeDataOutputPin = 3; // Example pin, replace with actual pin if different
- const uint8_t HexDataOutputPin = 4; // Example pin, replace with actual pin if different
- // Variables used in the code
- bool SilenceFound = false;
- unsigned long StartTime = 0;
- bool PreambleFound = false;
- unsigned long PreambleTimer = 0;
- byte ALDLbyte = 0;
- int DataStreamIndex = 1;
- const int ByteCount = 64; // Total bytes in data stream
- byte DataBytes[65]; // Data buffer
- const byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble sequence
- int i = 0;
- int CheckTotal = 0;
- int CheckSum = 0;
- const int linecount = 16; // Number of bytes per line in hex output
- int bytecounter = 0;
- // Command sequence for Mode 1 request (assuming it's defined elsewhere)
- const byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, replace with actual if different
- // Variables for parsed data
- int RPM = 0;
- float TPS = 0.0;
- float MAF = 0.0;
- byte BLCELL = 0;
- byte BLM = 0;
- byte INTEGRATOR = 0;
- float InjPW = 0.0;
- float O2mv = 0.0;
- float MAT = 0.0;
- unsigned long Runtime = 0;
- void setup() {
- // Configure I/O pins
- pinMode(ALDLTestPin, INPUT);
- pinMode(DecodeDataOutputPin, INPUT_PULLUP);
- pinMode(HexDataOutputPin, INPUT_PULLUP);
- // Initialize serial communications
- Serial.begin(115200); // Serial monitor
- Serial1.begin(8192); // ALDL data serial at 8192 baud
- delay(1500);
- Serial.println("Ready for data capture");
- // Initialize Bluetooth serial
- HC05.begin(9600); // Bluetooth serial at 9600 baud
- // Initialize variables
- i = 0; // Reset preamble index
- }
- void loop() {
- // Wait for silence period on the ALDL
- Serial.print("wait for silence ");
- SilenceFound = false; // Reset silence flag
- StartTime = micros(); // Start timer
- // Wait for 15 ms of silence
- while ((micros() - StartTime) < 15000) {
- if (digitalRead(ALDLTestPin) == 0) {
- StartTime = micros(); // Reset timer if activity detected
- }
- }
- SilenceFound = true; // Silence detected
- while (SilenceFound == true) {
- // Continuously request and transmit Mode 1 data
- PreambleFound = false; // Reset preamble flag
- // Send Mode 1 command sequence
- Serial.print(" M1 cmd ");
- i = 0;
- while (i < 4) {
- Serial1.write(M1Cmd[i]);
- i++;
- }
- Serial.println(" Finding Preamble ");
- i = 0;
- PreambleTimer = millis();
- // Search for preamble in incoming data
- while ((millis() - PreambleTimer) < 100 && PreambleFound == false) {
- 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
- 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 over Bluetooth and Serial based on output mode
- if (digitalRead(DecodeDataOutputPin) == LOW) {
- // Send 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[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
- 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]);
- // Output parsed data to Serial monitor
- 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");
- // Send parsed data over Bluetooth
- HC05.print("Engine Speed: ");
- HC05.print(RPM);
- HC05.println(" RPM");
- HC05.print("Throttle Position: ");
- HC05.print(TPS);
- HC05.println(" V");
- HC05.print("Mass Air Flow: ");
- HC05.print(MAF);
- HC05.println(" g/sec");
- HC05.print("BLM Cell: ");
- HC05.print(BLCELL);
- HC05.print(" BLM: ");
- HC05.print(BLM);
- HC05.print(" Fuel Integrator: ");
- HC05.println(INTEGRATOR);
- HC05.print("Injector Pulse: ");
- HC05.print(InjPW);
- HC05.println(" ms");
- HC05.print("O2 Voltage: ");
- HC05.print(O2mv);
- HC05.println(" mV");
- HC05.print("Intake Temp: ");
- HC05.print(MAT);
- HC05.println(" C");
- HC05.print("Runtime: ");
- HC05.print(Runtime);
- HC05.println(" sec");
- // 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) {
- // Send hex data over Bluetooth and Serial
- 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);
- HC05.print(DataBytes[j], HEX); // Send over Bluetooth
- j++;
- bytecounter++;
- if (bytecounter >= linecount) {
- bytecounter = 0;
- Serial.println("");
- HC05.println("");
- } else {
- Serial.print(" ");
- HC05.print(" ");
- }
- }
- Serial.println("");
- HC05.println("");
- } else {
- // Send raw binary data over Serial and Bluetooth
- Serial.write(0x80);
- Serial.write(0x95);
- Serial.write(0x01);
- HC05.write(0x80);
- HC05.write(0x95);
- HC05.write(0x01);
- for (int j = 1; j < ByteCount + 1; j++) {
- Serial.write(DataBytes[j]);
- HC05.write(DataBytes[j]);
- }
- }
- }
- }
- // Implementation of T_interpolate function from user code
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment