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-12 21:08:55
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* send data from bluetooth to android app */
- /****** 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 *****/
- const uint8_t HC05_Bluetooth_TX_PIN = A0; // Transmit pin for HC05 (Serial TX)
- const uint8_t HC05_Bluetooth_RX_PIN = A1; // Receive pin for HC05 (Serial RX)
- SoftwareSerial HC05(HC05_Bluetooth_RX_PIN, HC05_Bluetooth_TX_PIN); // RX, TX
- // Additional variables from USER CODE
- // Hardware serial for serial1 (Serial1) is used for ALDL communication
- // On Arduino Mega, Serial1 is available on pins 18 (TX) and 19 (RX)
- const byte ALDLSerialPin_TX = 18; // TX pin for Serial1
- const byte ALDLSerialPin_RX = 19; // RX pin for Serial1
- // Variable declarations from USER CODE
- const byte ALDLTestPin = 4; // Input used to listen for 160 BAUD activity
- 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 indicating 8192 baud communication initialized
- const double SilenceWait = 15000; // Minimum silence period before Mode 1 command
- 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
- // Decoded data variables
- float RPM; // Engine RPM
- float TPS; // Throttle position percentage
- float MAF; // Mass Air Flow in gm/sec
- float InjPW; // Injector pulse width
- float O2mv; // O2 sensor millivolts
- int BLCELL; // Block learn cell
- int BLM; // Block learn value
- int INTEGRATOR; // Fuel control integrator
- float MAT; // Intake manifold temp
- unsigned int Runtime; // Engine run time
- void setup() {
- // Configure I/O pins
- pinMode(ALDLTestPin, INPUT);
- pinMode(DecodeDataOutputPin, INPUT_PULLUP);
- pinMode(HexDataOutputPin, INPUT_PULLUP);
- // Initialize Serial ports
- Serial.begin(115200); // Serial monitor
- HC05.begin(9600); // Bluetooth HC05 module
- Serial1.begin(8192); // ALDL serial communication at 8192 baud
- delay(1500); // Diagnostic delay
- Serial.println("Ready for data capture");
- }
- void loop() {
- // Wait for silence on ALDL line
- Serial.print("wait for silence ");
- SilenceFound = false;
- StartTime = micros();
- while ((micros() - StartTime) < 15000) { // Wait 15 ms silence
- if (digitalRead(ALDLTestPin) == 0) {
- StartTime = micros(); // Reset timer on activity
- }
- }
- SilenceFound = true;
- while (SilenceFound == true) {
- // Send Mode 1 command to ECM
- PreambleFound = false;
- while (PreambleFound == false) {
- 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 == 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 after preamble
- DataStreamIndex = 1;
- while (DataStreamIndex < 65) {
- if (Serial1.available() > 0) {
- DataBytes[DataStreamIndex] = Serial1.read();
- DataStreamIndex++;
- }
- }
- // Calculate checksum
- i = 1;
- CheckTotal = 0x80 + 0x95 + 0x01;
- while (i < ByteCount) {
- CheckTotal += DataBytes[i];
- i++;
- }
- CheckSum = 0x200 - CheckTotal; // Two's complement
- // Send data over Bluetooth in JSON format
- 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 - Sending data over Bluetooth");
- } else {
- Serial.println("Checksum *** ERROR *** - Sending data over Bluetooth");
- }
- // Parse data
- RPM = DataBytes[11] * 25;
- TPS = DataBytes[10] * 0.019608;
- MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906;
- 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];
- // Construct JSON string
- String jsonData = "{";
- jsonData += "\"RPM\":" + String(RPM) + ",";
- jsonData += "\"TPS\":" + String(TPS) + ",";
- jsonData += "\"MAF\":" + String(MAF) + ",";
- jsonData += "\"InjPW\":" + String(InjPW) + ",";
- jsonData += "\"O2mv\":" + String(O2mv) + ",";
- jsonData += "\"BLCELL\":" + String(BLCELL) + ",";
- jsonData += "\"BLM\":" + String(BLM) + ",";
- jsonData += "\"INTEGRATOR\":" + String(INTEGRATOR) + ",";
- jsonData += "\"MAT\":" + String(MAT) + ",";
- jsonData += "\"Runtime\":" + String(Runtime);
- jsonData += "}";
- // Send JSON string over Bluetooth
- HC05.println(jsonData);
- } else if (digitalRead(HexDataOutputPin) == LOW) {
- // Send 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[ByteCount], HEX);
- if (CheckSum == DataBytes[ByteCount]) {
- Serial.println(" Checksum GOOD - Sending hex data over Bluetooth");
- } else {
- Serial.println("Checksum *** ERROR *** - Sending hex data over Bluetooth");
- }
- String hexString = "";
- int j = 1;
- bytecounter = 0;
- while (j < ByteCount + 1) {
- if (bytecounter == 0) {
- hexString = "";
- }
- if (j > 1) {
- hexString += ",";
- }
- hexString += String(DataBytes[j], HEX);
- j++;
- bytecounter++;
- if (bytecounter >= linecount) {
- bytecounter = 0;
- HC05.println(hexString);
- }
- }
- if (bytecounter > 0) {
- HC05.println(hexString);
- }
- } else {
- // Send raw binary data stream
- Serial.write(0x80);
- Serial.write(0x95);
- Serial.write(0x01);
- for (int j = 1; j < ByteCount + 1; j++) {
- Serial.write(DataBytes[j]);
- }
- }
- }
- }
- // 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment