Thursday, 19 March 2015

Land of Hose and Glory

I've just bought a Raspberry Pi. I got it straight out of the box and tried to connect it to a hosepipe.

Martyn Currey wrote a very helpful article on how to run a solenoid from an Arduino, and I used exactly the same circuit to make the Raspberry Pi control a 12v solenoid valve.

Classic GCSE electronics. (I've just noticed the diode seems to be wired up wrong, but it didn't seem to matter.)

The Raspberry Pi encourages you to use a programming language called Python. It's an easy-to-use language based entirely on Monty Python's Flying Circus, and so everybody loves it.

Here's the code:

import RPi.GPIO as GPIO    # the GPIO library allows us to control the pins
import time                # the time library lets us pause
import random              # library has random number generator

GPIO.setmode(GPIO.BCM)      # I don't know what this does. I copy + pasted it.
GPIO.setwarnings(False)     # Perhaps I should find out.

hose = 4                    # Sets the GPIO pin the hose is connected to

# Create list with the rhythm of Land of Hope and Glory in quarter notes
pomp = [4,1,1,2,4,4,4,1,1,2,8,4,1,2,1,4,4,4,1,2,1,8,4,1,1,2,4,4,4,1,1,2,8,4,1,2,1,4,4,

tempo = 88.34 # tempo in BPM based on conducting of Sir Arthur Bliss

quaver = 60/(2 * tempo) # length of 1 quarter note, in seconds

legato = 0.5 # number from 0 to 1. 1 is full legato but would mean the hose
             # ...was on all the time.

GPIO.setup(hose, GPIO.OUT) # I don't know what this does either.

for x in pomp:                          # Loop through array

    on = x * quaver * legato            # Time in seconds for hose to be on
    off = x * quaver * (1 - legato)     # Time in seconds for hose to be off

    GPIO.output(hose, 1)                # Turn hose on
    time.sleep(on)                      # Wait
    GPIO.output(hose, 0)                # Turn hose off
    time.sleep(off)                     # Wait


It works. Huzzah! The video finishes just before the Pi gets covered in water.

No comments:

Post a Comment