Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Distance Sensors"
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2024-12-19 07:13:30
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* This Arduino project integrates three TFMPlus for */
- /* precise distance tracking by using three Serial */
- /* ports in Arduino Mega, focusing on reliable sensor */
- /* connections and data accuracy. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <TFMPlus.h> // https://github.com/budryerson/TFMini-Plus
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Create instances for three TFMPlus sensors
- TFMPlus tfm1;
- TFMPlus tfm2;
- TFMPlus tfm3;
- // Variables for distance and strength for each sensor
- int distance1 = 0;
- int strength1 = 0;
- boolean receiveComplete1 = false;
- int distance2 = 0;
- int strength2 = 0;
- boolean receiveComplete2 = false;
- int distance3 = 0;
- int strength3 = 0;
- boolean receiveComplete3 = false;
- void getTFminiData(int* distance, int* strength, boolean* complete, int sensorIndex) {
- static char i = 0;
- char j = 0;
- int checksum = 0;
- static int rx[9];
- // Use the appropriate Serial port based on sensorIndex
- if (sensorIndex == 1) {
- if (Serial1.available()) {
- rx[i] = Serial1.read();
- }
- } else if (sensorIndex == 2) {
- if (Serial2.available()) {
- rx[i] = Serial2.read();
- }
- } else if (sensorIndex == 3) {
- if (Serial3.available()) {
- rx[i] = Serial3.read();
- }
- }
- if (rx[0] != 0x59) {
- i = 0;
- } else if (i == 1 && rx[1] != 0x59) {
- i = 0;
- } else if (i == 8) {
- for (j = 0; j < 8; j++) {
- checksum += rx[j];
- }
- if (rx[8] == (checksum % 256)) {
- *distance = rx[2] + rx[3] * 256;
- *strength = rx[4] + rx[5] * 256;
- *complete = true;
- }
- i = 0;
- } else {
- i++;
- }
- }
- void setup(void) {
- // Initialize serial communication for each sensor
- Serial.begin(115200); // Initialize serial communication at 115200 baud
- Serial1.begin(115200); // Initialize second serial port for sensor 1
- Serial2.begin(115200); // Initialize third serial port for sensor 2
- Serial3.begin(115200); // Initialize fourth serial port for sensor 3
- }
- void loop(void) {
- // Get data from each sensor
- getTFminiData(&distance1, &strength1, &receiveComplete1, 1);
- getTFminiData(&distance2, &strength2, &receiveComplete2, 2);
- getTFminiData(&distance3, &strength3, &receiveComplete3, 3);
- // Print data for sensor 1
- if (receiveComplete1) {
- receiveComplete1 = false;
- Serial.print("Sensor 1: ");
- Serial.print(distance1);
- Serial.print("cm\t");
- Serial.print("strength: ");
- Serial.println(strength1);
- }
- // Print data for sensor 2
- if (receiveComplete2) {
- receiveComplete2 = false;
- Serial.print("Sensor 2: ");
- Serial.print(distance2);
- Serial.print("cm\t");
- Serial.print("strength: ");
- Serial.println(strength2);
- }
- // Print data for sensor 3
- if (receiveComplete3) {
- receiveComplete3 = false;
- Serial.print("Sensor 3: ");
- Serial.print(distance3);
- Serial.print("cm\t");
- Serial.print("strength: ");
- Serial.println(strength3);
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement