I want to stop a servo motor which turns continuously until an obstacle is detected by ultrasonic sensor. For instance, I want the servo motor to stop when the obstacle is within 5 cm of the ultrasonic sensor. If there is no obstacle, servo motor should turn without stopping.
However, after the obstacle is removed, my servo motor starts rotating from a different angle, not where it stops. I added servo motor rotation part of Arduino code.`
void loop() {
for (int i=0; i<=180; i++) {
distance = calculateDistance();
if (distance <= 10){
moveStop();
} else {
moveForward();
myServo.write(i);
delay(5);
}
}
for (int i=180; i>0; i--) {
distance = calculateDistance();
if (distance <= 10) {
moveStop();
} else {
moveForward();
myServo.write(i);
delay(5);
}
the problem is that i gets continously counted even when the object is in the way. cant recreate your setup exactly but I hope this helps you out
int y = 0
void loop() {
for(int i=0;i<=180;i++){
distance = calculateDistance();
if (distance <= 10){
moveStop();
y++; //"counts the time" the servo is blocked by object
}
else{
moveForward();
myServo.write(i-y); //and subtracts that time from i
delay(5);
}
}
for(int i=180;i>0;i--){
distance = calculateDistance();
if (distance <= 10){
moveStop();
y++;
}
else{
moveForward();
myServo.write(i+y); //same but in reverse
delay(5);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.