Search This Blog

Monday, 28 March 2016

Little playtime with a Bubble machine and Raspberry Pi - now you see them, now you don't

Tested with Python 2.7.9.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Playtime with a Bubble machine
#-----------------------------------------------------------------------------------
#
# The user has a predetermines time to have fun with the Bubble machine
# He needs to provide the password. If it is correct, he will be able
# to enjoy the bubbles for a limited time, until he is asked for the password again.
# 
# If the password the correct, a green LED lights up and the Bubble machine
# starts churning. Before the password is provided or if it is wrong, a red LED 
# is lit up and no bubbles.
#
#-----------------------------------------------------------------------------------
# Bubble machine is connected to:
# ground (GND)
# 5V pin
# G25 pin
# (for more information see: http://blog.web-zazen.co.uk/2014/12/controlling-bubble-machine-with-arduino.html
#
# LEDs are connected:
# long wire through 10kOhme resistor:  
#  green LED: to G18 
#  red LED  : to G20 
# short wire to ground (GND)
#-----------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------

import RPi.GPIO as gpio
import time

# Setup
# Rasperry Pi pins
#-----------------------------------------------------------------------------------
gpio.setmode(gpio.BCM)
gpio.setup(18, gpio.OUT)   # Green LED
gpio.setup(20, gpio.OUT)   # Red LED
gpio.setup(25, gpio.OUT)   # Bubble machine

#  timeout
#-----------------------------------------------------------------------------------
playtime = 30     # Playtime length
nowtime = time.time()    # Starting now
stoptime = nowtime + playtime

# Playtime !!
#-----------------------------------------------------------------------------------
while ((stoptime-nowtime) > 0):
 print "You have %d seconds left" % (stoptime-nowtime)

 gpio.output(18, gpio.LOW)  # Starting with switched off green LED
 gpio.output(20, gpio.HIGH)  #   switched on red LED
 gpio.output(25, gpio.HIGH)  #  switched off Bubbles

 password = raw_input("Show you are the privileged one. What is the password? ")

 # Authentication
 # successful
        #---------------------------------------------------------------------------
 if password == "bubbles":
  gpio.output(18, gpio.HIGH) # Green LED on
  gpio.output(20, gpio.LOW) # Red LED off
  gpio.output(25, gpio.LOW) # Bubbles blowing!

  time.sleep(5)   # ... for 5 seconds we are happy

 # failed
        #---------------------------------------------------------------------------
 else:
  gpio.output(18, gpio.LOW) # Green LED off
  gpio.output(20, gpio.HIGH) # Red LED on
  gpio.output(25, gpio.HIGH) # No bubbles

  print("You failed the test!")

 nowtime = time.time()
 
# Clean up - switch everything off
#-----------------------------------------------------------------------------------
print "No time left, buster"
gpio.output(18, gpio.LOW)  # Ending with switched off green LED
gpio.output(20, gpio.LOW)  #        switched off red LED
gpio.output(25, gpio.HIGH)  #       switched off Bubbles

# Close the channels to avoid a message about channels being already in use
#-----------------------------------------------------------------------------------
gpio.cleanup()

No comments:

Post a Comment

Note: only a member of this blog may post a comment.