Hardware Reference
In-Depth Information
#The wobbling/talking chicken
def __init__(self, pfio, pin):
pfio.init()
self.pfio = pfio
self.relay_pin = pin
def start_wobble(self):
#Starts wobbling the chicken
self.pfio.digital_write(self.relay_pin,1)
if VERBOSE_MODE:
print “Chicken has started wobbling.”
def stop_wobble(self):
#Stops wobbling the chicken
self.pfio.digital_write(self.relay_pin,0)
if VERBOSE_MODE:
print “Chicken has stopped wobbling.”
def say(self, text_to_say):
#Makes the chicken say something
if VERBOSE_MODE:
print “Chicken says: %s” % text_to_say
talker.say(text_to_say)
def test_chicken():
#create a sample Chicken object called tweetyPi
tweetyPi = Chicken(pfio,1)
tweetyPi.start_wobble()
tweetyPi.say(“hello world”)
tweetyPi.stop_wobble()
if __name__ == '__main__':
test_chicken()
With a speaker connected, run the preceding script and check that it works. You should hear
the relay click (and see the corresponding LED illuminate) on the PiFace interface, the sound
“hello world” come from the speaker and then the relay click of again.
Looking back at the code, you should notice a couple of points. he line VERBOSE_MODE =
True is an example of a constant. It is set by the programmer and then never changes - in other
words, it remains constant. In this case, it provides an easy way to turn on and of debugging
information in one place. With it turned on, you'll receive messages saying when the chicken
Search WWH ::




Custom Search