Multiple Ultrasonic Sensors with Configurable Parameters
1. Download the MySonarJC library
2. Include it in your Arduino project using:
#include "MySonarJC.h"
MySonarJC myObjSonar;
void begin(int numberOfSensors);
Initializes the sonar system with the specified number of sensors.
void parameters(int index, int rxPin, int txPin, int minDistance = 5, int maxDistance = 300, int maxRetry = 2, int fineTuningPulse = 200, bool debug = false);
Configures individual sensor parameters.
int distance(int index);
Reads the distance from a specified sensor. Returns distance in centimeters.
#include "MySonarJC.h"
// Pin definitions
#define TRIG_PIN1 5
#define ECHO_PIN1 35
#define TRIG_PIN2 4
#define ECHO_PIN2 26
MySonarJC sonar;
void setup() {
Serial.begin(115200);
// Initialize for 2 sensors
sonar.begin(2);
// Configure sensors
sonar.parameters(0, TRIG_PIN1, ECHO_PIN1, 23, 300); // JSN-SR04T
sonar.parameters(1, TRIG_PIN2, ECHO_PIN2); // HC-SR04
}
void loop() {
int distance1 = sonar.distance(0);
int distance2 = sonar.distance(1);
Serial.println("Distance 1: " + String(distance1) + " cm");
Serial.println("Distance 2: " + String(distance2) + " cm");
delay(50);
}
sonar.parameters(0, TRIG_PIN, ECHO_PIN, 5, 300, 2, 200, true);
Enable debug mode to monitor echo duration values in Serial monitor for optimization.