Advertisement
pleasedontcode

"Distance Sensors" rev_03

Dec 19th, 2024
34
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: "Distance Sensors"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-12-19 07:13:30
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* This Arduino project integrates three TFMPlus for */
  21.     /* precise distance tracking by using three Serial */
  22.     /* ports in Arduino Mega, focusing on reliable sensor */
  23.     /* connections and data accuracy. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <TFMPlus.h>    // https://github.com/budryerson/TFMini-Plus
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. // Create instances for three TFMPlus sensors
  37. TFMPlus tfm1;
  38. TFMPlus tfm2;
  39. TFMPlus tfm3;
  40.  
  41. // Variables for distance and strength for each sensor
  42. int distance1 = 0;
  43. int strength1 = 0;
  44. boolean receiveComplete1 = false;
  45.  
  46. int distance2 = 0;
  47. int strength2 = 0;
  48. boolean receiveComplete2 = false;
  49.  
  50. int distance3 = 0;
  51. int strength3 = 0;
  52. boolean receiveComplete3 = false;
  53.  
  54. void getTFminiData(int* distance, int* strength, boolean* complete, int sensorIndex) {
  55.   static char i = 0;
  56.   char j = 0;
  57.   int checksum = 0;
  58.   static int rx[9];
  59.  
  60.   // Use the appropriate Serial port based on sensorIndex
  61.   if (sensorIndex == 1) {
  62.     if (Serial1.available()) {  
  63.       rx[i] = Serial1.read();
  64.     }
  65.   } else if (sensorIndex == 2) {
  66.     if (Serial2.available()) {  
  67.       rx[i] = Serial2.read();
  68.     }
  69.   } else if (sensorIndex == 3) {
  70.     if (Serial3.available()) {  
  71.       rx[i] = Serial3.read();
  72.     }
  73.   }
  74.  
  75.   if (rx[0] != 0x59) {
  76.     i = 0;
  77.   } else if (i == 1 && rx[1] != 0x59) {
  78.     i = 0;
  79.   } else if (i == 8) {
  80.     for (j = 0; j < 8; j++) {
  81.       checksum += rx[j];
  82.     }
  83.     if (rx[8] == (checksum % 256)) {
  84.       *distance = rx[2] + rx[3] * 256;
  85.       *strength = rx[4] + rx[5] * 256;
  86.       *complete = true;
  87.     }
  88.     i = 0;
  89.   } else {
  90.     i++;
  91.   }
  92. }
  93.  
  94. void setup(void) {
  95.   // Initialize serial communication for each sensor
  96.   Serial.begin(115200); // Initialize serial communication at 115200 baud
  97.   Serial1.begin(115200); // Initialize second serial port for sensor 1
  98.   Serial2.begin(115200); // Initialize third serial port for sensor 2
  99.   Serial3.begin(115200); // Initialize fourth serial port for sensor 3
  100. }
  101.  
  102. void loop(void) {
  103.   // Get data from each sensor
  104.   getTFminiData(&distance1, &strength1, &receiveComplete1, 1);
  105.   getTFminiData(&distance2, &strength2, &receiveComplete2, 2);
  106.   getTFminiData(&distance3, &strength3, &receiveComplete3, 3);
  107.  
  108.   // Print data for sensor 1
  109.   if (receiveComplete1) {
  110.     receiveComplete1 = false;
  111.     Serial.print("Sensor 1: ");
  112.     Serial.print(distance1);
  113.     Serial.print("cm\t");
  114.     Serial.print("strength: ");
  115.     Serial.println(strength1);
  116.   }
  117.  
  118.   // Print data for sensor 2
  119.   if (receiveComplete2) {
  120.     receiveComplete2 = false;
  121.     Serial.print("Sensor 2: ");
  122.     Serial.print(distance2);
  123.     Serial.print("cm\t");
  124.     Serial.print("strength: ");
  125.     Serial.println(strength2);
  126.   }
  127.  
  128.   // Print data for sensor 3
  129.   if (receiveComplete3) {
  130.     receiveComplete3 = false;
  131.     Serial.print("Sensor 3: ");
  132.     Serial.print(distance3);
  133.     Serial.print("cm\t");
  134.     Serial.print("strength: ");
  135.     Serial.println(strength3);
  136.   }
  137. }
  138.  
  139. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement