The heart of Derby's Maker community
After initial experiences with Scratch with Code Club we were interested in ways to connect it to the outside world. The SilkMill have some arduino based robots, and there was talk of getting a MeArm, so was there a way to drive them using Scratch? Snap4Arduino is the closest I’ve seen, and so far it’s looking pretty good.
It’s not being able to control things on a tethered arduino. That’s good, but there are others that can do that. It’s the ability to export the project as arduino code so you can upload it and have it run independently.
I just followed the instructions, put Standard Firmata on a Leonardo, started Snap4Arduino and connected to it to give things a try.
It’s the obvious first test – can we make an LED flash? I went to the ‘Arduino’ menu and hit the ‘Connect Arduino’ button, and it found my Leonardo. You will see blocks in there for setting and reading things, which we’ll come to shortly. Now from the ‘Control’ menu we want a ‘when (green flag) clicked’ to start things off, and a ‘forever’ block so our code keeps running. Inside that we want a pair of ‘wait 1 secs’ blocks from ‘Control’, then before each one a ‘set digital pin to’ block from ‘Arduino’. Set the pin for your LED, and the values ‘true’ and false’ from the ‘Operators’ menu. Start the program and your LED should start flashing slowly.
Here I hit my first gotcha – you can’t just convert any project to arduino code. Instead you need to go to the file menu and pick ‘New Arduino translatable project’. Once you’ve done that take a moment to look through the menus. You’ll see that many of them are empty. All the blocks related to sprites, the stage and so on have been removed because they don’t exist on the arduino.
Now you can recreate the flashing led program as before. The arduino still has firmata on it, so you can connect it and set the pin for the LED as before. You can start the program and it’ll start flashing as before too. However now you can also right-click the ‘when (green flag) clicked’ block and pick ‘export as Arduino sketch…’ from the bottom of the menu to generate your arduino code. A message will briefly appear to tell you where it’s put it, but will probably disappear before you see where it was. On linux it seems to be in your home directory, with the same name as your project but with the .ino extension. That’s not where the arduino ide wants it, but it’ll prompt you when you try to open it. Before doing that you should use the ‘Disconnect Arduino’ button in Snap4Android.
Now you can open your whatever.ino file in the Arduino IDE. If you’ve not already put it somewhere it likes it’ll prompt you about creating a directory to put it in. When it’s open you’ll see the warning header about it being experimental and so on. It will have spotted you’re using a pin for output, and added a suitable pinMode call in the setup() section of the code. The ‘forever’ block becomes the arduino’s loop() section, containing the digitalWrite() and delay() calls. You can now verify and upload your code, and the LED will start flashing again. It’s now a standalone LED flasher – you can disconnect the usb, power it up from another supply and it’ll start flashing. We have proof of concept.
The next step was to try to replicate some of the functionality of existing code running the robot. This uses 5 output pins, one for enable and two each for the left and right motors, plus a couple of digital inputs for microswitch whiskers. The idea is simple – keep going until something tweaks a whisker. When that happens back up and turn away a bit, then go again. It all worked rather easily, the only problem being the pullup issue mentioned below as the robot didn’t have external pullup resistors.
It’s still experimental, so a few problems are to be expected. Apart from the occasional crash, I’ve found a couple of things to look out for with the exported code. This is hardly a surprise, as there is a big header telling you to expect just that!
If you initialise anything before the ‘forever’ block that becomes the arduino’s main loop() the exported code will be outside the arduino setup() block and need moving inside it.
If you read digital pins it configures them as INPUT so if you need INPUT_PULLUP you’ll need to edit it.
I rather like it! It seems like an excellent stepping stone, and it’ll be interesting to see if we can bring it into some workshops.
I’ve skipped over using it tethered with Firmata other than as a test, but in that mode you can use all the usual sprite related blocks. This lets you do things on screen to control outputs on the arduino, and vice versa. I’ve not tried the servo output yet, but it should work a treat with the MeArm.