Tutorial
Wire up a button and see how Marty reacts

Introduction
In this lesson you'll learn how to wire up a button to Marty's Raspberry Pi and then write some code to create a reaction to the button being pressed.
Some help will need to be given to strip the cables
The first thing we have to do is wire up our button
Strip one end of your cable to expose the copper wire. Make sure you leave the female end on (the one with the hole) as this will be plugged into your Pi
Loop the copper ends of your cables through the holes in your button like this. Twist them both to make them secure. Make sure they're not touching!
Pass the cables through the front of Marty's mouth. You can use Blu-Tak to secure your button in place,
Now wire the female end of your jumper cable into your Pi on the pins shown below. You are connecting to the 5 volt pin and pin number 2,
Once our button is in place we then have to write the code to make Marty react to the button press.
Using your preferred Python editor, type in the below code. Remember to change the IP address (192.168.x.x) to your Marty's address (you can use the Marty the Robot app to find out the IP address of your robot).
The code below sets up pin number 2 as an input and constantly (while True) checks if the button has been pressed,
import martypy, time, RPi.GPIO as GPIO commandNumber = 0 GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) #insert the IP address for the Marty you want to connect to marty = martypy.Marty('socket://192.168.xx.xx') marty.hello() marty.eyes(100,1500) time.sleep(1) while True: if GPIO.input(2) == GPIO.HIGH: if commandNumber == 1: marty.celebrate() elif commandNumber == 2: marty.circle_dance('left', 4000) elif commandNumber == 3: marty.circle_dance('right', 4000) commandNumber = 0 #if at number 3 - set it back to number 0 commandNumber = commandNumber + 1
We're now going to try extending the code to do more actions.
Add some more code to your program so that if you push the button 4 times Marty waves the right arm.
Some things to think about:
The code below is required for waving Marty's right arm
Now, can you change the angle of the move and the time it takes to complete the action?
marty.arms(0,90,1000)
Now, can you change the angle of the move and the time it takes to complete the action?
import martypy, time, RPi.GPIO as GPIO
commandNumber = 0
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#insert the IP address for the Marty you want to connect to
marty = martypy.Marty('socket://192.168.xx.xx')
marty.hello()
marty.eyes(100,1500)
time.sleep(1)
while True:
if GPIO.input(2) == GPIO.HIGH:
if commandNumber == 1:
marty.celebrate()
elif commandNumber == 2:
marty.circle_dance('left', 4000)
elif commandNumber == 3:
marty.circle_dance('right', 4000)
elif commandNumber == 4:
marty.arms(0,90,1000)
commandNumber = 0 #if at number 4 - set it back to number 0
commandNumber = commandNumber + 1
Using what you've learned in this task and previous tasks, think about how you might extend this code futher. Can you:
If you're thinking about wiring in more buttons to Marty you'll have to consider where you're going to place your pins on your Raspberry Pi. Using the image below you can decide where to place your cables. You'll need to use one of the 3.3v pins and then choose a GPIO pin
Using the Python documentation on the link below can you extend the program to do different actions?
You have now learned how to wire a button to your Raspberry Pi and write Python code to make the button do something. Why don't you now try: