Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Friday, 23 September 2016

Receiving Lego IR

In the previous post, we used an IR LED to control Lego Power Functions motors, which was pretty useful, but what about going the other way - using a Lego PF remote to control an Arduino?

That standard Lego remote is really nice for controlling Robots as it has two levers, giving you forward/back tank style steering. There's a handy "reverse" switch for each lever, and best of all there's a channel selector, so you can operate up to four remotes at the same time. This is always a problem in workshops where everyone gets the same kit of parts, and everyones remote controls everyone else robots!

At about £7.50 from the Lego store, they're pretty reasonably priced if you're buying them for yourself, or even a classroom, though unfortunatly our workshop budget won't stretch to giving them away, as we do with the regular £1 remotes.

Lego uses a unique (but well documented) protocol, so which we've added to the regular receiveIR device, so if you have build a robot with an IR receiver you don't need to change anything, other than detect the new keypress codes.

Working with IR is cheap and easy - just get a  tsop-4828 receiver for about 50p, and wire it to a data pin on an Arduino (note that the transmit code should work on any micro controller, but the receive as some Arduino specific bits):



The code to read from an device, works exactly the same as it always did:

make remote receiveIR device D2
make irProtocol number
make keyPressed number

when start
.forever
..tell remote to "read"
..if not keyPressed = 0
...say join "Protocol:" [ irProtocol ]
...say join "KeyVal  :" [ keyPressed ]
...say ""

Now, if you point a lego transmitter at it you'll start getting key codes. The channel is returned in the variable irProtocal, so you can easily run multiple controllers at once. If you're really interested in making sense of keyPressed value then there's documentation available from lego. The value returned is the middle 2 nibbles of the data packet, with the escape bit tagged on the beginning.

While its possible to decode the packet based on the documentation, its probably easier to just look at the values that are being sent when each button/lever is pressed, and actual accordingly.

If we look at the codes sent by the regular remote, a value of 16 is sent when nothing is pressed. In fact you can subtract 16 from every received code and things start to make sense: Left stick generates 0 in the centre, 1 when forwards and 2 when backwards. Add these to the 16, that this remote always sends and you're in business. The right stick works the same except we multiple by four, so the value sent is 16+4*rightStick+leftStick.

Of course you can still use most other regular remotes to, but the Lego ones are a bit nicer. The code is now on live.sniff.org.uk along with an example and will by in the next desktop release.

Wednesday, 21 September 2016

Power Functions IR


