Writing RaspiOrgan code

If you want to have a go at writing the code for RaspiOrgan yourself, start by creating a new python file
touch raspberryOrgan.py

And open the newly created file in leafpad (or your favourite editor)
leafpad raspberryOrgan.py

To write the Raspberry Organ yourself, read the explanations and type the code into your raspberryOrgan.py file.
As with any python PiFace project we need to import the PiFace module and initialise it
import pifacedigitalio as p

To use conductive objects as keys, we need to turn off the Input Pullups as we did before in the PiFace emulator. You should replace pin_number with the number of your pin. p.digital_write_pullup(pin_number,0)

Now we need to ensure our program will play sounds. For this we will use the pygame module we installed earlier
import pygame
pygame.mixer.pre_init(44100, -16, 12, 512)

Now we need to tell our program where our sound files are and what volume the sound will be played at
sound1 = pygame.mixer.Sound(‘sounds/sound1.wav’)

When we press a button, we want the corresponding sound to play once for each press of that button. To ensure the sound is not played repeatedly we will have a variable to check the last state of the button was ‘up’. We will initialise this variable to True, to enable the sound to be played the first time the button is pressed.
button0Up = True

Now for the main loop, for this we will use a while loop to keep our program responding to button presses. We will then check the input pin has been pressed. Remember to replace pin_number with the number of your pin.
while True:
  if piface.digital_read(pin_number):

We now have to check the variable we set earlier to ensure we are not repeating the sound.
   if button0Up:

If the previous two if statements have been True, our program will now play the sound and we should print to the terminal, the name of the sound. In this line (print “Sound1”) you can replace Sound1 with a name for your sound. We will also need to set our repeating button check to False so the sound is not repeated on a single button press
    print “Sound1”
    button0Up = False

If the button is up, we need to set our variable to True to enable the sound to be played when the button is pressed.
    button0Up = True

And finally copy the code below to ensure our pygame module can close when we want the program to exit
  for event in pygame.event.get():
     if event.type==QUIT:

Save your work and close the editor.
Back in your Terminal, to run your Raspbery Organ, type the following command and press your fruit
python raspberryOrgan.py

Add the rest of your sounds to your Raspberry Organ in the same way, by:

  • Deactivating the other input pins Pullups.
  • Telling your program where the sound files are and setting their volume.
    soundx = pygame.mixer.Sound(‘sounds/soundx.wav’)
  • Initialising another buttonUp variable for each one.
    buttonXUp = True
  • And adding another button check for each button used, inside your while loop. if piface.digital_read(pin_number_x):
      if buttonXUp:
       print “Soundx”
       buttonXUp = False
      buttonXUp = True