pleasedontcode

Vehicle Data rev_11

Jul 11th, 2025
438
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:40:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data to bluetooth to android */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. // No additional libraries needed for basic Bluetooth serial communication
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float T_interpolate(byte DS_Temp);
  32.  
  33. // Define Bluetooth serial object (assuming HC-05 connected to Serial2)
  34. #define BluetoothSerial Serial2
  35.  
  36. // Define pins (assuming these are defined elsewhere in the code)
  37. const int ALDLTestPin = 4;
  38. const int DecodeDataOutputPin = 2;
  39. const int HexDataOutputPin = 3;
  40.  
  41. // Define command and data variables
  42. byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, adjust as needed
  43. byte Preamble[3] = {0x80, 0x95, 0x01};
  44. bool SilenceFound = false;
  45. bool PreambleFound = false;
  46. int DataStreamIndex = 0;
  47. const int ByteCount = 64;
  48. byte DataBytes[65];
  49. int linecount = 32;
  50. int bytebytecounter = 0;
  51.  
  52. // Variables for data processing
  53. unsigned long ALDLbyte;
  54. float RPM, TPS, MAF, O2mv, MAT;
  55. int BLCELL, BLM, INTEGRATOR;
  56. float InjPW;
  57. unsigned long Runtime;
  58.  
  59. // Initialize Bluetooth in setup
  60. void setup()
  61. {
  62.   // **** I/O configuration and setup first
  63.   pinMode(ALDLTestPin, INPUT);
  64.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);
  65.   pinMode(HexDataOutputPin, INPUT_PULLUP);
  66.  
  67.   // Initialize serial ports
  68.   Serial.begin(115200);
  69.   Serial1.begin(8192);
  70.   BluetoothSerial.begin(9600); // Initialize Bluetooth serial at 9600 baud
  71.  
  72.   delay(1500);
  73.   Serial.println("Ready for data capture");
  74.  
  75.   int i=0; // Reset the preamble index flag
  76. }
  77.  
  78. // Main loop
  79. void loop() {
  80.   // Wait for silence period on the ALDL
  81.   Serial.print("wait for silence ");
  82.   SilenceFound = false;
  83.   unsigned long StartTime= micros();
  84.   while ((micros() - StartTime) < 15000)
  85.   {
  86.     if (digitalRead(ALDLTestPin) == 0)
  87.     {
  88.       StartTime= micros();
  89.     }
  90.   }
  91.   SilenceFound = true;
  92.  
  93.   while (SilenceFound == true)
  94.   {
  95.     PreambleFound = false;
  96.     while (PreambleFound == false)
  97.     {
  98.       Serial.print(" M1 cmd ");
  99.       int i=0;
  100.       while (i<4)
  101.       {
  102.         Serial1.write(M1Cmd[i]);
  103.         i++;
  104.       }
  105.       Serial.println(" Finding Preamble  ");
  106.       i=0;
  107.       unsigned long PreambleTimer = millis();
  108.       while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false))
  109.       {
  110.         if (Serial1.available() > 0)
  111.         {
  112.           ALDLbyte = Serial1.read();
  113.           if (ALDLbyte == Preamble[i])
  114.           {
  115.             i++;
  116.             if (i > 2) PreambleFound = true;
  117.           }
  118.           else
  119.           {
  120.             PreambleFound = false;
  121.             i=0;
  122.           }
  123.         }
  124.       }
  125.     }
  126.  
  127.     DataStreamIndex = 1;
  128.     while (DataStreamIndex < 65)
  129.     {
  130.       if (Serial1.available() > 0)
  131.       {
  132.         DataBytes[DataStreamIndex] = Serial1.read();
  133.         DataStreamIndex++;
  134.       }
  135.     }
  136.  
  137.     // Calculate checksum
  138.     int i=1;
  139.     unsigned int CheckTotal = 0x80 + 0x95 + 0x01;
  140.     while (i < ByteCount)
  141.     {
  142.       CheckTotal += DataBytes[i];
  143.       i++;
  144.     }
  145.     byte CheckSum = 0x200 - CheckTotal;
  146.  
  147.     // Send data over Bluetooth if connected
  148.     // For simplicity, we assume Bluetooth is always connected
  149.     // Send checksum and data bytes
  150.     BluetoothSerial.print("Data received at ");
  151.     BluetoothSerial.print(millis());
  152.     BluetoothSerial.print(" Checksum: ");
  153.     BluetoothSerial.print(CheckSum, HEX);
  154.     BluetoothSerial.print(" Data: ");
  155.     for (int j=1; j<=ByteCount; j++)
  156.     {
  157.       BluetoothSerial.print(DataBytes[j], HEX);
  158.       BluetoothSerial.print(" ");
  159.     }
  160.     BluetoothSerial.println();
  161.  
  162.     // Check decoded output pin
  163.     if (digitalRead(DecodeDataOutputPin) == LOW)
  164.     {
  165.       Serial.print("New Data Stream received at ");
  166.       Serial.print(millis());
  167.       Serial.print(" Calc CHECKSUM: ");
  168.       Serial.print(CheckSum, HEX);
  169.       Serial.print(" Transmitted CHECKSUM: ");
  170.       Serial.print(DataBytes[ByteCount], HEX);
  171.       if (CheckSum == DataBytes[ByteCount])
  172.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  173.       else
  174.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  175.  
  176.       RPM = DataBytes[11] * 25;
  177.       TPS = DataBytes[10] * 0.019608;
  178.       MAF = ((DataBytes[36] * 256) + (DataBytes[37])) * 0.003906;
  179.       BLCELL = DataBytes[21];
  180.       BLM = DataBytes[20];
  181.       INTEGRATOR = DataBytes[22];
  182.       InjPW = ((DataBytes[45] * 256) + (DataBytes[46])) * 0.015259;
  183.       O2mv = DataBytes[17] * 4.44;
  184.       MAT = T_interpolate(DataBytes[30]);
  185.       Runtime = (DataBytes[52] * 256 + DataBytes[53]);
  186.  
  187.       Serial.print("Engine Speed     : ");
  188.       Serial.print(RPM);
  189.       Serial.println(" RPM");
  190.       Serial.print("Throttle Position: ");
  191.       Serial.print(TPS);
  192.       Serial.println(" Volts");
  193.       Serial.print("Mass Air Flow    : ");
  194.       Serial.print(MAF);
  195.       Serial.println(" Grams/Sec");
  196.       Serial.print("Current BLM Cell: ");
  197.       Serial.print(BLCELL);
  198.       Serial.print(" BLM Value: ");
  199.       Serial.print(BLM);
  200.       Serial.print("  Current Fuel Integrator: ");
  201.       Serial.println(INTEGRATOR);
  202.       Serial.print("Injector Pulse   : ");
  203.       Serial.print(InjPW);
  204.       Serial.println(" Milliseconds");
  205.       Serial.print("O2 Sensor Voltage: ");
  206.       Serial.print(O2mv);
  207.       Serial.println(" Millivolts");
  208.       Serial.print("Intake Air Temp  : ");
  209.       Serial.print(MAT);
  210.       Serial.println(" Deg C");
  211.       Serial.print("Engine Run Time  : ");
  212.       Serial.print(Runtime);
  213.       Serial.println(" Seconds");
  214.  
  215.       // Delay to allow user to read data
  216.       unsigned long StartTime = millis();
  217.       while (millis() < StartTime + 3000)
  218.       {
  219.         if (Serial1.available() > 0) ALDLbyte = Serial1.read();
  220.       }
  221.     }
  222.     else if (digitalRead(HexDataOutputPin) == LOW)
  223.     {
  224.       Serial.print("New Data Stream received at ");
  225.       Serial.print(millis());
  226.       Serial.print(" Calc CHECKSUM: ");
  227.       Serial.print(CheckSum, HEX);
  228.       Serial.print(" Transmitted CHECKSUM: ");
  229.       Serial.print(DataBytes[ByteCount], HEX);
  230.       if (CheckSum == DataBytes[ByteCount])
  231.         Serial.println(" Checksum GOOD - Data as follows: ");
  232.       else
  233.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  234.  
  235.       int j=1;
  236.       bytebytecounter = 0;
  237.       while (j<=ByteCount)
  238.       {
  239.         Serial.print(DataBytes[j], HEX);
  240.         Serial.print(" ");
  241.         j++;
  242.         bytebytecounter++;
  243.         if (bytebytecounter >= linecount)
  244.         {
  245.           bytebytecounter=0;
  246.           Serial.println();
  247.         }
  248.       }
  249.       Serial.println();
  250.     }
  251.     else
  252.     {
  253.       // Send raw data stream over serial
  254.       Serial.write(0x80);
  255.       Serial.write(0x95);
  256.       Serial.write(0x01);
  257.       for (int j=1; j<=ByteCount; j++)
  258.       {
  259.         Serial.write(DataBytes[j]);
  260.       }
  261.     }
  262.   }
  263. }
  264.  
  265. // Interpolates temperature
  266. float T_interpolate(byte DS_Temp)
  267. {
  268.   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};
  269.   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};
  270.   float T_Range, T_Diff, TempRange, Temperature;
  271.   int i=0;
  272.   while (i<38)
  273.   {
  274.     if (TempScale[i] > DS_Temp) break;
  275.     i++;
  276.   }
  277.   if (i>0)
  278.   {
  279.     T_Range = TempScale[i] - TempScale[i-1];
  280.     T_Diff = DS_Temp - TempScale[i-1];
  281.     TempRange = TempValue[i] - TempValue[i-1];
  282.     Temperature = TempValue[i-1] + (T_Diff / T_Range) * TempRange;
  283.   }
  284.   else
  285.   {
  286.     Temperature = TempValue[0];
  287.   }
  288.   return Temperature;
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment