Step 3
Now that we can get PopPet to avoid driving into obstacles, it is time to make PopPet a bit smarter. Instead of just turning left when PopPet sees an obstacle, we will get PopPet to choose which direction it should go. To do this, we will get PopPet to look left, then right and check to see which way is clear (no obstacles, or a further distance to an obstacle) and go that way.
Download the Sketch below and follow along.
Download the Sketch below and follow along.
step3.zip | |
File Size: | 2 kb |
File Type: | zip |
The start of the Sketch will start the same as before, first we must check the distance returned from the Ultrasonic Sensor. We check to see if it is less than 15cm and if it is, we get PopPet to stop:
Checking distance
Now that PopPet has stopped, we will need to see if there is anything on the left; to do this, we turn left and delay for a set period of time to make sure PopPet has turned roughly 90 degrees, then we get PopPet to stop again:
Code Editor
Now that PopPet is facing left we need to take a distance reading to see if there are any obstacles there. Next we turn right, but for twice as long as last time to make sure that we do a complete 180 degree turn. PopPet will then stop again and take a reading for the right hand side.
Code Editor
Now that we have checked both the left and right hand side we need to make a decision to see which way PopPet should go. Now if there is something closer on the left hand side, we need to go right, but because PopPet is already facing right, PopPet can go forward. If there is something closer on the right hand side, we need to go back to the left , by turning 180 degrees to the left, then move forward.
Code Editor
You can now upload the Sketch to your PopPet and see it perform in real life. That's it! PopPet can now successfully avoid obstacles and make decisions about which is the best way to go.
You can tick this off your 'PopPet Goal Checklist'.
You can tick this off your 'PopPet Goal Checklist'.