pleasedontcode

Vehicle Data rev_21

Jul 11th, 2025
481
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 22:33:52
  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); // Added prototype for T_interpolate function
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. const uint8_t HC05_Bluetooth_TX_PIN = A0; // Bluetooth TX pin connected to Arduino A0
  35. const uint8_t HC05_Bluetooth_RX_PIN = A1; // Bluetooth RX pin connected to Arduino A1
  36. SoftwareSerial HC05(BT_TX_PIN, BT_RX_PIN); // Instantiate Bluetooth serial
  37.  
  38. // Define other variables used in the code
  39. bool SilenceFound = false;
  40. unsigned long StartTime = 0;
  41. bool PreambleFound = false;
  42. unsigned long PreambleTimer = 0;
  43. byte ALDLbyte = 0;
  44. const int ByteCount = 64; // Total bytes in data packet
  45. int DataStreamIndex = 0;
  46. unsigned int CheckTotal = 0;
  47. byte DataBytes[65]; // Data buffer
  48. byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble sequence
  49. int i = 0;
  50. int CheckSum = 0;
  51. float RPM, TPS, MAF, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime;
  52. const int HexDataOutputPin = 4; // Pin for hex output flag
  53. int linecount = 32; // For hex output formatting
  54. int bytecounter = 0;
  55. int linecount = 32; // For hex output formatting
  56. byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, replace with actual command if needed
  57.  
  58. void setup(void)
  59. {
  60.     // Initialize Bluetooth serial
  61.     HC05.begin(9600);
  62.     // Initialize serial communication for debugging
  63.     Serial.begin(115200);
  64.     // Set pin modes
  65.     pinMode(4, INPUT_PULLUP); // Assuming active low for hex output flag
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // Wait for silence period on the ALDL
  71.     Serial.print("wait for silence ");
  72.     SilenceFound = false; // Reset silence flag
  73.     StartTime= micros(); // First look for an active signal or a timeout - initialize timer
  74.     // Should exit this loop on the start of a bit
  75.     while ((micros() - StartTime) < 15000) // Wait for a 15 ms silent period
  76.     {
  77.         if (digitalRead(4) == 0) // Any line activity resets the start time
  78.         {
  79.             StartTime= micros(); // Timing starts over
  80.         }
  81.     }
  82.     SilenceFound = true; // Set the silence flag on exit
  83.  
  84.     while (SilenceFound == true) // While silence found flag is set, continuously request and transmit Mode 1 data
  85.     {
  86.         PreambleFound = false; // Reset preamble found flag
  87.  
  88.         while (PreambleFound == false) // Look for preamble
  89.         {
  90.             Serial.print(" M1 cmd ");
  91.             i=0; // use bytecounter to send the outgoing Mode1CMD sequence
  92.             while (i<4)
  93.             {
  94.                 Serial1.write(M1Cmd[i]); // Send command bytes
  95.                 i++;
  96.             }
  97.             Serial.println(" Finding Preamble ");
  98.             i=0; // Reset byte counter
  99.             PreambleTimer = millis(); // Initialize timer
  100.             while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false))
  101.             {
  102.                 if (Serial1.available() > 0)
  103.                 {
  104.                     ALDLbyte = Serial1.read();
  105.                     if (ALDLbyte == Preamble[i])
  106.                     {
  107.                         i++;
  108.                         if (i > 2) PreambleFound = true;
  109.                     }
  110.                     else
  111.                     {
  112.                         PreambleFound = false;
  113.                         i=0;
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.  
  119.         // Read data packet
  120.         DataStreamIndex = 1;
  121.         while (DataStreamIndex < 65)
  122.         {
  123.             if (Serial1.available() > 0)
  124.             {
  125.                 DataBytes[DataStreamIndex] = Serial1.read();
  126.                 DataStreamIndex++;
  127.             }
  128.         }
  129.  
  130.         // Calculate checksum
  131.         i=1;
  132.         CheckTotal = 0x80+0x95+0x01; // Sum preamble bytes
  133.         while (i < ByteCount)
  134.         {
  135.             CheckTotal += DataBytes[i];
  136.             i++;
  137.         }
  138.         CheckSum = 0x200 - CheckTotal;
  139.  
  140.         // Verify checksum
  141.         if (CheckSum == DataBytes[ByteCount])
  142.         {
  143.             // Prepare data string to send via Bluetooth
  144.             String dataString = "";
  145.             dataString += "RPM: ";
  146.             dataString += String((int)(DataBytes[11] * 25));
  147.             dataString += ", TPS: ";
  148.             dataString += String(DataBytes[10] * 0.019608);
  149.             dataString += ", MAF: ";
  150.             dataString += String(((DataBytes[36] * 256) + DataBytes[37]) * 0.003906);
  151.             dataString += ", BLM: ";
  152.             dataString += String(DataBytes[20]);
  153.             dataString += ", BLCELL: ";
  154.             dataString += String(DataBytes[21]);
  155.             dataString += ", INTEGRATOR: ";
  156.             dataString += String(DataBytes[22]);
  157.             dataString += ", InjPW: ";
  158.             dataString += String(((DataBytes[45] * 256) + DataBytes[46]) * 0.015259);
  159.             dataString += ", O2mv: ";
  160.             dataString += String(DataBytes[17] * 4.44);
  161.             dataString += ", MAT: ";
  162.             dataString += String(T_interpolate(DataBytes[30]));
  163.             dataString += ", Runtime: ";
  164.             dataString += String(DataBytes[52] * 256 + DataBytes[53]);
  165.  
  166.             // Send data string over Bluetooth
  167.             HC05.println(dataString);
  168.         }
  169.         // Optional: add delay or further processing
  170.         delay(1000); // Send data every 1 second
  171.     }
  172. }
  173.  
  174. // T_interpolate function remains unchanged from the provided user code
  175. float T_interpolate(byte DS_Temp)
  176. {
  177.     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};
  178.     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};
  179.     float T_Range;
  180.     float T_Diff;
  181.     float TempRange;
  182.     float Temperature;
  183.     int i = 0;
  184.     while (i<38)
  185.     {
  186.         if  (TempScale[i]> DS_Temp) break;
  187.         i++;
  188.     }
  189.     if (i>0)
  190.     {
  191.         T_Range = TempScale[i] - TempScale[i-1];
  192.         T_Diff =  DS_Temp - TempScale[i-1];
  193.         TempRange = TempValue[i] - TempValue[i-1];
  194.         Temperature =  TempValue[i-1] + (T_Diff/T_Range)*TempRange;
  195.     }
  196.     else Temperature = TempValue[0];
  197.  
  198.     return Temperature;
  199. }
  200.  
Advertisement
Add Comment
Please, Sign In to add comment