GOING over the edge
Learn how to give Marty the ability to sense where the edge of the table is
On the front and bottom of Marty's feet there are some special sensors that help Marty sense different obstacles around them. In this activity we will show you how to use the sensors on the bottom of the feet to stop your Marty from walking off the edge of a table, or accidentally taking a stage dive!
what you will need
- A device with our Marty the Robot v2 app downloaded and up to date
- Marty the Robot v2
What will you learn about?
extra information for educators
Through example code, this activity presents an example of how to use the sensors on the bottom of the feet to sense when Marty is about to fall off a table or object!
Open up the Marty the Robot v2 app and follow the instructions there to connect your robot to the app. Then click on Code with Scratch to open a new Scratch project ready for coding,
Detecting an Edge
We are going to use a simple loop to check if Marty's foot is still on the ground.
A sensor block allows you to read the current state of the sensor that is checking if the foot is on the ground. By assigning what the sensor block reads to a variable we can monitor it in Scratch.
Create a variable called foot on ground. Using the code below, this variable (foot on ground) will read true when the Marty's foot is on the ground and false when Marty's foot is not on the ground. Set foot on ground to store the value of the sensor,
You need this short section of code to run at the same time as other code that you write to control Marty.
Now we can use the value of the foot on ground variable to detect whether Marty’s foot is touching the ground.
At the end of each step Marty takes, we can check the sensor again to see if the foot is still on the ground or not. If Marty's foot is on the ground then the foot on ground variable will read true.
However, if Marty is about to step into thin air foot on ground will read false, indicating Marty has come to an edge. When this happens Marty will no longer take any more steps forward and instead will look angry!
Start both sections of code by clicking on the green flag.
Step Away from the Edge
Once Marty has detected the danger of being about to step off an edge it would be useful if Marty reacted appropriately rather than just looking angry!
Make the code below . This will program Marty to step backwards for 3 steps and then turn around when encountering an edge. The outer loop (called repeat in scratch) allows Marty to walk to an edge 4 times before giving up! And there are appropriate Marty eye movements of course...
You have written many activities worth of code now so will be becoming an expert in Scratch. Try to work out and explain what each part of the code does. You could try making Marty do different moves when getting to the edge - just keep a hand ready to catch Marty if it goes a bit wrong!
So far we have thought about Marty losing contact with the ground by stepping off an edge into thin air. Another way that Marty could lose contact with the ground is if Marty is lifted up. Can you set an alarm so that if someone picks Marty up and tries to take Marty away they will get a big surprise?!
Code a routine so that if Marty's feet lose contact with the ground, an alarm sounds! Here are a few things you will need to think about,
- What is Marty going to be doing until picked up? Standing still? Going round in circles?!
- What sort of noise do you want for your alarm? Something annoying? Something funny?
- Will anything else happen when Marty is picked up?
I need some help!
Here is our example alarm. We used the same idea as the code used in the activity - a repeat loop containing instructions for how Marty should move until the sensor tells us that Marty's foot is no longer on the ground. We have Marty doing a little dance to pass the time!
We keep repeating this dance until Marty's foot is lifted off the ground and the sensor returns false.
We have chosen to add a funny sound using a play sound block if Marty has been picked up. We have set this noise to repeat a few times using the repeat block.
You have now just programmed Marty to react if the sensors on the bottom of the feet tell us that Marty has lost contact with the ground. If you want to continue to explore this idea, here are a few ideas!
- How can you prevent Marty stepping off a platform when walking sideways?
- What about stopping Marty walking off a table backwards?