Visual Command Guide

This post is to hopefully document all the available broadcasts and variable usage that ScratchGPIO understands and responds to

DocPic

Extra Blocks for specific Hardware

Several add-on boards can be connected to a Pi and be automatically recognised and used

AnalogueDocsAnalog Input/Output based on 8591Chip

Any board containing this chip can be used to read 0-5V analogue voltages and to control one variable output

 

 

 

 

 

Scratch Variable “bug”

Although using variables is necessary once you start writing complex scripts, there is a “bug” or a “feature” (depending on yourpoint of view) in the way that Scratch sends out variable changes to any external programs.

If you say broadcast pin11On then Scratch will always send Pin11On out to any listening programs

But if you use a variable called Pin11 and say set Pin11 to On then it only sends this information out if the previous value of Pin11 variable was not On i.e. only changes are sent out

pindocThis code will not switch pin11 on the second time as the variable has not changed from its previous value.

One workaround is to use the green join block and broadcast any variables

joindoc

This method will ensure than any variable changes are always sent out

41 thoughts on “Visual Command Guide

  1. HI
    I have PiRingo board and have been using it with scratch and GPIO ver 4.1.09a

    I can use all the leds and set then on or off and alter powerlevels ok, but I haven’t been able to figure out how to read the push button values. I can’t get any changing output from the sensor blocks, and not 100% certain which one to use switch1Β§, switch2 or button pressed.
    Can you give an example of use of reading the button values pushed/not pushed please.

    • Further to my previous question, I have now completed a program which demonstrates the use of the PiRingo ChristmasWreath/Traffic Light board using both leds and push buttons. To overcome the problems of using variables to send commands to the gpio interface I have used List variables. Once set up, these give a very fast response and enable fast moving patterns similar to those achieved in the python demo flash.py program.
      It is downloadable with a browser from http://r.newman.ch/rpi/circlelight.sb.zip

  2. Simon, I’ve now built a new Raspbian system from a new noobs sd card in the hope that this will somehow allow your dev version of scratchgpio4 work for me with the steppers. No change I’m afraid. I see others have got it working which is encouraging. I am at a complete loss here. Through scratch I can manually turn on/off each of pins 11, 12, 13, 15 but it’s no go for the “broadcast stepper, set motorA to 100, etc. I was wondering if I had actually got all the Scratch features ( eg variable names) that you are using. I noticed in your Visual Commands Guide there are some functions that use “sensor value” such as “say [Version] sensor value for 2 secs” to display the scratch GPIO version. I assume these sensor based function are something you have built into the scratchgpiox. The allowable variables for the sensor value on my system are fixed and limited to the basic list (slider, etc…….) .Maybe one needs to build these oneself using the MESH capability. I am sure you must be fed up with this but am baffled by the stepper situation, particularly when the python stepper program work fine as does my DC motor side. You are welcome to try and remotely access my Pi and take a look.
    Mike S.

    • I am just wondering if you know about the Scratch “feature” that means that once you set a variable – it never bothers to repeat its value across MESH?

      In my scripts I do this
      On GreenFlag
      broadcast Stepper
      set MotorA to 0

      On Spacebar
      Set MotorA to 100

      If that doesn’t work – can you modify that simple sequence from before to make all the leds go on and off sequance.
      Once you’ve done that – can you change the waits from 1 sec to 0.1 sec and at least get the stepper to turn that way πŸ™‚

      Simon

    • Like this
      On GreenFlag
      broadcast AllOff
      forever
      broadcast pin15off
      broadcast pin11on
      wait 0.1
      broadcast pin11off
      broadcast pin12on
      wait 0.1
      broadcast pin12off
      broadcast pin13on
      wait 0.1
      broadcast pin13off
      broadcast pin15on
      wait 0.1

  3. 11:00 am EST
    Simon – “Houston, we have lift-off”
    Your first script worked. The essence of the problem and the many hours in head scratching was totally on my side with the opening BROADCAST statement. I had created a variable call STEPPER and with the mouse move this on top of the existing “set pins” variable in the BROADCAST box. I did not see any other way at the time to add STEPPER.

    The STEPPER variable block seemed to set in nicely on top of the “setpins” variable name so I assumed it had “taken” and all was well. This time I again clicked the BROADCAST pull down menu and noticed, in addition to setpins they was a “new” item for selection. Clicking on new a “Message Name” block appeared into which I typed “Stepper”. On clicking the “on green flag” the stepper started stepping. Hurrah!

    So, Simon, many thanks for sticking with me on this one. It’s very much appreciated.

    Now, on those “sensor value” blocks, does the MESH function need to be “activated” in order to add different variable names to the preset one ( eg to add “Version” to display the scratchGPIO version). I did discover some instruction to activation MESH on a Manchester U. Site called Piface digital. http:/pi.cs.man.ac.uk/download/EnablingMeshInScratch.pdf
    I followed their instructions but not sure if I activated it or not. Some of us need some good doc(s) on the inner working of Scratch for things like MESH and it’s capabilities. You likely use all this in your Scratchgpio development.

    Anyway, thanks again for all your help.
    Mike S.

    • Yippee πŸ™‚
      All this work is not lost as it all helps the next person and it makes me not assume things πŸ™‚
      Right – Scratch MESH 101 πŸ™‚
      Ignore the sensors above the line – these relate to a thing called the PicoBoard and that’s a compeltely differentconcept

      MESH is the way Scratch can talk to external programs by sending data using TCP/IP (same as internet packets) to and from other computers on the same network. IF you Google Scratch Mesh you’ll find out details

      So we use MESH (or Remote Sensor Connections or RSC as its also know) to just send these packets to the RaspberryPi itself and then run a Python program to listen and respond to them. Scratch sends broadcasts and variable changes out this way – my Python prog listens and acts on them to control GPIO pins and also looks for changes in any pins used for input and sends them back to Scratch using TCP/IP and Scratch can read them as sensor values. This is used to detect switchs and things like Ultrasonic or IR detectors.

      All this is done automagically for you by my launch script which launches the Python handler program, then launches Scratch and automatically opens up the rsc.sb script in Documents/ Scratch Projects which because it was last saved with RSC enabled, then it automatically re-enables it. This way Scratch is all set up ready to communicate with the Python handler running in background.

      Hope this info helps but basiscally its all been done for you πŸ™‚

      Simon
      PS My code was originally forked from the original PiFace code – its the universal way of communicating with Scratch 1.4

      • Thanks Simon, your MESH overview is very interesting and good to know of.

        I have been trying out items in your Visual Command List, in particular the ones that use the “say XX sensor value for 2 secs”. I got the “GetTime” and the scratch GPIO “Version” work OK. Very useful.

        However, I could not get the “say ipaddress” one working, mainly because after forming the variable “ipaddress” ( as I did for fulldatetime, hours, minutes and Version) I could not get the darn thing to appear in the pulldown parameter selection menu in order to select it for XX in the “say XX sensor value for 2 sec” command. In fact, sometimes the XX pull down list does and then doesn’t show any of these variables, just shows the “as supplied list). Things seem a little bit flaky in this global variable area, maybe its a timing thing. I usually “Quit” scratch plus reboot to reset everything, but still no “ipaddress” appears in the list.

        Wonder if anyone else has experienced this? As I mention before I have an up-to-date raspbian system. Great to finally have the stepper working on Scratch
        Any thoughts?
        Mike S.

      • I’ll check the IPAddress handling – your the 1st person to ever tell me that they noticed those commands and tried them πŸ™‚
        It should be completely sufficient to just quite Scratch and restart ScratchGPIO as the launching process kilss any previous running process and starts again.

        Reboot would be very much a last resort and I personally have never had to do that.

        If you want to see what is happening in the background then you can just launch the handler seperately using LX terminal
        sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py
        and then run NORMAL Scratch (NOT SCRATCHGPIO) and load in /home/pi/Documents/Scratch Projects/rsc.sb (or any other saved ScratchGPIO script that enables RSC)
        You can then look at the Python handler programming running and spot if any errors are happening but its normally pretty boring (unless I’ve accidently left debugging enabled”)

        Simon

  4. Simon, did not get a lift-off with the first command: sudo python home/pi/…………
    here is LXterm screen shot. Tried it several times. Maybe something on my side needs turning on. Looks like you debug is enabled. Should it be disables?

    pi@raspberrypi ~ $ sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py
    Board Revision 2
    Debug enabled
    2
    PiGlow init
    i2cbus: 1
    No PiGlow Detected
    No Compass Detected
    No pcaPwm Detected
    No AdaMatrix Detected
    No PiMatrix Detected
    PATH: /home/pi/scratchgpio4
    Starting to connect… Trying
    There was an error connecting to Scratch!
    I couldn’t find a Mesh session at host: 127.0.0.1, port: 42001
    Trying
    There was an error connecting to Scratch!
    I couldn’t find a Mesh session at host: 127.0.0.1, port: 42001
    Trying
    There was an error connecting to Scratch!
    I couldn’t find a Mesh session at host: 127.0.0.1, port: 42001
    Trying
    above keeps repeatting until do ctrl+C
    Mike S.

    • if you’d like to try part 2 of the instructions
      “and then run NORMAL Scratch (NOT SCRATCHGPIO) and load in /home/pi/Documents/Scratch Projects/rsc.sb (or any other saved ScratchGPIO script that enables RSC)”

      πŸ™‚

      Until my Python prog sees a Scratch session with RSC/Mesh enabled – its just sits there waiting for one to appear πŸ™‚

      Simon

  5. Simon, assume by “load in” you mean execute rsc.sb?
    Whats the secret to cd’ing to Scratch Projects? Its two words with no joining underscore so get “bash: cd: Scratch: no such file or Dir”
    Mike S

  6. Simon, forget last reply, went through ‘file manager’ and executed rsc.sb from there (double-click).
    Looking in ‘task manager’ though, don’t see rsc.sb running……………?
    What next?

  7. 5:15 pm EST
    Got it! Thanks. Here’s the result. By the way, talking of reservations, we have a real one with a casino about 45 mins north of us!

    OK, given whats below, what exactly am I looking for re possible errors?
    Maybe I have to try more of the items (like IPaddress) from the
    “visual commands set?
    MIke S

    Result

    No PiMatrix Detected
    PATH: /home/pi/scratchgpio4
    Starting to connect… Trying
    Connected!
    Running….
    pin 7 changed to digital out from unused
    (‘pin’, 7, ‘set to’, 0.0)
    pin 8 changed to digital out from unused
    (‘pin’, 8, ‘set to’, 0.0)
    pin 10 changed to digital out from unused
    (‘pin’, 10, ‘set to’, 0.0)
    pin 19 changed to digital out from unused
    (‘pin’, 19, ‘set to’, 0.0)
    pin 21 changed to digital out from unused
    (‘pin’, 21, ‘set to’, 0.0)
    pin 22 changed to digital out from unused
    (‘pin’, 22, ‘set to’, 0.0)
    pin 23 changed to digital out from unused
    (‘pin’, 23, ‘set to’, 0)
    pin 24 changed to digital out from unused
    more of the same displays ending in pin 5 msg.

  8. 5:27 pm EST
    Simon, here’s first few lines after the sudo etc etc is executed. Debug is “On”/
    MIke S.

    pi@raspberrypi ~/Documents/Python_Projects $ sudo python /home/pi/scratchgpio4/scratchgpio_handler4.py
    Board Revision 2
    Debug enabled
    2
    PiGlow init
    i2cbus: 1
    No PiGlow Detected
    No Compass Detected
    No pcaPwm Detected
    No AdaMatrix Detected
    No PiMatrix Detected
    PATH: /home/pi/scratchgpio4
    Starting to connect… Trying
    Connected!
    Running….
    pin 7 changed to digital out from unused
    (‘pin’, 7, ‘set to’, 0.0)

    • I’ve lost the plot myself now πŸ™‚
      i think (for whatever reason) your setup is not in the same state as mine (And since I have 4 different ScratchGPIO V4, V4dev, V4DevDev and V5Dev) I think the fault is at my end πŸ™‚

      The IpAddress isn’t very useful so I think its just best to leave it alone – at least the datetime works πŸ™‚

      Forget everything about running the python handler manually and stick to the desktop icons – I’m nearly there on releasing V5 with all recent updates so we should be good to go when I do that πŸ™‚

      Simon

      • Simon, OK, good suggestion.
        I did try IPaddress and the Version again on the scratch session opened by RSC. Neither worked initially with non of the new variables showing in the list, but shortly thereafter they magically appeared. So IPaddress did work as did version. Seems to be delays in the custom variables (version, ipaddress, etc) showing up. But no matter as you say – “on the scratchGPIO5!.
        (Whats in gpio5?)
        Mike S

      • Basically just extra support for add-on boards and revert the default pin state back to what I had origannly 11,12,13,15,16,18 as outputs and 7 and 22 as inputs but with internal pull-up resistors enabled so that all people have to do is connect a switch between them and ground.

        The delay is very wierd but just to note – they won’t show up unless you do the approriate broadcast -i.e they are not there until you ask for a reading and the reading takes 0.1 sec to happen

        Simon

  9. This is awesome! Incredibly easy to get it up and running for simple pushbuttons and controlling LEDs. Do you have any more details or documentation on reading analog inputs? Would it be possible to get ScratchGPIO working with any arbitrary ADC and not just the 8591? Sorry if this is a dumb question, I’m used to Arduino and spoiled by the built-in ADC, and have yet to try using an external one with the Pi.

      • Ok – how about the blocks for the ultrasonic sensor – are those written for a specific model number, or do ultrasonic sensors all follow the same protocol? (again, sorry for the amateur questions)

      • Ok – so I’m trying to get the 8591 working based on the code above. I’ve found some wiring diagrams – for example here:

        http://blog.jacobean.net/?p=258

        And it looks like it mostly consists of hooking up the proper V+ and GND connections (although I’ve seen conflicting information about whether to use 3.3 or 5V for VDD…the chip’s data sheet says “operating supply voltage 2.5V to 6V”…either away I assume you want to use 3.3V for the analog reference VREF), along with the SCL and SDA pins on the Pi.

        However, I’m not sure if I have the Scratch code right. The “say” block has a bunch of options in a drop-down menu (slider, light, sound etc) but there’s no “adc1” option, nor is there a “new” option so I can type in my own.

        Ideally I’d like to use the number as a variable in the program – for example, read in from an analog distance sensor and use that to steer a sprite left or right. Any suggestions on how to get this running properly in Version 5? As always, really appreciate the help and effort you put into this!

      • I just use a pre-built prototype board by 4tronix so I don’t know how the hardware is wired up and the board isn’t on market yet.

        If its wired up right, it should be autodetected by the Scratch handler and the adc1..4 values will just appear in the sensor values like pins do.

        best thing would be to make sure its appearing.

        I’ll try and dig my board out and jsut check the code is still working Ok but I’ve got a backlog of requests at the moment and a lot of DIY to do at home πŸ™‚

        Simon

      • Ok – I’ll see if I can get the chip working in Python in the meantime, so I can make sure I actually have it wired up correctly.

      • Dumb question. What does “detected at address 48” mean, and how would I check that?

  10. I love what you are doing here. I found it through a link from a tutorial page here:

    http://computers.tutsplus.com/tutorials/learn-how-to-use-raspberry-pi-gpio-pins-with-scratch–mac-59941

    That page has a nifty little game that uses ScratchGPIO, and it works fine as written. However, to increase my understanding, I have been trying various simple changes, and I am stumped on one of them. Maybe I don’t know which pins are allowable inputs in ScratchGPIO. The game uses push buttons attached to pins 3 and 5 to move the cat. However, if I try to change which pins are used, (7 and 8 for example) and change the wires to connect to those pins, the cat just stutters and the buttons do nothing. They behave as if they are getting constant 0 values (i.e. not pulled high) because if I change one of the equations to check if the pin value is something other than zero, then the one that is still set to zero moves the cat.

    Any explanation would be appreciated. If I can figure this out, I plan to use it with a local young person I am teaching to program.

    Thanks,
    -Steve

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s