GOING over the edge

Learn how to give Marty the ability to sense where the edge of the table is


We introduced bump switches in Activity 8 and gave a brief overview of some of their possible uses. In this activity we will detail how bump switches can be used to stop your Marty from walking off the edge of a table, or accidentally taking a stage dive!

Find out how to save Marty from falling off a table! It's all thanks to the humble bump switch.

what you will need

What will you learn about?

  • Experimentation
  • Sequencing

extra information for educators

Through example code, this activity presents another example of how to use bump switches to sense when Marty is about to fall off an object!

Connecting the bump switch to Marty’s foot

Create a new Scratch 3 project with the Marty the Robot extension added.

To detect if Marty is about to walk off an edge we need the bump switch to be positioned at the front of the foot on the bottom, as shown in the photo below.

It can be quite fiddly to attach the bump switches however don't let this worry you - after attaching our fair share of bump switches we have some top tips!

It is easiest if you remove Marty's foot before you add the bump switch.

Now do the following:

Take two of the small M2 bolts (these are the longer, thinner bolts included with Marty)

Thread them through the holes in the bump switch

Now attach the M2 nuts to the end of the bolts, but only screw them on a very small amount, leaving a gap between the bump switch and the nuts, as shown in the picture on the left below

Position the bump switch on the bottom of Marty's foot as shown. Make sure the bolts are lowered into the two grooves as in the picture

Tighten up the bolts securing the bump switch in place

Reattach the foot to Marty (and check that the spring is on the correct side)

For this activity, you need to place a bump switch on the bottom of Marty’s foot, as shown in the image below, and connect it to port 0. For more detailed instructions on how to do this, see Activity 8.

Detecting an Edge

As before, we are going to use a simple loop to check if the bump switch is pressed at the end of each step.

An Input block allows you to read the current state of a switch. By assigning what the Input block reads to a variable we can monitor it in Scratch.

Create a variable called ground_contact. Using the code below, this variable (ground_contact) will read 1 when the bump switch is pressed and 0 when the bump switch is not pressed. Set ground_contact to take the value of the output from the bump switch using the GPIO block.

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 ground_contact variable to detect whether Marty’s foot is touching the ground.

At the end of each step Marty takes, we can check to see if the bump switch is pressed. If it is pressed, Marty is standing on firm ground and the ground_contact variable will read 1.

However, if Marty is about to step into thin air ground_contact will read 0, indicating Marty has come to an edge. When this happens Marty will no longer take 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 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 circiles?!
  • 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 bump switch is no longer on because it is not in contact with the ground at the end of the repeat. We have Marty doing a little dance to pass the time!

If the bump switch is off, the output is zero then the repeat stops. We have chosen to add a funny sound using a start sound block if Marty has been picked up.

Remember that this loop of code (written at the start of this activity) needs to run at the same time:


What Next?

You have now just programmed Marty to react if the bump switches lose contact with the ground. If you want to continue to explore this idea, here are a few ideas!

  • Each Marty comes supplied with two bump switches, how can you prevent Marty stepping off a platform when walking sideways?
  • What about preventing Marty walking off backwards?