Lego WEDO is great - you can control power functions motors, but its a bit expensive, and you need a computer. An alternative and cheaper way to control power functions is with the IR control, which you costs £11.50  (important hint: NEVER buy Lego on Ebay or Amazon without checking the lego site first. It's usually much cheaper. The logo consumer store is also much cheaper than the edu store, even though you're buying the same component from the same company). Of course you'll still need some motors and lights and the remote control, but you probably have some of those anyway. Either way you're in business for about £40 rather than £135 (though of course if you buy a few more bits you'll get free shipping, so a few extra parts is saving money in the long run).


It turns out that while the lego IR system is a bit quirky, its well documented, and pretty easy to control from an Arduino or similar. All you need is an IR LED, which cost a few pennies - wire it to one of the pins with a 500ohm(ish) resistor in series so you don't break anything, and you'r good to go.

To code for it in Sniff, just make a device:

make remote transmitLegoIR device D13
make blueValue number
make redValue number
make channel number



I've called it "remote" and attached it to pin 13. Then I powered up the receiver as normal, and connected a motor to the red output. You'll notice that there's an orange slider on the side of the receiver, which sets the channel. If you've only got one then you can just keep it set to 1, otherwise match the channel in the code to the receiver you want to control.

All I want to do is run a motor back and forth, so here's the code:

when start
.set channel to 1
.set blueValue to 0
.forever
..set redValue to sin of (timer*100)
..tell remote to "send"
..wait 0.1 secs

All the code does is set the blue output to 0 (Stop), then goes into a loop. The red value is set to range from -1 to +1: full reverse to full forwards, changing with time. Once you've set the red and blue values as you want them, just tell the remote to "send" and it fires of a few IR pulses and the Power functions should do whatever you command!

Normally at this point I'd tell you that the code would be in the next update of Sniff (and it will be!), but if you go to live.sniff.org.uk now you'll find the example code already there, and you can compile it for Arduino online!


Friday, 16 September 2016

Sniff Live For Arduino

Our initial implementations of Sniff Live were aimed at getting regular programs compiling and running in the browser, so it would be easier for new users to write a few simple Sniff programs without having to install the system. On Windows in particular installation is a bit of work (it's MUCH easier if you're running Linux or Mac).

However the most fun projects we do with Sniff involve external hardware - usually the Arduino Uno, so getting that working with Sniff Live was something we always had as part of the plan, and now its ready for you to try out.

If you head over to live.sniff.org.uk  you'll see that on the front page there's a suggestion to download the Loader app.  Download and unzip it somewhere. The Loader app is the easiest way to upload intel hex files to an Uno, though if you have any other method you prefer that will work too.

Then log in as usual, and take a copy of the blink example using the "copy examples" pop up in the top left. Once you've got that, press the Arduino button in the editor to compile your code for arduino. If you were running Sniff on your own computer, this would also do the flashing for  you, but unfortunately it can't because now the code is being compiled on our server, while the arduino is connected to your PC!

Once the code is compiled, press the "run" button, either in the editor, or in the sidebar (the run link might not appear straight away). This should download the hex file to your computer. The exact details of what this looks like will depend on your browser and its settings, but you should save the file, and open it.

Your PC probably doesn't know what to do with a "hex" file, so set up the file association so that it opens in the "UnoLoader.exe" that you downloaded earlier. From then on when you double click a hex file, or tell your browser to "Open" it, then it should upload the code straight to your arduino without any further intervention from you.

If UnoLoader doesn't work, then check that you have an Uno connected, and that it is appearing as a COM port. If UnoLoader does fail, then its window will stay open and it will tell you whats going wrong. Check out what it says the problem is, and if you can't figure it out, let us know.


Monday, 12 September 2016

Sniff Live!

We've been fairly quiet recently, as we've been working on a big project. It's not exactly a secret (as we've mentioned it in a previous post), but we're just about ready to make a bit more of an announcement about it...

You can now program Sniff entirely in a browser!

After a few year of trying to make the install as easy and as portable as possible, we now have a solution which requires nothing installed on your machine. Just go to http://live.sniff.org.uk, and enter your name to create an account. From there you'll be taken to the online IDE, which is basically the regular Sniffpad IDE but now everything is online.

This isn't intended as a replacement of the regular Sniff implementation - more as a way for you to get started, so it's slightly more limited, but hopefully being able to try stuff out and online will make it an easy way to get started.

To run a program, just press the compile button, and then the run button. Run requires you to enable pop-ups as it runs the program in a new window. Alternatively if a program has been compiled previously a run link will appear in the left sidebar, which doesn't require pop-up permissions.

On the left sidebar you can copy some demo's which are preloaded onto the system, or edit programs you've previously worked on. You can also upload/download code or images. Images can be edited using the paint package, though it is limited to quite small images. Any code you write can load and save files to your account on the server - either text files or image files.

The system can technically compile code for Arduino Uno too, but currently it there's no way to actually execute that code on a board. We're working on that but its a bit tricky!!! 

The system is still in its development phase, so expect things to change/break, but hopefully it should make it a lot easier to use Sniff on a wider range of computers.

Check it out at: http://live.sniff.org.uk

Tuesday, 9 August 2016

ScratchMIT2016

We just got back from Boston, where we attended out third Scratch conference. It was lots of fun to be back at MIT, and meet up with everyone, including lots of people we met in Amsterdam last year.

Officially we had a poster, and were talking about Banana Physics which I've written about here already. Here's an updated PDF with our slides. Thanks to Andy for his help on the stand, and spreading the word.

We also showed off our online editor: live.sniff.org.uk. This is very experimental, but is working well - its the fastest, and easiest way to get started with Sniff, as there are installs required - It all runs in the browser (and compiles on our server), so you can just make an account, and start programming. In the future we hope to be able to support local hardware with a small browser plugin. We'll have a dedicated post on this very soon, and making bigger announcement, as it is a big thing.

