pleasedontcode

Data Transmission rev_03

Jul 11th, 2025
454
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: Data Transmission
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-07-11 20:56:05
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data from bluetooth to android pc */
  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); // Declare the interpolation function
  32.  
  33. /***** DEFINITION OF Software Serial *****/
  34. const uint8_t hc05_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  35. const uint8_t hc05_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  36. SoftwareSerial hc05_HC05_mySerial(hc05_HC05_mySerial_PIN_SERIAL_RX_A1, hc05_HC05_mySerial_PIN_SERIAL_TX_A0);
  37.  
  38. // Define pins for ALDL activity and data output controls
  39. const int ALDLTestPin = 2; // Example pin, set accordingly
  40. const int DecodeDataOutputPin = 3; // Example pin, set accordingly
  41. const int HexDataOutputPin = 4; // Example pin, set accordingly
  42.  
  43. void setup(void)
  44. {
  45.   // Initialize serial ports
  46.   Serial.begin(115200); // Main serial port for debugging
  47.   Serial1.begin(8192);  // ALDL data at 8192 baud
  48.   hc05_HC05_mySerial.begin(9600); // Bluetooth HC-05 default baud rate
  49.   delay(1500);          // delay for diagnostic print
  50.   Serial.println("Ready for data capture");
  51.  
  52.   // Initialize I/O pins
  53.   pinMode(ALDLTestPin, INPUT); // Listen for ALDL activity
  54.   pinMode(DecodeDataOutputPin, INPUT_PULLUP); // Decoded data output control
  55.   pinMode(HexDataOutputPin, INPUT_PULLUP); // Hex data output control
  56. }
  57.  
  58. void loop() {
  59.   // Wait for silence on ALDL line
  60.   Serial.print("wait for silence ");
  61.   bool SilenceFound = false; // Reset silence flag
  62.   unsigned long StartTime = micros(); // Start timer
  63.   while ((micros() - StartTime) < 15000) { // Wait for 15 ms silence
  64.     if (digitalRead(ALDLTestPin) == 0) {
  65.       StartTime = micros(); // Reset timer if activity detected
  66.     }
  67.   }
  68.   SilenceFound = true; // Silence detected
  69.  
  70.   while (SilenceFound == true) { // Continuous data capture after silence
  71.     // Send Mode 1 command to request data stream
  72.     bool PreambleFound = false;
  73.     uint8_t i = 0;
  74.     unsigned long PreambleTimer;
  75.    
  76.     // Send Mode 1 command
  77.     Serial.print(" M1 cmd ");
  78.     // Assuming M1Cmd is defined elsewhere; define here if needed
  79.     // For example:
  80.     const byte M1Cmd[4] = {0x68, 0x6A, 0xF1, 0x96}; // Example command
  81.     for (i = 0; i < 4; i++) {
  82.       Serial1.write(M1Cmd[i]);
  83.     }
  84.     Serial.println(" Finding Preamble  ");
  85.     // Search for preamble
  86.     i = 0;
  87.     PreambleTimer = millis();
  88.     while (((millis() - PreambleTimer) < 100) && (PreambleFound == false)) {
  89.       if (Serial1.available() > 0) {
  90.         int ALDLbyte = Serial1.read();
  91.         if (ALDLbyte == Preamble[i]) {
  92.           i++;
  93.           if (i > 2) PreambleFound = true;
  94.         } else {
  95.           PreambleFound = false;
  96.           i = 0;
  97.         }
  98.       }
  99.     }
  100.     // Read data stream
  101.     uint8_t DataStreamIndex = 1;
  102.     byte DataBytes[65]; // 64 bytes + 1 for index
  103.     while (DataStreamIndex < 65) {
  104.       if (Serial1.available() > 0) {
  105.         DataBytes[DataStreamIndex] = Serial1.read();
  106.         DataStreamIndex++;
  107.       }
  108.     }
  109.     // Calculate checksum
  110.     uint32_t CheckTotal = 0x80 + 0x95 + 0x01; // Sum of preamble bytes
  111.     for (i = 1; i < 64; i++) {
  112.       CheckTotal += DataBytes[i];
  113.     }
  114.     byte CheckSum = (0x200 - CheckTotal) & 0xFF; // Two's complement
  115.  
  116.     // Send data via Bluetooth
  117.     hc05_HC05_mySerial.write(CheckSum); // Send checksum
  118.     for (int j = 1; j <= 64; j++) {
  119.       hc05_HC05_mySerial.write(DataBytes[j]);
  120.     }
  121.  
  122.     // Output data based on control pins
  123.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  124.       // Decoded data output
  125.       Serial.print("New Data Stream received at ");
  126.       Serial.print(millis());
  127.       Serial.print(" Calc CHECKSUM: ");
  128.       Serial.print(CheckSum, HEX);
  129.       Serial.print(" Transmitted CHECKSUM: ");
  130.       Serial.print(DataBytes[64], HEX);
  131.       if (CheckSum == DataBytes[64]) {
  132.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  133.       } else {
  134.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  135.       }
  136.      
  137.       // Parse and display data
  138.       float RPM = DataBytes[11] * 25;
  139.       float TPS = DataBytes[10] * 0.019608;
  140.       float MAF = ((DataBytes[36] * 256) + DataBytes[37]) * 0.003906;
  141.       int BLCELL = DataBytes[21];
  142.       int BLM = DataBytes[20];
  143.       int INTEGRATOR = DataBytes[22];
  144.       float InjPW = ((DataBytes[45] * 256) + DataBytes[46]) * 0.015259;
  145.       float O2mv = DataBytes[17] * 4.44;
  146.       float MAT = T_interpolate(DataBytes[30]);
  147.       unsigned int Runtime = (DataBytes[52] * 256) + DataBytes[53];
  148.  
  149.       Serial.print("Engine Speed     : ");
  150.       Serial.print(RPM);
  151.       Serial.println(" RPM");
  152.       Serial.print("Throttle Position: ");
  153.       Serial.print(TPS);
  154.       Serial.println(" Volts");
  155.       Serial.print("Mass Air Flow    : ");
  156.       Serial.print(MAF);
  157.       Serial.println(" Grams/Sec");
  158.       Serial.print("Current BLM Cell: ");
  159.       Serial.print(BLCELL);
  160.       Serial.print(" BLM Value: ");
  161.       Serial.print(BLM);
  162.       Serial.print("  Current Fuel Integrator: ");
  163.       Serial.println(INTEGRATOR);
  164.       Serial.print("Injector Pulse   : ");
  165.       Serial.print(InjPW);
  166.       Serial.println(" Milliseconds");
  167.       Serial.print("O2 Sensor Voltage: ");
  168.       Serial.print(O2mv);
  169.       Serial.println(" Millivolts");
  170.       Serial.print("Intake Air Temp  : ");
  171.       Serial.print(MAT);
  172.       Serial.println(" Deg C");
  173.       Serial.print("Engine Run Time  : ");
  174.       Serial.print(Runtime);
  175.       Serial.println(" Seconds");
  176.      
  177.       // Delay to allow user to read data
  178.       unsigned long StartTimeDelay = millis();
  179.       while (millis() < StartTimeDelay + 3000) {
  180.         if (Serial1.available() > 0) ALDLbyte = Serial1.read(); // Flush buffer
  181.       }
  182.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  183.       // Hex data output
  184.       Serial.print("New Data Stream received at ");
  185.       Serial.print(millis());
  186.       Serial.print(" Calc CHECKSUM: ");
  187.       Serial.print(CheckSum, HEX);
  188.       Serial.print(" Transmitted CHECKSUM: ");
  189.       Serial.print(DataBytes[64], HEX);
  190.       if (CheckSum == DataBytes[64]) {
  191.         Serial.println(" Checksum GOOD - Data as follows: ");
  192.       } else {
  193.         Serial.println(" Checksum *** ERROR *** -  Data as follows: ");
  194.       }
  195.      
  196.       // Print hex data
  197.       int j = 1;
  198.       int bytecounter = 0;
  199.       const int linecount = 64;
  200.       while (j <= 64) {
  201.         Serial.print(DataBytes[j], HEX);
  202.         Serial.print(" ");
  203.         bytecounter++;
  204.         if (bytecounter >= linecount) {
  205.           bytecounter = 0;
  206.           Serial.println();
  207.         }
  208.         j++;
  209.       }
  210.       Serial.println();
  211.     } else {
  212.       // Raw binary data stream
  213.       Serial.write(0x80);
  214.       Serial.write(0x95);
  215.       Serial.write(0x01);
  216.       for (int j = 1; j <= 64; j++) {
  217.         Serial.write(DataBytes[j]);
  218.       }
  219.     }
  220.   }
  221. }
  222.  
  223. // Interpolation function for temperature
  224. float T_interpolate(byte DS_Temp) {
  225.   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};
  226.   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};
  227.   float T_Range, T_Diff, TempRange, Temperature;
  228.   int i = 0;
  229.   while (i < 38) {
  230.     if (TempScale[i] > DS_Temp) break;
  231.     i++;
  232.   }
  233.   if (i > 0) {
  234.     T_Range = TempScale[i] - TempScale[i - 1];
  235.     T_Diff = DS_Temp - TempScale[i - 1];
  236.     TempRange = TempValue[i] - TempValue[i - 1];
  237.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  238.   } else {
  239.     Temperature = TempValue[0];
  240.   }
  241.   return Temperature;
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment