pleasedontcode

Data Transmission rev_09

Jul 11th, 2025
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 10.37 KB | None | 0 0
  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:28:02
  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.  
  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. // Define pins
  33. const int ALDLTestPin = 4;
  34. const int DecodeDataOutputPin = 2; // Example pin, adjust as needed
  35. const int HexDataOutputPin = 3;    // Example pin, adjust as needed
  36.  
  37. // Define command array and other variables
  38. byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, adjust as needed
  39. const int ByteCount = 64;
  40. byte DataBytes[65];
  41. int DataStreamIndex = 0;
  42. unsigned long PreambleTimer = 0;
  43. bool PreambleFound = false;
  44. bool SilenceFound = false;
  45. unsigned long StartTime = 0;
  46. int i = 0;
  47. float RPM, TPS, MAF, BLCELL, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime;
  48. const int linecount = 32;
  49. int bytecounter = 0;
  50. const byte Preamble[3] = {0x80, 0x95, 0x01};
  51. const int linecount = 32;
  52. unsigned int CheckTotal = 0;
  53. byte ALDLbyte = 0;
  54. float CheckSum = 0;
  55.  
  56. // Bluetooth serial port
  57. // Adjust the serial port as per your hardware connection
  58. // For example, Serial2 for Mega
  59. // Initialize in setup()
  60.  
  61. void setup()
  62. {
  63.   // **** I/O configuration and setup first
  64.   pinMode(ALDLTestPin, INPUT);                                              // define D4 as an input pin to listen for the 160 baud input data
  65.   pinMode(DecodeDataOutputPin, INPUT_PULLUP);                                // User convenience pin.  Grounding this pin will send Decoded Data to the Serial Port
  66.   pinMode(HexDataOutputPin, INPUT_PULLUP);                                   // User convenience pin.  Grounding this pin will send HEX Data to the Serial Port
  67.  
  68.   // Initialize serial ports
  69.   Serial.begin(115200);                                                     // Open serial monitoring port
  70.   Serial1.begin(8192);                                                      // Test the capability of esp 8222 to run at 8192 baud directly
  71.   Serial2.begin(9600); // Initialize Bluetooth serial port at 9600 baud (adjust as needed)
  72.  
  73.   delay(1500);                                                              // delay for diagnostic print
  74.   Serial.println("Ready for data capture");
  75.  
  76.   // Initialize variables
  77.   i=0;                                                                      // Reset the preamble index flag
  78. }
  79.  
  80. void loop() {
  81.   // Send data from Bluetooth to Android
  82.   if (Serial2.available() > 0) {
  83.     int btData = Serial2.read();
  84.     Serial.print("Bluetooth data: ");
  85.     Serial.println(btData, HEX);
  86.   }
  87.  
  88.   // Wait for silence period on the ALDL
  89.   Serial.print("wait for silence ");
  90.   SilenceFound = false;                                                     // Reset silence flag
  91.   StartTime= micros();                                                      // First look for an active signal or a timeout - initialize timer
  92.   while ((micros() - StartTime)< 15000) {                                     // Wait for a 15 ms silent period
  93.     if (digitalRead(ALDLTestPin)== 0) {                                         // Any line activity resets the start time
  94.       StartTime= micros();                                                   // Timing starts over
  95.     }
  96.   }                                                            
  97.   SilenceFound = true;                                                      // Set the silence flag on exit
  98.  
  99.   while (SilenceFound == true) {                                              // While silence found flag is set, continuously request and transmit Mode 1 data
  100.     PreambleFound = false;                                                    // Reset preamble found flag                                                            
  101.     while (PreambleFound == false) {                                           // First look at data until the preamble has been found will read data forever until a preamble is read
  102.       Serial.print(" M1 cmd ");
  103.       i=0;                                                                  // use bytecounter to send the outgoing Mode1CMD sequence
  104.       while (i<4) {
  105.         Serial1.write(M1Cmd[i]);                                            // sends 1 byte of the command sequence
  106.         i++;
  107.       }  
  108.       Serial.println(" Finding Preamble  ");  
  109.       i=0;                                                                  // Then reset byte counter and scan incoming data for the preamble
  110.       PreambleTimer = millis();                                             // Initialize timer to detect timeout
  111.       while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false)) { // First look at data for 100 ms or until the preamble has been found
  112.         if (Serial1.available() > 0) {                                      // Check for available data on the serial port
  113.           ALDLbyte = Serial1.read();                                      // Read it and look for the preamble
  114.           if ( ALDLbyte == Preamble[i]) {                                   // Look for matching byte of data preamble in serial stream
  115.             i++;                                                        // Increment the preamble index and look for the next character
  116.             if (i>2) PreambleFound = true;                               // Once three characters are found, the preamble has been found, time to read in the data
  117.           } else {                                                            // If there isn't match, start over looking from the beginning
  118.             PreambleFound = false;                                      // Reset preamble found flag
  119.             i=0;                                                        // Reset the preamble index flag          
  120.           } // end if
  121.         } // end if
  122.       } // end while
  123.     } // end while
  124.  
  125.     // Read complete data packet
  126.     DataStreamIndex = 1;                                                  // Once a valid preamble has been found set the data stream index to the first byte
  127.     while (DataStreamIndex < 65) {                                          // and read data up to byte #63 + 1 byte Checksum = 64 Bytes total
  128.       if (Serial1.available() > 0) {                                    // Check for available data on the serial port
  129.         DataBytes[DataStreamIndex] = Serial1.read();                // And read bytes into the array as they come
  130.         DataStreamIndex++;                                          // update the index
  131.       } // end if
  132.     } // end while
  133.  
  134.     // Checksum calculation
  135.     i=1;                                                                  // use bytecounter as an index
  136.     CheckTotal = 0x80+0x95+0x01;                                          // sum preamble bytes
  137.     while (i< (ByteCount )) {                                               // Add received bytes to the checksum
  138.       CheckTotal = CheckTotal + DataBytes[i];                           // add a byte
  139.       i++;
  140.     }  
  141.     CheckSum = 0x200 - CheckTotal;                                        // Two's complement the checksum
  142.  
  143.     // Send data to serial port based on output mode
  144.     if (digitalRead(DecodeDataOutputPin) == LOW) {                          // Check decoded output bit
  145.       Serial.print("New Data Stream received at ");
  146.       Serial.print(millis());
  147.       Serial.print(" Calc CHECKSUM: ");
  148.       Serial.print(CheckSum, HEX);
  149.       Serial.print(" Transmitted CHECKSUM: ");
  150.       Serial.print(DataBytes[ByteCount], HEX);
  151.       if (CheckSum == DataBytes[ByteCount]) {
  152.         Serial.println(" Checksum GOOD - Decoded Data as follows:   (Page 1) ");
  153.       } else {
  154.         Serial.println(" Checksum *** ERROR *** -  Decoded Data as follows:   (Page 1) ");                  
  155.       }
  156.       // ... (rest of data processing and printing code)
  157.     } else if (digitalRead(HexDataOutputPin) == LOW) {                        // Hex output mode
  158.       Serial.print("New Data Stream received at ");
  159.       Serial.print(millis());
  160.       Serial.print(" Calc CHECKSUM: ");
  161.       Serial.print(CheckSum, HEX);
  162.       Serial.print(" Transmitted CHECKSUM: ");
  163.       Serial.print(DataBytes[ByteCount], HEX);
  164.       if (CheckSum == DataBytes[ByteCount]) {
  165.         Serial.println(" Checksum GOOD - Data as follows: ");      
  166.       } else {
  167.         Serial.println("Checksum *** ERROR *** -  Data as follows: ");
  168.       }
  169.       int j=1;
  170.       bytecounter = 0;
  171.       while (j<ByteCount +1) {
  172.         Serial.print(DataBytes[j], HEX);
  173.         j++;
  174.         bytecounter++;
  175.         if (bytecounter >= linecount) {
  176.           bytecounter = 0;
  177.           Serial.println("");
  178.         } else {
  179.           Serial.print(" ");
  180.         }
  181.       }
  182.       Serial.println("");
  183.     } else { // Raw binary stream
  184.       Serial.write(0x80);
  185.       Serial.write(0x95);
  186.       Serial.write(0x01);
  187.       for (int j=1; j<ByteCount +1; j++) {
  188.         Serial.write(DataBytes[j]);
  189.       }
  190.     }
  191.  
  192.     // Send data over Bluetooth to Android
  193.     for (int j=1; j<ByteCount +1; j++) {
  194.       Serial2.write(DataBytes[j]);
  195.     }
  196.   } // end while (SilenceFound)
  197.  
  198.   // Additional code for processing and other functions...
  199. }
  200.  
  201. // Implementation of the interpolation function
  202. float T_interpolate(byte DS_Temp) {
  203.   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};
  204.   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};
  205.   float T_Range, T_Diff, TempRange, Temperature;
  206.   int i=0;
  207.   while (i<38) {
  208.     if (TempScale[i]> DS_Temp) break;
  209.     i++;
  210.   }
  211.   if (i>0) {
  212.     T_Range = TempScale[i] - TempScale[i-1];
  213.     T_Diff = DS_Temp - TempScale[i-1];
  214.     TempRange = TempValue[i] - TempValue[i-1];
  215.     Temperature = TempValue[i-1] + (T_Diff/T_Range)*TempRange;
  216.   } else {
  217.     Temperature = TempValue[0];
  218.   }
  219.   return Temperature;
  220. }
  221.  
Advertisement
Add Comment
Please, Sign In to add comment