I'm using arduino IDE to program make a simple obstacle avoidance robot with only Ultrasonic Sensor mounted on a servo. I'm using the ESP32Servo.h library. If I load this function to esp32, it works fine and shows and scans the distance.
void scanDistance() {
int servoAngle;
const int angleLeft = 180;
const int angleRight = 0;
const int angleCenter = 90;
// Set servo to center position and measure distance
servo.write(angleCenter);
delay(500); // Allow time for servo to stabilize
servoAngle = angleCenter;
if (servoAngle == angleCenter) {
distanceCenter = getDistance();
Serial.print("Center distance: ");
Serial.println(distanceCenter);
}
// If center distance is less than 20 cm, check left and right
if (distanceCenter < 20) {
// Look left
servo.write(angleLeft);
servoAngle = angleLeft;
delay(500); // Allow time for servo to stabilize
distanceLeft = getDistance();
Serial.print("Left distance: ");
Serial.println(distanceLeft);
// Look right
servo.write(angleRight);
servoAngle = angleRight;
delay(500); // Allow time for servo to stabilize
distanceRight = getDistance();
Serial.print("Right distance: ");
Serial.println(distanceRight);
// Return to center
servo.write(angleCenter);
servoAngle = angleCenter;
delay(500); // Allow time for servo to stabilize
}
}
but the moment I edit this and insert any functions related to moving the other motors the servo goes haywire and turns to the right. I'm using a simpe Micro servo sg90. and i know this is not a problem of current or voltage since i did not connect the motor drivers yet and is just checking the serial for values. Please Help asap. Will prefer a simple solution since I'm a beginner. Not a complete noob but not so profficient either. Need ASAP. Thanks in advance.
there's really nothing else in the full code since I only have that function in the loop(); that one works fine but if I use this:
void navigationProtocol() {
int servoAngle;
const int angleLeft = 180;
const int angleRight = 0;
const int angleCenter = 90;
// Set servo to center position and measure distance
servo.write(angleCenter);
delay(500); // Allow time for servo to stabilize
servoAngle = angleCenter;
if (servoAngle == angleCenter) {
distanceCenter = getDistance();
Serial.print("Center distance: ");
Serial.println(distanceCenter);
}
// If center distance is less than 20 cm, check left and right
if (distanceCenter < 20) {
// Look left
servo.write(angleLeft);
servoAngle = angleLeft;
delay(500); // Allow time for servo to stabilize
distanceLeft = getDistance();
Serial.print("Left distance: ");
Serial.println(distanceLeft);
// Look right
servo.write(angleRight);
servoAngle = angleRight;
delay(500); // Allow time for servo to stabilize
distanceRight = getDistance();
Serial.print("Right distance: ");
Serial.println(distanceRight);
if (distanceLeft < distanceRight) {
goRight();
delay(500); // Allow time for turning
} else if (distanceLeft > distanceRight) {
goLeft();
delay(500); // Allow time for turning
}
// Return to center
servo.write(angleCenter);
servoAngle = angleCenter;
delay(500); // Allow time for servo to stabilize
}
// Return to center
servo.write(angleCenter);
servoAngle = angleCenter;
delay(500); // Allow time for servo to stabilize
}else {
moveForward();
}
}
which, as you can see, only added the movement functions. and there's really nothing wrong in those. just the typical motor control codes like enable pin and motor pin controls. the moment I run this code the servo goes to the right and it starts there. it's not even subtituting 0 degrees as 90 and 90 into 180 since it only moves about 20-30 degrees from 0.
If your servo motor has a brake system, make sure it's turned on.
Check the motor speed circuit gain
See if the motor speed circuit gain setting is too high, and adjust it if needed.
Check the serial monitor
If you can't get the motor to move, check the serial monitor to see if you're getting a value back.
Install the ESP32Servo Library
You can install this library from the Arduino IDE's Library Manager. Once installed, you can open the File/Examples menu and scroll down to the ESP32Servo section.
Allow all digital pins to be used as PWM pins from frequencies and timers on esp 32
i hope this help and if i am wrong plz correct if you can