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.

Wednesday, 20 April 2016

Sniff from Scratch#2: Moving to Microbit

If you read the previous post in this series you're now writing programs in Scratch, running on your computer, but that can get a bit boring. With Sniff it's no harder to run code on a Microbit or an Arduino and its so much more fun. Most Sniff workshops we run involve some kind of hardware simply because it shows code doing something. For some people just writing code is enough - they love the puzzle aspect of it, but most people just ask "whats the point?". Well the point is I want this robot to drive around the room, or I want the lights to turn on if it gets dark, or I want to make a game.

The first step of hooking up a microbit is simply to plug it in via usb. It should appear as both a USB disk and a serial/com port. Once its plugged in and detected, when you run click on the "clickMe" shortcut you'll see that in the startup messages Sniff will say that a hardware device has been found. Hopefully this will be the right device but if you have multiple serial devices plugged in then make sure its the right one (unplug others just in case!).

Now you should be able to take a Sniff program you wrote in previous sessions, but instead of pressing the compile button press the microbit button in Sniffpad (or Arduino - they work the same, but I'll just say Microbit from now on!). This compiles and downloads your program into the Microbit - check that you don't get any error messages, it should say Download OK, and the light on the microbit should flash for a few seconds as the program downloads.

So far we've only written programs that print things out using "say", but the Microbit doesn't have a proper screen to print things on, so where does it go? Back to the computer! Press the terminal button in Sniffpad, and you should get a window appear. Anything the Microbit prints out will appear in that window.

We could run most Sniff programs on the Microbit, but really there's not much point - it would be just like running them on your main computer but MUCH slower. Instead we want to flash some LED's. If you've never done this, then you're going to love it!!!

Strictly we should have a resistor in series with an LED to limit the current, but LED's cost pennies and the microbit is 3.3 volts so we can cheat and not use one. This is BAD electronics. If you try it on a 5v system like Arduino you will damage the LED and/or board, but for Microbit its probably safe.

LED's always have a long leg and a short leg. The long leg goes to +ve and the short leg to negative. On the Microbit that's the two pads on the right marked 3v and GND. Connect an LED between them and see it light up! If it doesn't turn it around (and check the thing is plugged in to power!).

Now connect the +ve leg to the pad marked 0, and the short -ve leg to GND. You'll need some wires/clips to do this - I'm not sure they really thought this bit though... the edge connector is very pretty but not very practical. Now we need to write some code to drive the LED.

The first thing we need to do is tell the system which pad the LED is connected to, so we create an output with the line:

make led digital output D0

This is just like creating a variable, but instead of making a number we're making a digital output. Of course we've got lots of digital outputs, so we need to know which one "led" refers to, and in this case we've chosen pad 0. The "D" in D0 stands for Digital, and is a hangover from Arduino which has two sets of pins: the D pins and the A pins. However its important to include the D, as the Microbit does have another "secret" way of numbering its pins - the pads are numbered based on they way they're laid out on the edge connector, but actually that's not how they're numbered internally. The "D"tells Sniff that we want to use the simple numbering system rather than the secret numbering system.

make led digital output D0

when start 
..set led to on
..wait 1 secs
..set led to off
..wait 1 secs

Now we can flash the LED! We can use LED just like a variable but because its a digital output it can only be either on or off. If you prefer you can use the names yes/no, high/low or  true/false. They all mean the same thing, as they're just different terms frequently used for the two possible states that the pin can be in. Note that 1 and 0 (which are also frequently used to mean the same things) are not allowed. That's because they're numbers and we don't want to confuse numbers and boolean states. Its just simpler that way.

Try playing with the different durations of wait, flash different patterns...

at some point you'll end up setting both durations to be very small. Anything less than 0.01secs and you probably won't see the flashing. If you want to flash the LED really fast you can specify durations in millisecs or microsecs.

.wait 10 millisecs
.wait 100 microsecs

You won't be able to see these flashes but the LED will be dimmer as its now off half the time. In fact we can control exactly how bright by changing the relative duration of the two delays in the program, so its on or off for more or less of the time.

This is so useful that its actually built in as a feature of the hardware. If we redefine out LED as:

make led analog output D0

when start
.set led to 0.5

We can set its brightness to a value from 0 (off) to 1 (on). However its now a number so we can set it to half brightness using a value of 0.5. Strictly speaking the LED is never "half on" as the microbit is actually flashing the led on and off very quickly but the effect is pretty similar.

make led analog output D0

when start
..set led to (timer mod 5)/5

Now you should see the LED get brighter. Finally lets make something really cool:

when start
..set led to (0.5*sin of (timer*100))+0.5

Now at Yr 7 most kids won't know what a sin function is, even though it is in scratch exactly the same way as it is in Sniff, but you can still use it. 

Everything you need is in this diagram. It goes up and down in 360 steps so timer*100 will make it go up and down every 3.6 seconds. It's output is from -1 to +1, but our LED needs 0-1 so we multiply by 0.5 (its now -0.5 to +0.5), then add 0.5 to map its output to the range we need for our LED.

The result is an "Apple" style pulse, and its sufficient to make the most cynical of hardened engineers smile and sometimes even giggle! It's seriously impossible to understate how effective this is. Flashing an LED might not sound like the most exciting class, but everyone will love it!

No comments:

Post a Comment