Step 2
The next step is to take what we have learned so far and expand on it to make PopPet do some more complicated things.
We will now look at getting PopPet to avoid obstacles. To do this, we need PopPet to use it's ultrasonic eyes to keep checking for obstacles, if there are no obstacles PopPet can move forward, if there is, then PopPet will turn left to avoid them.
Now this Sketch may look a little more complicated, but it is really just lots of simple things put together to make a more complex Sketch.
Firstly, you will see that we are utilizing the ultrasonic sensor, the IR sensor and the piezo buzzer. We will be using the ultrasonic sensor to check for objects, the IR sensor to make sure PopPet is on the ground and the piezo buzzer so that PopPet can tell us what it is doing.
We will now look at getting PopPet to avoid obstacles. To do this, we need PopPet to use it's ultrasonic eyes to keep checking for obstacles, if there are no obstacles PopPet can move forward, if there is, then PopPet will turn left to avoid them.
Now this Sketch may look a little more complicated, but it is really just lots of simple things put together to make a more complex Sketch.
Firstly, you will see that we are utilizing the ultrasonic sensor, the IR sensor and the piezo buzzer. We will be using the ultrasonic sensor to check for objects, the IR sensor to make sure PopPet is on the ground and the piezo buzzer so that PopPet can tell us what it is doing.
Naming Pins
We will be using the ultrasonic sensor to check for objects, the IR sensor to make sure PopPet is on the ground and the piezo buzzer so that PopPet can tell us what it is doing.
The next step is to tell the Arduino what these pins are. The trigger pin of the ultrasonic sensor is an OUTPUT, the echo is an INPUT. The IR sensor is an INPUT and the buzzer an OUTPUT (however we don't need to tell the Arduino this because we are using the buzzer tone() function which assumes it is an OUTPUT).
The next step is to tell the Arduino what these pins are. The trigger pin of the ultrasonic sensor is an OUTPUT, the echo is an INPUT. The IR sensor is an INPUT and the buzzer an OUTPUT (however we don't need to tell the Arduino this because we are using the buzzer tone() function which assumes it is an OUTPUT).
Pin Modes
To check to see if there is an obstacle in front of PopPet we use the 'scan' function, to return a distance in CM, we then save this to a variable called 'distance'.
Scan for obstacles
Now let's look at how we can get PopPet to avoid an obstacle. IF there are no obstacles, we want PopPet to move forward, ELSE we want PopPet to turn left. To write this in code, we write the following:
Now upload this Sketch to PopPet and hopefully you will see PopPet avoiding obstacles!
Tick this off your 'PopPet Goal Checklist'.
Tick this off your 'PopPet Goal Checklist'.