pleasedontcode

Data Transmission rev_12

Jul 11th, 2025
428
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 21:50:49
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* send data bluetooth to android */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. // No external libraries used
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31. float T_interpolate(byte DS_Temp); // Prototype for the interpolation function
  32.  
  33. // Define pins
  34. const int ALDLTestPin = 4;
  35. const int DecodeDataOutputPin = 2; // Example pin, set accordingly
  36. const int HexDataOutputPin = 3;    // Example pin, set accordingly
  37.  
  38. // Variables
  39. bool SilenceFound = false;
  40. unsigned long StartTime = 0;
  41. unsigned long PreambleTimer = 0;
  42. byte ALDLbyte = 0;
  43. int DataStreamIndex = 1;
  44. const int ByteCount = 64;
  45. byte DataBytes[65];
  46. const byte Preamble[3] = {0x80, 0x95, 0x01};
  47. bool PreambleFound = false;
  48. int i = 0;
  49. unsigned int CheckTotal = 0;
  50. byte CheckSum = 0;
  51. const int linecount = 32;
  52. int bytecounter = 0;
  53. int LineCount = 32; // For hex output formatting
  54.  
  55. // Mode 1 command sequence
  56. byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, set accordingly
  57.  
  58. // Bluetooth Serial
  59. #include <HardwareSerial.h>
  60. HardwareSerial BluetoothSerial(2); // Use UART2 for Bluetooth
  61.  
  62. // Additional variables for data
  63. unsigned long PreambleTimer;
  64. float RPM, TPS, MAF, BLCELL, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime;
  65.  
  66. void setup() {
  67.   // **** I/O configuration and setup first
  68.   pinMode(ALDLTestPin, INPUT);                                               // define D4 as an input pin to listen for the 160 baud input data
  69.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);                                // User convenience pin.  Grounding this pin will send Decoded Data to the Serial Port
  70.   pinMode(HexDataOutputPin, INPUT_PULLUP);                                   // User convenience pin.  Grounding this pin will send HEX Data to the Serial Port
  71.  
  72.   // Initialize serial ports
  73.   Serial.begin(115200);                                                     // Open serial monitoring port
  74.   Serial1.begin(8192);                                                      // Serial port for ALDL data
  75.   BluetoothSerial.begin(9600);                                                // Bluetooth serial port at 9600 baud
  76.  
  77.   delay(1500);                                                              // delay for diagnostic print
  78.   Serial.println("Ready for data capture");
  79.   // Initialize variables
  80.   i = 0; // Reset the preamble index flag
  81. }
  82.  
  83. void loop() {
  84.   // Wait for silence period on the ALDL
  85.   Serial.print("wait for silence ");
  86.   SilenceFound = false; // Reset silence flag
  87.   StartTime = micros(); // First look for an active signal or a timeout - initialize timer
  88.  
  89.   // Wait for a 15 ms silent period
  90.   while ((micros() - StartTime) < 15000) {
  91.     if (digitalRead(ALDLTestPin) == 0) { // Any line activity resets the start time
  92.       StartTime = micros(); // Timing starts over
  93.     }
  94.   }
  95.   SilenceFound = true; // Set the silence flag on exit
  96.  
  97.   while (SilenceFound == true) { // While silence found flag is set, continuously request and transmit Mode 1 data
  98.     PreambleFound = false; // Reset preamble found flag
  99.  
  100.     while (PreambleFound == false) { // First look at data until the preamble has been found
  101.       Serial.print(" M1 cmd ");
  102.       i = 0; // use bytecounter to send the outgoing Mode1CMD sequence
  103.       while (i < 4) {
  104.         Serial1.write(M1Cmd[i]); // send 1 byte of the command sequence
  105.         i++;
  106.       }
  107.  
  108.       Serial.println(" Finding Preamble  ");
  109.       i = 0; // reset byte counter
  110.       PreambleTimer = millis(); // initialize timer to detect timeout
  111.  
  112.       while ((millis() - PreambleTimer) < 100 && PreambleFound == false) {
  113.         if (Serial1.available() > 0) {
  114.           ALDLbyte = Serial1.read();
  115.           if (ALDLbyte == Preamble[i]) {
  116.             i++;
  117.             if (i > 2) PreambleFound = true; // Preamble found
  118.           } else {
  119.             PreambleFound = false;
  120.             i = 0;
  121.           }
  122.         }
  123.       }
  124.     }
  125.  
  126.     // Read complete data packet
  127.     DataStreamIndex = 1;
  128.     while (DataStreamIndex < 65) {
  129.       if (Serial1.available() > 0) {
  130.         DataBytes[DataStreamIndex] = Serial1.read();
  131.         DataStreamIndex++;
  132.       }
  133.     }
  134.  
  135.     // Checksum calculation
  136.     i = 1;
  137.     CheckTotal = 0x80 + 0x95 + 0x01;
  138.     while (i < ByteCount) {
  139.       CheckTotal += DataBytes[i];
  140.       i++;
  141.     }
  142.     CheckSum = 0x200 - CheckTotal;
  143.  
  144.     // Send data over Bluetooth
  145.     if (digitalRead(DecodeDataOutputPin) == LOW) {
  146.       // Send decoded data
  147.       Serial.print("New Data Stream received at ");
  148.       Serial.print(millis());
  149.       Serial.print(" Calc CHECKSUM: ");
  150.       Serial.print(CheckSum, HEX);
  151.       Serial.print(" Transmitted CHECKSUM: ");
  152.       Serial.print(DataBytes[ByteCount], HEX);
  153.       if (CheckSum == DataBytes[ByteCount]) {
  154.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  155.       } else {
  156.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");
  157.       }
  158.       // Send over Bluetooth
  159.       BluetoothSerial.print("Data at ");
  160.       BluetoothSerial.print(millis());
  161.       BluetoothSerial.print(" Checksum: ");
  162.       BluetoothSerial.print(CheckSum, HEX);
  163.       BluetoothSerial.print(" Data: ");
  164.       for (int j = 1; j <= ByteCount; j++) {
  165.         BluetoothSerial.print(DataBytes[j], HEX);
  166.         BluetoothSerial.print(" ");
  167.       }
  168.       BluetoothSerial.println();
  169.     } else if (digitalRead(HexDataOutputPin) == LOW) {
  170.       // Send hex data
  171.       Serial.print("New Data Stream received at ");
  172.       Serial.print(millis());
  173.       Serial.print(" Calc CHECKSUM: ");
  174.       Serial.print(CheckSum, HEX);
  175.       Serial.print(" Transmitted CHECKSUM: ");
  176.       Serial.print(DataBytes[ByteCount], HEX);
  177.       if (CheckSum == DataBytes[ByteCount]) {
  178.         Serial.println(" Checksum GOOD - Data as follows: ");
  179.       } else {
  180.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  181.       }
  182.       // Send over Bluetooth
  183.       BluetoothSerial.print("Hex Data at ");
  184.       BluetoothSerial.print(millis());
  185.       BluetoothSerial.print(" Checksum: ");
  186.       BluetoothSerial.print(CheckSum, HEX);
  187.       BluetoothSerial.print(" Data: ");
  188.       for (int j = 1; j <= ByteCount; j++) {
  189.         BluetoothSerial.print(DataBytes[j], HEX);
  190.         BluetoothSerial.print(" ");
  191.       }
  192.       BluetoothSerial.println();
  193.     } else {
  194.       // Send raw binary data over Bluetooth
  195.       Serial.write(0x80);
  196.       Serial.write(0x95);
  197.       Serial.write(0x01);
  198.       for (int j = 1; j <= ByteCount; j++) {
  199.         Serial.write(DataBytes[j]);
  200.       }
  201.       // Send over Bluetooth
  202.       BluetoothSerial.write(0x80);
  203.       BluetoothSerial.write(0x95);
  204.       BluetoothSerial.write(0x01);
  205.       for (int j = 1; j <= ByteCount; j++) {
  206.         BluetoothSerial.write(DataBytes[j]);
  207.       }
  208.     }
  209.   }
  210. }
  211.  
  212. // Implementation of the T_interpolate function
  213. float T_interpolate(byte DS_Temp) {
  214.   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};
  215.   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};
  216.   float T_Range;
  217.   float T_Diff;
  218.   float TempRange;
  219.   float Temperature;
  220.   int i = 0;
  221.   while (i < 38) {
  222.     if (TempScale[i] > DS_Temp) break;
  223.     i++;
  224.   }
  225.   if (i > 0) {
  226.     T_Range = TempScale[i] - TempScale[i - 1];
  227.     T_Diff = DS_Temp - TempScale[i - 1];
  228.     TempRange = TempValue[i] - TempValue[i - 1];
  229.     Temperature = TempValue[i - 1] + (T_Diff / T_Range) * TempRange;
  230.   } else {
  231.     Temperature = TempValue[0];
  232.   }
  233.   return Temperature;
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment