Hardware Reference
In-Depth Information
Listing 14-1 Slot Car Racer Hack Test
#!/usr/bin/env python
“””
Slot Racer Hack tester on the PiFace board
“””
import piface.pfio as pfio # piface library
pfio.init() # initialise pfio
def main():
lastInput =0
print “Slot Racer Hack test press the two input ;
switches”
print “on the PiFace board to change who is racing”
print “Ctrl - C to quit”
while True :
buttons = pfio.read_input()
if (buttons & 1) ==1 and buttons != lastInput:
print “player 1 racing”
pfio.write_output(0x01)
if (buttons & 2) ==2 and buttons != lastInput:
print “player 2 racing”
pfio.write_output(0x02)
lastInput = buttons
if __name__ == '__main__':
main()
You will see it is a very simple program that just energised each of the relays depending on if
you press one of two buttons on the end of the PiFace board. A message is printed to say
which player is racing each time it changes. Make sure that the players are the right way
around and that you can play when the console message says you can. If you ind the control
is the wrong way around - that is, when it says you can play you can't and when it says you
can't you can - then you may have mixed up the NO and the NC relay connections. NC
stands for normally closed - there is a connection between the common line and this one
when the relay is not energised. If you don't hear the relay clicking at all but do see the two
lower LEDs come on and of, then check that the links JP5 and JP6 are made.
Getting the Player Input
Next you need to ind a way to input the players' answers. You could just arrange a row of four
switches for each player along with red and green LEDs. In fact the schematic in Figure 14-3
can be implemented in exactly that way. However, at this stage in the game you can be a lot
more adventurous than that, so I am going to show you how to make an illuminated switch
joystick using that same schematic. Not only is this useful for this project, but you also can use
the joystick on other projects in this topic, replacing a keyboard input. In Chapter 8, “Colour
 
Search WWH ::




Custom Search