Due to a couple of requests, I've produced this verbose guide on how to control the PiFace with Scratch. I can't claim any specialist technical wizardry on this matter and all the info is available on the web, but I do hope I have managed to clarify the steps needed to do this.
How to use Scratch to control the Raspberry Pi PiFace interface - A Guide for Dummies (like me)
1) Download the 4gb Raspbian Linux SD Card image
from the PiFace team from here (http://pi.cs.man.ac.uk/download/
). This includes all the required libraries and extension programs (including a graphical emulator of the PiFace) for interfacing the PiFace with Scratch, including a modified version of Scratch. You can download and install the additional components yourself, but this is much easier.
2) Burn the .img to SD card using your preferred method (check the web if you don't know how), insert into the RPi with a keyboard, mouse and monitor attached, connect the PiFace and boot the RPi (network or internet access is not needed unless you want to use ssh/vnc for remote access) Login with pi and raspberry as usual and type startx at the command prompt to run the desktop environment.
3) Run Scratch from the desktop and then shift-click on 'Share' in the top menu line
4) Select 'Host Mesh' from the drop down menu
5) Note IP address displayed (mine was 127.0.1.1) and click OK
6) Minimise Scratch (don't close it) and run LXTerminal from the desktop. Type in the following commands at the pi@raspberrypi$
prompt in the terminal window:
python scratch_handler.py 127.0.1.1
(replacing the IP numbers with those from your machine if necessary)
Note: Do not click on the 'Scratch Handler' icon ion the desktop!
If everything is correct, the terminal window will now show:
Connecting … Connected!
sending sensor-update 'piface-input1' 0
sending sensor-update 'piface-input2' 0
sending sensor-update 'piface-input3' 0
sending sensor-update 'piface-input4' 0
sending sensor-update 'piface-input5' 0
sending sensor-update 'piface-input6' 0
sending sensor-update 'piface-input7' 0
sending sensor-update 'piface-input8' 0
Minimise the LXTerminal Window (don't close it) and maxmise Scratch again - you should now be good to go.
It's messy but simples when you know how. Hopefully someone will create an set up program or installation script that will automate this process for those of a nervous disposition - i.e. non-specialist ICT Teachers
Make sure not to close the terminal window or else the connection with the 'scratch_handler' will be lost.
(Sorry, being a text-based description with no graphics, this is very verbose and, as I don't know the correct Scratch terms yet, I'm using my own i.e. 'button', 'control' and 'program window' in these instructions)
To program the outputs:
1) In Scratch, click on the 'Variables
' button (top left corner of Scratch) followed by the 'Make a variable
' button (which appears below)
2) Type piface-output1
into the pop up dialog box and click OK
3) Repeat this seven more times with different numbers ie piface-output2
etc to make variables for all 8 outputs
To make the PiFace LED 1 flash 10 times
1) Click on the 'Control
' button (top left corner of Scratch) and drag the 'when space key is pressed
' control to the empty 'program window' on the right
2) Drag the 'repeat 10
' control to the 'program window' and drop it directly below the 'when space is pressed
' control so that the two become attached
3) Drag two separate copies of the 'wait 1 secs
' control and leave them unconnected in the lower half of the 'program window'
4) Click on the 'Variables
' button and drag 'set piface-output1 to 0
' to the 'program window'. Change the value of the output from 0
in the control
5) Drag one of the 'wait 1 secs
' controls (from the lower half of the screen) underneath the 'set piface-output to 1
' control so that it attaches to it
6) Drag another copy of 'set piface-output1 to 0
' to the 'program window' and drop it below the 'wait 1 secs
' control so that it attaches to it.
7) Drag the other 'wait 1 secs
' controls (from the lower half of the screen) underneath the 'set piface-output to 0
' control so that it attaches to it
Now, if you press the 'space key
' on the keyboard the program should run immediately and LED 1 should flash on and off 10 times. It's interesting to note that once the program controls are dropped in the program window they can be run just by clicking on them - or in this case by pressing the 'space' key.
To program the inputs:
(this carries on from using the program created for flashing LED 1 above)
1) Click on the 'Control
' button (top left corner of Scratch) and then drag the 'forever if
' control into the lower half of the program window
2) Click on the 'Operators
' button and drag the green '[ ] > [ ]
' control on to the empty space of the same shape in the 'forever if
3) Click on the 'Sensing
' button (top left corner of Scratch) and drag the blue 'sensor value
' control into the first white box in the 'forever if
4) In the 'sensor value
' control, change the dropdown value from 'slider
' to 'piface-input1
if there are no piface-inputs
available, the connection has been lost with the scratch handler and the 'setup
' routine outlined above will need to redone.
5) In the second white box after the >
operator, type 0
6) Separate the 'when space key is pressed
' control from the 'repeat 10 loop
' in the previous program (by dragging the 'repeat 10 loop
' away from it and then delete the 'when space key is pressed
' control by dragging and dropping it left out of the program window
7) Finally, drag the 'repeat 10 loop
' and drop it inside the 'forever if
' loop so that it connects
Click on the 'forever if
' loop and the program will run, but nothing will happen until the PiFace's Input 1
switch is pressed (or a switch is connected to the PiFace and pressed). When the Input 1
switch is pressed, the loop will start flashing the LED 1
10 times. This will continue forever or until you stop it due to boredom!
This should be enough to give anyone the basic concepts of using Scratch to control the Raspberry Pi with the PiFace interface. It would be much better to use graphics to explain the Scratch programming and so I will probably do this somewhere on the web. Any constructive comments are welcome including notification of any typos.
Hope this helps the RPi community.
All the above instructions will also work with the PiFace Emulator
(a graphical simulation of the PiFace included with the Raspian image provided by the PiFace team) too, so you don't actually need to have a PiFace to try them out. To use the emulator (instead of or as well as the PiFace) add the -e
switch to Step 6
of the Setup
instructions above so that it becomes:
python scratch_handler.py -e 127.0.1.1
(or whatever the IP address Scratch gives to your machine)