My objective: is to have a robot follow me. (I'm using an arduiono bot for this)
So I started off what some code that wasn't so dynamic. Essentially I had no issues with the robot following me (holding a cardboard because these robots need a good flat surface to see) when I was right in front of it, but when it turned it just simply scanned while turning to the right till it found presence of something and just followed it. As you might tell, this is very inefficient and doesn't accomplish a proper robot follow.
In order to make it more dynamic I was suggested to do this:
- basically have the robot scan the right and left edges of the cardboard every n seconds, and the average the distance of the edges to get the center point of the cardboard and just have the sensor do a real time scan
My issue: I don't get how to get the robot to like, turn to the center distance. This is what I've written so far, and the pseudocode indicates my logic behind each of the steps
void loop() {
//code for left side of cardboard
myServo.write(initial); //servo facing straight ahead, initial set to 90
leftDistance = getDistance();
while (leftDistance < 30)){ //while the servo detects presence of cardboard
myServo.write(initial); //increments of 5 to the left side of carboard
leftDistance = getDistance(); //updates detected distance
initial = initial - 5;
delay(100);
}
myServo.write(initial + 5); //the final distance before cardboard went undetected
leftDistance = getDistance();
delay(100);
//same code as above for right side
myServo.write(initial); //servo facing straight ahead, initial set to 90
rightDistance = getDistance();
while (rightDistance < 30)){ //while the servo detects presence of cardboard
myServo.write(initial); //increments of 5 to the right side of carboard
rightDistance = getDistance(); //updates detected distance
initial = initial + 5;
delay(100);
}
myServo.write(initial - 5); //the final distance before cardboard went undetected
rightDistance = getDistance();
delay(100);
avgDistance = (rightDistance + leftDistance)/2; //center point
myServo.write();// HELP: basically i'm unsure how to get the robot to turn/acknowledge center of cardboard at this point
forward(); //it moves forward
delay(200);
//loop begins again
}