pleasedontcode

Vehicle Data rev_18

Jul 11th, 2025
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.02 KB | None | 0 0
  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-11 22:19:51
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to android */
  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. // Define Bluetooth module pins
  35. const uint8_t HC05_BluetoothModule_TX_PIN = A0; // Bluetooth TX pin connected to Arduino Mega A0
  36. const uint8_t HC05_BluetoothModule_RX_PIN = A1; // Bluetooth RX pin connected to Arduino Mega A1
  37.  
  38. // Instantiate SoftwareSerial for Bluetooth communication
  39. SoftwareSerial HC05(HC05_BluetoothModule_TX_PIN, HC05_BluetoothModule_RX_PIN); // Initialize SoftwareSerial for Bluetooth communication
  40.  
  41. // Define other pins used in the code (assuming they are declared elsewhere in your full code)
  42. const uint8_t ALDLTestPin = 2; // Example pin, replace with actual pin if different
  43. const uint8_t DecodeDataOutputPin = 3; // Example pin, replace with actual pin if different
  44. const uint8_t HexDataOutputPin = 4; // Example pin, replace with actual pin if different
  45.  
  46. // Variables used in the code
  47. bool SilenceFound = false;
  48. unsigned long StartTime = 0;
  49. bool PreambleFound = false;
  50. unsigned long PreambleTimer = 0;
  51. byte ALDLbyte = 0;
  52. int DataStreamIndex = 1;
  53. const int ByteCount = 64; // Total bytes in data stream
  54. byte DataBytes[65]; // Data buffer
  55. const byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble sequence
  56. int i = 0;
  57. int CheckTotal = 0;
  58. int CheckSum = 0;
  59. const int linecount = 16; // Number of bytes per line in hex output
  60. int bytecounter = 0;
  61.  
  62. // Command sequence for Mode 1 request (assuming it's defined elsewhere)
  63. const byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, replace with actual if different
  64.  
  65. // Variables for parsed data
  66. int RPM = 0;
  67. float TPS = 0.0;
  68. float MAF = 0.0;
  69. byte BLCELL = 0;
  70. byte BLM = 0;
  71. byte INTEGRATOR = 0;
  72. float InjPW = 0.0;
  73. float O2mv = 0.0;
  74. float MAT = 0.0;
  75. unsigned long Runtime = 0;
  76.  
  77. void setup() {
  78.   // Configure I/O pins
  79.   pinMode(ALDLTestPin, INPUT);
  80.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);
  81.   pinMode(HexDataOutputPin, INPUT_PULLUP);
  82.  
  83.   // Initialize serial communications
  84.   Serial.begin(115200); // Serial monitor
  85.   Serial1.begin(8192); // ALDL data serial at 8192 baud
  86.   delay(1500);
  87.   Serial.println("Ready for data capture");
  88.  
  89.   // Initialize Bluetooth serial
  90.   HC05.begin(9600); // Bluetooth serial at 9600 baud
  91.  
  92.   // Initialize variables
  93.   i = 0; // Reset preamble index
  94. }
  95.  
  96. void loop() {
  97.   // Wait for silence period on the ALDL
  98.   Serial.print("wait for silence ");
  99.   SilenceFound = false; // Reset silence flag
  100.   StartTime = micros(); // Start timer
  101.  
  102.   // Wait for 15 ms of silence
  103.   while ((micros() - StartTime) < 15000) {
  104.     if (digitalRead(ALDLTestPin) == 0) {
  105.       StartTime = micros(); // Reset timer if activity detected
  106.     }
  107.   }
  108.   SilenceFound = true; // Silence detected
  109.  
  110.   while (SilenceFound == true) {
  111.     // Continuously request and transmit Mode 1 data
  112.     PreambleFound = false; // Reset preamble flag
  113.  
  114.     // Send Mode 1 command sequence
  115.     Serial.print(" M1 cmd ");
  116.     i = 0;
  117.     while (i < 4) {
  118.       Serial1.write(M1Cmd[i]);
  119.       i++;
  120.     }
  121.     Serial.println(" Finding Preamble  ");
  122.     i = 0;
  123.     PreambleTimer = millis();
  124.  
  125.     // Search for preamble in incoming data
  126.     while ((millis() - PreambleTimer) < 100 && PreambleFound == false) {
  127.       if (Serial1.available() > 0) {
  128.         ALDLbyte = Serial1.read();
  129.         if (ALDLbyte == Preamble[i]) {
  130.           i++;
  131.           if (i > 2) PreambleFound = true;
  132.         } else {
  133.           PreambleFound = false;
  134.           i = 0;
  135.         }
  136.       }
  137.     }
  138.  
  139.     // Read data stream
  140.     DataStreamIndex = 1;
  141.     while (DataStreamIndex < 65) {
  142.       if (Serial1.available() > 0) {
  143.         DataBytes[DataStreamIndex] = Serial1.read();
  144.         DataStreamIndex++;
  145.       }
  146.     }
  147.  
  148.     // Calculate checksum
  149.     i = 1;
  150.     CheckTotal = 0x80 + 0x95 + 0x01; // sum preamble bytes
  151.     while (i < ByteCount) {
  152.       CheckTotal += DataBytes[i];
  153.       i++;
  154.     }
  155.     CheckSum = 0x200 - CheckTotal; // two's complement
  156.  
  157.     // Send data over Bluetooth and Serial based on output mode
  158.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  159.       // Send decoded data
  160.       Serial.print("New Data Stream received at ");
  161.       Serial.print(millis());
  162.       Serial.print(" Calc CHECKSUM: ");
  163.       Serial.print(CheckSum, HEX);
  164.       Serial.print(" Transmitted CHECKSUM: ");
  165.       Serial.print(DataBytes[ByteCount], HEX);
  166.       if (CheckSum == DataBytes[ByteCount]) {
  167.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  168.       } else {
  169.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  170.       }
  171.  
  172.       // Parse data
  173.       RPM = DataBytes[11] * 25; // Engine RPM
  174.       TPS = DataBytes[10] * 0.019608; // TPS volts
  175.       MAF = ((DataBytes[36] * 256) + (DataBytes[37])) * 0.003906; // MAF gm/sec
  176.       BLCELL = DataBytes[21];
  177.       BLM = DataBytes[20];
  178.       INTEGRATOR = DataBytes[22];
  179.       InjPW = ((DataBytes[45] * 256) + (DataBytes[46])) * 0.015259;
  180.       O2mv = DataBytes[17] * 4.44;
  181.       MAT = T_interpolate(DataBytes[30]);
  182.       Runtime = (DataBytes[52] * 256 + DataBytes[53]);
  183.  
  184.       // Output parsed data to Serial monitor
  185.       Serial.print("Engine Speed     : ");
  186.       Serial.print(RPM);
  187.       Serial.println(" RPM");
  188.       Serial.print("Throttle Position: ");
  189.       Serial.print(TPS);
  190.       Serial.println(" Volts");
  191.       Serial.print("Mass Air Flow    : ");
  192.       Serial.print(MAF);
  193.       Serial.println(" Grams/Sec");
  194.       Serial.print("Current BLM Cell: ");
  195.       Serial.print(BLCELL);
  196.       Serial.print(" BLM Value: ");
  197.       Serial.print(BLM);
  198.       Serial.print("  Current Fuel Integrator: ");
  199.       Serial.println(INTEGRATOR);
  200.       Serial.print("Injector Pulse   : ");
  201.       Serial.print(InjPW);
  202.       Serial.println(" Milliseconds");
  203.       Serial.print("O2 Sensor Voltage: ");
  204.       Serial.print(O2mv);
  205.       Serial.println(" Millivolts");
  206.       Serial.print("Intake Air Temp  : ");
  207.       Serial.print(MAT);
  208.       Serial.println(" Deg C");
  209.       Serial.print("Engine Run Time  : ");
  210.       Serial.print(Runtime);
  211.       Serial.println(" Seconds");
  212.  
  213.       // Send parsed data over Bluetooth
  214.       HC05.print("Engine Speed: ");
  215.       HC05.print(RPM);
  216.       HC05.println(" RPM");
  217.       HC05.print("Throttle Position: ");
  218.       HC05.print(TPS);
  219.       HC05.println(" V");
  220.       HC05.print("Mass Air Flow: ");
  221.       HC05.print(MAF);
  222.       HC05.println(" g/sec");
  223.       HC05.print("BLM Cell: ");
  224.       HC05.print(BLCELL);
  225.       HC05.print(" BLM: ");
  226.       HC05.print(BLM);
  227.       HC05.print(" Fuel Integrator: ");
  228.       HC05.println(INTEGRATOR);
  229.       HC05.print("Injector Pulse: ");
  230.       HC05.print(InjPW);
  231.       HC05.println(" ms");
  232.       HC05.print("O2 Voltage: ");
  233.       HC05.print(O2mv);
  234.       HC05.println(" mV");
  235.       HC05.print("Intake Temp: ");
  236.       HC05.print(MAT);
  237.       HC05.println(" C");
  238.       HC05.print("Runtime: ");
  239.       HC05.print(Runtime);
  240.       HC05.println(" sec");
  241.  
  242.       // Delay for readability
  243.       unsigned long StartTime = millis();
  244.       while (millis() < StartTime + 3000) {
  245.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // flush buffer
  246.       }
  247.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  248.       // Send hex data over Bluetooth and Serial
  249.       Serial.print("New Data Stream received at ");
  250.       Serial.print(millis());
  251.       Serial.print(" Calc CHECKSUM: ");
  252.       Serial.print(CheckSum, HEX);
  253.       Serial.print(" Transmitted CHECKSUM: ");
  254.       Serial.print(DataBytes[ByteCount], HEX);
  255.       if (CheckSum == DataBytes[ByteCount]) {
  256.         Serial.println(" Checksum GOOD - Data as follows: ");
  257.       } else {
  258.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  259.       }
  260.  
  261.       int j = 1;
  262.       bytecounter = 0;
  263.       while (j < ByteCount + 1) {
  264.         Serial.print(DataBytes[j], HEX);
  265.         HC05.print(DataBytes[j], HEX); // Send over Bluetooth
  266.         j++;
  267.         bytecounter++;
  268.         if (bytecounter >= linecount) {
  269.           bytecounter = 0;
  270.           Serial.println("");
  271.           HC05.println("");
  272.         } else {
  273.           Serial.print(" ");
  274.           HC05.print(" ");
  275.         }
  276.       }
  277.       Serial.println("");
  278.       HC05.println("");
  279.     } else {
  280.       // Send raw binary data over Serial and Bluetooth
  281.       Serial.write(0x80);
  282.       Serial.write(0x95);
  283.       Serial.write(0x01);
  284.       HC05.write(0x80);
  285.       HC05.write(0x95);
  286.       HC05.write(0x01);
  287.       for (int j = 1; j < ByteCount + 1; j++) {
  288.         Serial.write(DataBytes[j]);
  289.         HC05.write(DataBytes[j]);
  290.       }
  291.     }
  292.   }
  293. }
  294.  
  295. // Implementation of T_interpolate function from user code
  296. float T_interpolate(byte DS_Temp) {
  297.   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};
  298.   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};
  299.   float T_Range;
  300.   float T_Diff;
  301.   float TempRange;
  302.   float Temperature;
  303.   int i = 0;
  304.   while (i < 38) {
  305.     if (TempScale[i] > DS_Temp) break;
  306.     i++;
  307.   }
  308.   if (i > 0) {
  309.     T_Range = TempScale[i] - TempScale[i - 1];
  310.     T_Diff = DS_Temp - TempScale[i - 1];
  311.     TempRange = TempValue[i] - TempValue[i - 1];
  312.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  313.   } else {
  314.     Temperature = TempValue[0];
  315.   }
  316.   return Temperature;
  317. }
  318.  
Advertisement
Add Comment
Please, Sign In to add comment