Graphical, Scratch-like programming languages for Arduino

Snap4Arduino

Back in issue 6 of Raspberry Pi Geek [5], I wrote about Snap!, a very capable, web-based alternative to Scratch. I was even able to create blocks using XML and Python that allowed Snap! to access and use the Rasp Pi's GPIO pins. Snap! is so easily extended that it wouldn't be surprising to see versions for the Arduino; hence, Snap4Arduino [6].

Being Snap-based, Snap4Arduino is quite different from the two options already mentioned. First, its interface (Figure 6) is much more similar to Scratch. Second, it doesn't rely on the Arduino IDE to program a board (well, after a bit of pre-configuration – see below); rather, it goes a completely different way.

Figure 6: Snap4Arduino looks a lot like Scratch.

You can get a zipped package for your platform from the project's homepage and decompress it in the directory of your choice; however, before you start using Snap4Arduino, you have to upload the StandardFirmata [7] sketch to the board you want to use. To do this, make sure your board is connected to your computer and open your Arduino IDE. Configure the port and type of board as usual, and navigate to Files | Examples | Firmata | StandardFirmata. Once the sketch is open in the IDE, upload it to your board. You can now close the Arduino IDE because you won't be using it anymore. You can even use Snap4Arduino on a computer that doesn't have the Arduino IDE installed, and it will still work, as long as the board is running StandardFirmata.

Note that I hit a snag on my Linux box when the startup script Snap4Arduino could not find the libudev library. Changing the line in the script from

if test -e /lib/x86_64-linux-gnu/libudev.so.1; then
ln -s /lib/x86_64-linux-gnu/libudev.so.1 $BASEDIR/libudev.so.0

to

if test -e /usr/lib64/libudev.so.1; then
ln -s /usr/lib64/libudev.so.1 $BASEDIR/libudev.so.0

(/usr/lib64/libudev.so.1 being the correct location of the library) sorted that out.

After that, Snap4Arduino behaved extraordinarily well. For one, Snap has a lot more default blocks than any of the two Blockly-based programming languages. Another thing going for it is that, once you have uploaded StandardFirmata to the board, you can forget about the official Arduino IDE. If you are working with students, that's one less program you have to worry about.

However, as with BlocklyDuino, you have no way to include customized libraries: no serial channel and no panel for messages from the IDE; in fact, no connection to the IDE at all.

Another drawback is that it doesn't work on the Rasp Pi at all. Only Intel-based operating systems are supported.

That said, you can do plenty of cool things with Snap4Arduino to initiate students and beginners into the mysteries of Arduino programming. Figure 7 shows a script that, given the hardware setup shown in Figure 8, reads in a value from the potentiometer and spins the sprite on the stage (top right) accordingly.

Figure 7: This Snap4Arduino program reacts to input from the potentiometer in Figure 8.
Figure 8: Wiring diagram for the Snap4Arduino project.

Snap!2Arduino

Snap!2Arduino [8], or s2a for short, is an extension for Scratch and Snap that provides support for Arduino boards. For some hare-brained backward reason, the people in charge of Scratch have decided to rewrite the language entirely in Flash (!), so I will be ignoring the Scratch bit and concentrating on the Snap! bit.

To get s2a up and running, download the software from the developers' GitHub repository and install some dependencies:

git clone https://github.com/MrYsLab/s2a_fm.git
pip install pyserial
pip install pymata

The pySerial module allows Python to talk with the Arduino over the Serial channel and the PyMata module is Python's interface for the Firmata protocol you installed on your Arduino board earlier.

From the above, you might have guessed that, as with Snap4Arduino, you first have to upload the StandardFirmata sketch to the board you're going to use, if you haven't done so already, by opening the Arduino IDE, configuring it for your board, navigating to Files | Examples | Firmata | StandardFirmata, and uploading.

Leave your Arduino board hooked up to your computer, change into the s2a_fm/ directory, and run

python s2a_fm.py

to start a server that acts like bridge between Snap! (or Scratch) and the Arduino.

You can now start a server running Snap! locally as explained in the Snap! article [5], or you could just visit the official Snap! server online [9]. One way or the other, you will first see a vanilla Snap! without any Arduino extensions.

To load in the extensions, click on the left-most button in the toolbar (with the icon that looks like a sheet of paper with a folded corner). In the menu that opens, choose Import and navigate to the s2a_fm/Snap!Files/ directory on your hard disk. Select the s2a_fm_Snap_base.xml file and click Open.

Now you have a number of new blocks that allow you to interact with your Arduino.

Whereas the Snap4Arduino program discussed earlier places its blocks in a specific Arduino category, s2a spreads them out over several categories depending on their function. You will find a servo block under Motion and analogue and digital input blocks in Sensing, for example.

Another difference is that, in s2a, you have to designate a pin explicitly as input or output (as you would in the setup() section of a native Arduino sketch), whereas in Snap4Arduino, it is done automatically. In Figure 9 you can see how the Snap4Arduino script would have to be rewritten in s2a. Notice the extra block to initialize the pin.

Figure 9: The Snap2Arduino code for the potentiometer project differs slightly from the Snap4Arduino code (Figure 7).

Does it work on the Raspberry Pi? Sort of. Again, you will have to resort to Firefox, but even then, the heavy JavaScript load Snap! puts on the Rasp Pi will make the interaction with the interface laggy and might even crash your Pi. You are better off accessing it from a regular computer.

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content