We also met lots of new people - too numerous to mention, so I'll just call out some specific work that was on show:

The Colour Divide: https://scratch.mit.edu/projects/71447764/ is a great animation done in Scratch by bubble103 (who was an invited speaker at the conference as a result - aged 16!). As someone who works full time in the computer animation field, I generally don't get that excited by Scratch animations, but this is a great piece of work that deserves all the recognition its getting.

There were a lot of students from Warwick techvolenteers who are doing a lot of great schools outreach doing science projects much like we've been building in Sniff, though they were showing off work aimed at slightly younger kids. They've just transitioned from Picoboard to a custom Arduino shield, which we hope to get a good look at soon.

We finally got hands on with the wedo2, and it was as much a let down as we expected. Driver compatabilities, HW limitations on about 50% of the laptops, binding problems and battery requirements all make it a pretty unconvincing upgrade. While we didn't do any serious testing  it seemed like the motor was lacking in torque, and struggled to handle gearing well. Maybe in the future things will get better, but right now we'll hang on to Wedo V1.0 (or better yet Control Centre B).
There were a couple of companies showing of their robot buggies. Dexter Industries had a session on their GoPiGo, which is a lot like our SniffBotJr that we use for workshops. However unfortunately its based around a Pi, which means it costs $210 for their starter kit.

The robot kit from ROBBO costs about the same at 250Euros and is Arduino based, which is a lot of money for an Arduino based robot, but it is beautifully made. The sensors attach magnetically providing both physical and electrical connections to both digital and analog pins, which is incredibly clever. The sensors just clip on, so you can reconfigure in seconds and they're physically polarised by the shape cut in the acrylic housing, so its impossible to connect them incorrectly. We spend a chunk of time on Friday night, and Saturday afternoon playing with both their robot, and their "lab kit" (an arduino powered pico board style device), and they are sweet if your budget can handle them. Both work out of the box with Sniff (once we figured the pins being used) - program them using the nano-sniff command, and they work great.


If I can make it to the conference next year I think we'll run a workshop on building a real low cost robot - we spend about £25 per robot to cover hardware costs on our workshops, which means every student gets to build a robot, and take it home with them! Check the "Robot" posts here on the blog to see what we've built, and information on how to build your own, or just get in touch if you need help.

Finally Derek Breen (no fixed abode - coming to a sofa near you soon) has a couple of "new" books - well not really... he's quite open that they're cut down rehashes of is original Scratch for Kids book, but they're smaller, and focus on specific things: one on animation and on one games, which means they're much more approachable for a kid with a specific interest. 

And then it was over - a quick stop at the MIT Museum and we were back on a plane to the UK - missing the Delta datacenter outage but a few hours!

Thursday, 21 July 2016

Getting Friendly with Hue

Philips Hue system is an expensive addiction. I picked up a cheap starter kit that was on sale, and a couple of months later, I've spent hundreds of pounds installing it in as many rooms as I can afford. It's crazy fun, and while its not without technical issues, it works really great and does simple stuff that's genuinely useful. While the colour changing is its headline feature, having timers to turn lights on and off automatically, and being able turn on/off multiple table lamps when you enter/leave a room is something you grow to love quickly.

It's also very hacker friendly. Philips have built it as a totally open system. Not only do they provide a documented API and tutorials, but there are even tools built into the Hue Bridge to encourage you to play with the thing. Now with Sniff support its really easy to turn a hue light on and off, just as easily as you flash an LED!


The first thing you need to do is find the IP address of your bridge. It should be four numbers, something like 192.168.0.164. Then open up the Hue's debug tool in a web browser: 

http://<bridge ip address>/debug/clip.html

fill out the first two fields
URL: 
/api
Message Body:
{"devicetype":"Sniff"}

then press physical button on your Hue Bridge (a security feature, so it knows its you and not someone hacking your network). Finally press the  POST button on the web page.

In the response box you should see a value for your "username", which looks something like: 1028d66426293e821ecfd9ef1a0731df. Though they call it a username, its actually like more like  a password. It's whats know as a sparse space - a really big number, where only a few numbers are accepted to let you in. If you know the number then we know who you are, even though you haven't explicitly told us.

