pleasedontcode

Vehicle Data rev_24

Jul 12th, 2025
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Vehicle Data
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-07-12 21:08:55
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to android app */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <SoftwareSerial.h>
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float T_interpolate(byte DS_Temp);
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. const uint8_t HC05_Bluetooth_TX_PIN = A0; // Transmit pin for HC05 (Serial TX)
  35. const uint8_t HC05_Bluetooth_RX_PIN = A1; // Receive pin for HC05 (Serial RX)
  36. SoftwareSerial HC05(HC05_Bluetooth_RX_PIN, HC05_Bluetooth_TX_PIN); // RX, TX
  37.  
  38. // Additional variables from USER CODE
  39. // Hardware serial for serial1 (Serial1) is used for ALDL communication
  40. // On Arduino Mega, Serial1 is available on pins 18 (TX) and 19 (RX)
  41. const byte ALDLSerialPin_TX = 18; // TX pin for Serial1
  42. const byte ALDLSerialPin_RX = 19; // RX pin for Serial1
  43.  
  44. // Variable declarations from USER CODE
  45. const byte ALDLTestPin = 4; // Input used to listen for 160 BAUD activity
  46. const byte DecodeDataOutputPin = 5; // Input pin for decoded data output control
  47. const byte HexDataOutputPin = 6; // Input pin for hex data output control
  48.  
  49. int ALDLbyte = 0; // One byte of ALDL data
  50. int bytecounter = 0; // count bytes for line spacing in output
  51. const int linecount = 64; // 32 bytes per line
  52. byte M1Cmd[4] = {0x80, 0x56, 0x01, 0x29}; // Mode 1 command to start 8192 Mode 1 DataStream (80 56 01 29 HEX)
  53. byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble from ECM indicates start of 8192 Mode 1 DataStream (80 95 01 HEX)
  54. bool PreambleFound = false; // Reset preamble found flag
  55. bool SilenceFound = false; // Flag to indicate ECM silence in 160 baud mode
  56. bool CommInit8192 = false; // Flag indicating 8192 baud communication initialized
  57. const double SilenceWait = 15000; // Minimum silence period before Mode 1 command
  58. unsigned long PreambleTimer; // Timeout timer for preamble
  59. const int ByteCount = 64; // Number of data bytes following preamble
  60. byte DataBytes[ByteCount]; // Array to hold serial data stream
  61. int DataStreamIndex = 1; // Data stream byte index
  62. int i = 0; // Preamble index counter
  63. unsigned long StartTime; // Timer for measuring incoming data
  64. unsigned int CheckTotal; // Total for checksum calculation
  65. byte CheckSum; // Calculated checksum
  66.  
  67. // Decoded data variables
  68. float RPM; // Engine RPM
  69. float TPS; // Throttle position percentage
  70. float MAF; // Mass Air Flow in gm/sec
  71. float InjPW; // Injector pulse width
  72. float O2mv; // O2 sensor millivolts
  73. int BLCELL; // Block learn cell
  74. int BLM; // Block learn value
  75. int INTEGRATOR; // Fuel control integrator
  76. float MAT; // Intake manifold temp
  77. unsigned int Runtime; // Engine run time
  78.  
  79. void setup() {
  80.   // Configure I/O pins
  81.   pinMode(ALDLTestPin, INPUT);
  82.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);
  83.   pinMode(HexDataOutputPin, INPUT_PULLUP);
  84.  
  85.   // Initialize Serial ports
  86.   Serial.begin(115200); // Serial monitor
  87.   HC05.begin(9600); // Bluetooth HC05 module
  88.   Serial1.begin(8192); // ALDL serial communication at 8192 baud
  89.  
  90.   delay(1500); // Diagnostic delay
  91.   Serial.println("Ready for data capture");
  92. }
  93.  
  94. void loop() {
  95.   // Wait for silence on ALDL line
  96.   Serial.print("wait for silence ");
  97.   SilenceFound = false;
  98.   StartTime = micros();
  99.  
  100.   while ((micros() - StartTime) < 15000) { // Wait 15 ms silence
  101.     if (digitalRead(ALDLTestPin) == 0) {
  102.       StartTime = micros(); // Reset timer on activity
  103.     }
  104.   }
  105.   SilenceFound = true;
  106.  
  107.   while (SilenceFound == true) {
  108.     // Send Mode 1 command to ECM
  109.     PreambleFound = false;
  110.     while (PreambleFound == false) {
  111.       Serial.print(" M1 cmd ");
  112.       i = 0;
  113.       while (i < 4) {
  114.         Serial1.write(M1Cmd[i]);
  115.         i++;
  116.       }
  117.       Serial.println(" Finding Preamble ");
  118.       i = 0;
  119.       PreambleTimer = millis();
  120.       while ((millis() - PreambleTimer) < 100 && PreambleFound == false) {
  121.         if (Serial1.available() > 0) {
  122.           ALDLbyte = Serial1.read();
  123.           if (ALDLbyte == Preamble[i]) {
  124.             i++;
  125.             if (i > 2) PreambleFound = true;
  126.           } else {
  127.             PreambleFound = false;
  128.             i = 0;
  129.           }
  130.         }
  131.       }
  132.     }
  133.  
  134.     // Read data stream after preamble
  135.     DataStreamIndex = 1;
  136.     while (DataStreamIndex < 65) {
  137.       if (Serial1.available() > 0) {
  138.         DataBytes[DataStreamIndex] = Serial1.read();
  139.         DataStreamIndex++;
  140.       }
  141.     }
  142.  
  143.     // Calculate checksum
  144.     i = 1;
  145.     CheckTotal = 0x80 + 0x95 + 0x01;
  146.     while (i < ByteCount) {
  147.       CheckTotal += DataBytes[i];
  148.       i++;
  149.     }
  150.     CheckSum = 0x200 - CheckTotal; // Two's complement
  151.  
  152.     // Send data over Bluetooth in JSON format
  153.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  154.       // Send decoded data
  155.       Serial.print("New Data Stream received at ");
  156.       Serial.print(millis());
  157.       Serial.print(" Calc CHECKSUM: ");
  158.       Serial.print(CheckSum, HEX);
  159.       Serial.print(" Transmitted CHECKSUM: ");
  160.       Serial.print(DataBytes[ByteCount], HEX);
  161.       if (CheckSum == DataBytes[ByteCount]) {
  162.         Serial.println(" Checksum GOOD - Sending data over Bluetooth");
  163.       } else {
  164.         Serial.println("Checksum *** ERROR *** - Sending data over Bluetooth");
  165.       }
  166.  
  167.       // Parse data
  168.       RPM = DataBytes[11] * 25;
  169.       TPS = DataBytes[10] * 0.019608;
  170.       MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906;
  171.       BLCELL = DataBytes[21];
  172.       BLM = DataBytes[20];
  173.       INTEGRATOR = DataBytes[22];
  174.       InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259;
  175.       O2mv = DataBytes[17] * 4.44;
  176.       MAT = T_interpolate(DataBytes[30]);
  177.       Runtime = (DataBytes[52] * 256) + DataBytes[53];
  178.  
  179.       // Construct JSON string
  180.       String jsonData = "{";
  181.       jsonData += "\"RPM\":" + String(RPM) + ",";
  182.       jsonData += "\"TPS\":" + String(TPS) + ",";
  183.       jsonData += "\"MAF\":" + String(MAF) + ",";
  184.       jsonData += "\"InjPW\":" + String(InjPW) + ",";
  185.       jsonData += "\"O2mv\":" + String(O2mv) + ",";
  186.       jsonData += "\"BLCELL\":" + String(BLCELL) + ",";
  187.       jsonData += "\"BLM\":" + String(BLM) + ",";
  188.       jsonData += "\"INTEGRATOR\":" + String(INTEGRATOR) + ",";
  189.       jsonData += "\"MAT\":" + String(MAT) + ",";
  190.       jsonData += "\"Runtime\":" + String(Runtime);
  191.       jsonData += "}";
  192.  
  193.       // Send JSON string over Bluetooth
  194.       HC05.println(jsonData);
  195.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  196.       // Send hex data
  197.       Serial.print("New Data Stream received at ");
  198.       Serial.print(millis());
  199.       Serial.print(" Calc CHECKSUM: ");
  200.       Serial.print(CheckSum, HEX);
  201.       Serial.print(" Transmitted CHECKSUM: ");
  202.       Serial.print(DataBytes[ByteCount], HEX);
  203.       if (CheckSum == DataBytes[ByteCount]) {
  204.         Serial.println(" Checksum GOOD - Sending hex data over Bluetooth");
  205.       } else {
  206.         Serial.println("Checksum *** ERROR *** - Sending hex data over Bluetooth");
  207.       }
  208.  
  209.       String hexString = "";
  210.       int j = 1;
  211.       bytecounter = 0;
  212.       while (j < ByteCount + 1) {
  213.         if (bytecounter == 0) {
  214.           hexString = "";
  215.         }
  216.         if (j > 1) {
  217.           hexString += ",";
  218.         }
  219.         hexString += String(DataBytes[j], HEX);
  220.         j++;
  221.         bytecounter++;
  222.         if (bytecounter >= linecount) {
  223.           bytecounter = 0;
  224.           HC05.println(hexString);
  225.         }
  226.       }
  227.       if (bytecounter > 0) {
  228.         HC05.println(hexString);
  229.       }
  230.     } else {
  231.       // Send raw binary data stream
  232.       Serial.write(0x80);
  233.       Serial.write(0x95);
  234.       Serial.write(0x01);
  235.       for (int j = 1; j < ByteCount + 1; j++) {
  236.         Serial.write(DataBytes[j]);
  237.       }
  238.     }
  239.   }
  240. }
  241.  
  242. // Interpolation function
  243. float T_interpolate(byte DS_Temp) {
  244.   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};
  245.   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};
  246.   float T_Range;
  247.   float T_Diff;
  248.   float TempRange;
  249.   float Temperature;
  250.   int i = 0;
  251.   while (i < 38) {
  252.     if (TempScale[i] > DS_Temp) break;
  253.     i++;
  254.   }
  255.   if (i > 0) {
  256.     T_Range = TempScale[i] - TempScale[i - 1];
  257.     T_Diff = DS_Temp - TempScale[i - 1];
  258.     TempRange = TempValue[i] - TempValue[i - 1];
  259.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  260.   } else {
  261.     Temperature = TempValue[0];
  262.   }
  263.   return Temperature;
  264. }
  265.  
Advertisement
Add Comment
Please, Sign In to add comment