Bump Switches: Stop Marty Going Over the Edge!

Hello guys! We introduced bump switches in a previous blog post here and gave a brief overview of some of their possible uses. In this post 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!

Connecting the bump switch to Marty’s foot

To detect if Marty is about to walk off an edge we need the bump switch to be positioned at the front of his 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:


  1. Take two of the small M2 bolts (these are the longer, thinner bolts included with Marty)
  2. Thread them through the holes in the bump switch
  3. 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
  4. 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
  5. Tighten up the bolts securing the bump switch in place
  6. 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 1. For more detailed instructions on how to do this, see our first post on bump switches.

Detecting an Edge

As before, we use a simple loop to check if the bump switch is pressed at the end of each step. The Input block allows you to read the current state of a switch. By assigning the reading of the Input block to a variable we can monitor it in Scratch. Using the code below the variable “ground_contact” will read 1 when the bump switch is pressed and 0 when the bump switch is not pressed.



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!



Step Away from the Edge

Once Marty has detected he is about to step off an edge it would be useful if he reacted appropriately rather than just looking angry!

The following code will program Marty to step backwards for 3 steps and then turn around when he encounters an edge. The outer loop allows Marty to walk to an edge 4 times before giving up!



Further Ideas

Each Marty comes supplied with two bump switches, so why not attach one to the bottom of his other foot to prevent him stepping off a platform when walking sideways? What about preventing him walking off backwards?