If you have problems, then check the linked tutorials from Philips to get the data you need. Now we've got a password we can start writing in Sniff. The first thing we need to do is make a device, and set up the "username" we got from the bridge:

make bridge hueBridge device
make bridgeAddress string
make authorisation string

when start
.set bridgeAddress to "192.168.0.164"
.set authorisation to "oCckjJ2ApQNHc76Q6vrAVmnrsnOJKaLESMK2uqLk"


There are lots of tutorials on how to blink and LED, but now we can "go large":

make bridge hueBridge device

make bridgeAddress string
make authorisation string
make light number
make state boolean

when start
.set bridgeAddress to "192.168.0.164"
.set authorisation to "oCckjJ2ApQNHc76Q6vrAVmnrsnOJKaLESMK2uqLk"
.
.set light to 1
.forever
..set state to on
..tell bridge to "set state"
..wait 1 secs
..set state to not state

To turn a light on or off, just select the light by number, (here we're using light number 1! The Philips tutorial shows you how to get a list of all your lights, and see there names - we'll probably support this in the future), set state to be on or off, and then tell the bridge to set the state.

The next thing to do would be to control brightness:

make brightness number

when start
.set bridgeAddress to "192.168.0.164"
.set authorisation to "oCckjJ2ApQNHc76Q6vrAVmnrsnOJKaLESMK2uqLk"
.
.set light to 1
.set state to on
.tell bridge to "set state"
.
.forever
..set light to 1
..set brightness to (sin of (timer*100))*0.5+0.5
..tell bridge to "set brightness"
..wait 0.2 secs

Here we turn the light on as before, but now we set the variable brightness, and call "set brightness to fade it in and out (using a value between 0 and 1). Note the delay - It's recommended not to send more than about 10 requests per second, just so the system can keep up.

Finally if you have a colour bulb can set the colour, using Hue, Saturation and Brightness, and telling the bridge to "set color". Hue is from 0 to 360, while Saturation is 0-1.


Running this from the desktop is fun, but we can go a stage further. If you've got an Arduino with an Ethernet shield then everything will work exactly the same on the stand alone hardware. All you need to do is tell it to use the ethernet shield instead of normal networking, so you need to add the following lines at the beginning of your code:

make spi device
make ethernet device D10
make networkMAC string "b6:ee:63:ed:95:cb"
make networkIP string "192.168.0.200"


Then use uno-sniff to download your program to an Arduino with an ethernet shield attached. That means you can write code to turn the lights on in response to any kind of hardware event. You could use a PIR motion sensor to turn the lights on automatically when you go into a room (and off when you leave), or just add a whole load of buttons to make a monster Hue control panel!

Release 28: And About Time too...

We normally seem to push out a release every month or so, with some new drivers and a few fixes and optimisations. However since the massive Microbit push back in April we've got almost 3 months without an update. I'm blaming end of school year burn out!

So here we have release 28. To be honest, its been such a long release cycle some of the thing that have gone into it seem like ancient history. There are lots of updates, and fixes for Microbit that just missed the last release, including code for the demos. It should also be a little faster.

jsniff has seen a lot of work. This lets you compile sniff programs to javascript and embed them in a web page. We've been using this internally for a fairly major application that we'll announce soon. If you compile with "jsniff -S xxx.sniff" it will include code to load and save files to a web server for permanent storage, so you can make really advanced web apps. There's a bit of work you need to do server side, but we'll document that soon. If you get a compiler error about "blacklist"ing when compiling with jsniff then you need to make a minor mod to your emscripten install - check the error line which should specific a line of code in the emscripten code... just edit that file, and delete the two lines and you're good to go again. More details later...

If you use the Arduino ethernet shield we've made a change to the way the ip address is specified. Previously this was stored in eeprom, but to increase portability you need to add this to your code. Check the examples/Embedded/webclient example to see how.

Finally we added support for Philips Hue light bulbs!! This is lots of fun, as you can make your own devices to turn your lights on and off. This is still a pretty basic implementation, and we'll probably add more to it in the future

There's a few other odds and ends in there too, but head over to the downloads page and try it out!

Incidentally "and about time too" is the title of Bernie Marsden's first solo album from 1979.