pleasedontcode

Vehicle Data rev_14

Jul 11th, 2025
437
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-11 21:55:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* transive data from bluetooth to android */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26.  
  27. /****** FUNCTION PROTOTYPES *****/
  28. void setup(void);
  29. void loop(void);
  30. float T_interpolate(byte DS_Temp); // Prototype for the interpolation function
  31.  
  32. // Declare all variables and constants used in the code
  33. const byte ALDLTestPin = 4;                  // ALDL input pin
  34. const byte DecodeDataOutputPin = 2;          // Output pin for decoded data
  35. const byte HexDataOutputPin = 3;             // Output pin for hex data
  36. const byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble sequence
  37. const int ByteCount = 64;                    // Total bytes expected in data stream
  38. byte DataBytes[65];                          // Array to hold data bytes
  39. byte ALDLbyte = 0;                           // Byte read from Serial1
  40. unsigned long PreambleTimer = 0;             // Timer for preamble timeout
  41. unsigned long StartTime = 0;                 // Timer for silence detection
  42. bool SilenceFound = false;                   // Silence detection flag
  43. bool PreambleFound = false;                  // Preamble detection flag
  44. int DataStreamIndex = 1;                     // Data stream index
  45. float CheckTotal = 0;                        // Checksum total
  46. byte CheckSum = 0;                           // Calculated checksum
  47. int i = 0;                                 // Loop variable
  48. int linecount = 32;                        // lines per block for hex printing
  49.  
  50. // Variables related to data processing
  51. unsigned int RPM;
  52. float TPS;
  53. float MAF;
  54. byte BLCELL;
  55. byte BLM;
  56. byte INTEGRATOR;
  57. float InjPW;
  58. float O2mv;
  59. float MAT;
  60. unsigned long Runtime;
  61.  
  62. // Function prototypes for optional functions if needed
  63. float T_interpolate(byte DS_Temp);  // Already declared
  64.  
  65. void setup() {
  66.   // Setup pins
  67.   pinMode(ALDLTestPin, INPUT);                          // define D4 as an input pin
  68.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);           // decoded data output pin
  69.   pinMode(HexDataOutputPin, INPUT_PULLUP);              // hex data output pin
  70.  
  71.   // Initialize serial communications
  72.   Serial.begin(115200);                                // Serial monitor
  73.   Serial1.begin(8192);                                 // Serial port for ALDL data
  74.   delay(1500);
  75.   Serial.println("Ready for data capture");
  76.  
  77.   // Initialize variables
  78.   i = 0;
  79. }
  80.  
  81. void loop() {
  82.   // Wait for silence on the ALDL line
  83.   Serial.print("wait for silence ");
  84.   SilenceFound = false;
  85.   StartTime = micros();
  86.  
  87.   while ((micros() - StartTime) < 15000) {  // wait for 15 ms silence
  88.     if (digitalRead(ALDLTestPin) == 0) {   // line activity resets timeout
  89.       StartTime = micros();
  90.     }
  91.   }
  92.   SilenceFound = true;
  93.  
  94.   while (SilenceFound == true) {
  95.     // Send Mode 1 command
  96.     bool PreambleDetected = false;
  97.     while (!PreambleDetected) {
  98.       Serial.print(" M1 cmd ");
  99.       i = 0;
  100.       byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command - adjust as needed
  101.       for (i = 0; i < 4; i++) {
  102.         Serial1.write(M1Cmd[i]);
  103.       }
  104.  
  105.       Serial.println(" Finding Preamble ");
  106.       i = 0;
  107.       PreambleTimer = millis();
  108.  
  109.       // Search for preamble
  110.       while ((millis() - PreambleTimer) < 100 && !PreambleDetected) {
  111.         if (Serial1.available() > 0) {
  112.           ALDLbyte = Serial1.read();
  113.           if (ALDLbyte == Preamble[i]) {
  114.             i++;
  115.             if (i > 2) PreambleDetected = true; // found preamble
  116.           } else {
  117.             PreambleDetected = false;
  118.             i = 0;
  119.           }
  120.         }
  121.       }
  122.     }
  123.  
  124.     // Read full data packet
  125.     DataStreamIndex = 1;
  126.     while (DataStreamIndex < ByteCount + 1) {
  127.       if (Serial1.available() > 0) {
  128.         DataBytes[DataStreamIndex] = Serial1.read();
  129.         DataStreamIndex++;
  130.       }
  131.     }
  132.  
  133.     // Calculate checksum
  134.     i = 1;
  135.     CheckTotal = 0x80 + 0x95 + 0x01;  // Sum preamble
  136.     while (i < ByteCount) {
  137.       CheckTotal += DataBytes[i];
  138.       i++;
  139.     }
  140.     CheckSum = 0x200 - CheckTotal;
  141.  
  142.     // Send data according to output flags
  143.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  144.       Serial.print("New Data Stream received at ");
  145.       Serial.print(millis());
  146.       Serial.print(" Calc CHECKSUM: ");
  147.       Serial.print(CheckSum, HEX);
  148.       Serial.print(" Transmitted CHECKSUM: ");
  149.       Serial.print(DataBytes[ByteCount], HEX);
  150.       if (CheckSum == DataBytes[ByteCount]) {
  151.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  152.       } else {
  153.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  154.       }
  155.       // Process and display data
  156.       RPM = DataBytes[11] * 25; // Engine RPM
  157.       TPS = DataBytes[10] * 0.019608; // Throttle volts
  158.       MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906; // MAF GM/sec
  159.       BLCELL = DataBytes[21];
  160.       BLM = DataBytes[20];
  161.       INTEGRATOR = DataBytes[22];
  162.       InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259;
  163.       O2mv = DataBytes[17] * 4.44;
  164.       MAT = T_interpolate(DataBytes[30]);
  165.       Runtime = (DataBytes[52] * 256) + DataBytes[53];
  166.  
  167.       // Print data
  168.       Serial.print("Engine Speed : ");
  169.       Serial.print(RPM);
  170.       Serial.println(" RPM");
  171.       Serial.print("Throttle Position: ");
  172.       Serial.print(TPS);
  173.       Serial.println(" Volts");
  174.       Serial.print("Mass Air Flow : ");
  175.       Serial.print(MAF);
  176.       Serial.println(" Grams/Sec");
  177.       Serial.print("Current BLM Cell: ");
  178.       Serial.print(BLCELL);
  179.       Serial.print(" BLM Value: ");
  180.       Serial.print(BLM);
  181.       Serial.print(" Current Fuel Integrator: ");
  182.       Serial.println(INTEGRATOR);
  183.       Serial.print("Injector Pulse : ");
  184.       Serial.print(InjPW);
  185.       Serial.println(" Milliseconds");
  186.       Serial.print("O2 Sensor Voltage: ");
  187.       Serial.print(O2mv);
  188.       Serial.println(" Millivolts");
  189.       Serial.print("Intake Air Temp : ");
  190.       Serial.print(MAT);
  191.       Serial.println(" Deg C");
  192.       Serial.print("Engine Run Time : ");
  193.       Serial.print(Runtime);
  194.       Serial.println(" Seconds");
  195.  
  196.       // Delay for data reading
  197.       unsigned long StartTime = millis();
  198.       while (millis() < StartTime + 3000) {
  199.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // flush buffer
  200.       }
  201.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  202.       // Hex output
  203.       Serial.print("New Data Stream received at ");
  204.       Serial.print(millis());
  205.       Serial.print(" Calc CHECKSUM: ");
  206.       Serial.print(CheckSum, HEX);
  207.       Serial.print(" Transmitted CHECKSUM: ");
  208.       Serial.print(DataBytes[ByteCount], HEX);
  209.       if (CheckSum == DataBytes[ByteCount]) {
  210.         Serial.println(" Checksum GOOD - Data as follows: ");
  211.       } else {
  212.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  213.       }
  214.       int j = 1;
  215.       byte bytecounter = 0;
  216.       while (j < ByteCount + 1) {
  217.         Serial.print(DataBytes[j], HEX);
  218.         j++;
  219.         bytecounter++;
  220.         if (bytecounter >= linecount) {
  221.           bytecounter = 0;
  222.           Serial.println("");
  223.         } else {
  224.           Serial.print(" ");
  225.         }
  226.       }
  227.       Serial.println("");
  228.     } else {
  229.       // Raw binary data stream
  230.       Serial.write(0x80);
  231.       Serial.write(0x95);
  232.       Serial.write(0x01);
  233.       for (int j = 1; j < ByteCount + 1; j++) {
  234.         Serial.write(DataBytes[j]);
  235.       }
  236.     }
  237.   }
  238. }
  239.  
  240. // Interpolates temperature from thermistor data
  241. float T_interpolate(byte DS_Temp) {
  242.   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};
  243.   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};
  244.  
  245.   float T_Range, T_Diff, TempRange, Temperature;
  246.   int i = 0;
  247.   while (i < 38) {
  248.     if (TempScale[i] > DS_Temp) break;
  249.     i++;
  250.   }
  251.   if (i > 0) {
  252.     T_Range = TempScale[i] - TempScale[i - 1];
  253.     T_Diff = DS_Temp - TempScale[i - 1];
  254.     TempRange = TempValue[i] - TempValue[i - 1];
  255.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  256.   } else {
  257.     Temperature = TempValue[0];
  258.   }
  259.   return Temperature;
  260. }
  261.  
Advertisement
Add Comment
Please, Sign In to add comment