The next step in our Sniff Microbit Tutorial series to start collecting inputs to control things. Specifically lets press a button to turn an led on and off. The Microbit has two buttons which are connected to pins D5 and D11, so the first thing we need to do is to tell Sniff about that:
make buttonA digital input D5
make buttonB digital input D11
These lines of code will be the same for pretty much every microbit Sniff program, but if you wanted to take that program and run it on an Arduino you might hook buttons up do different pins. The only change you'd need to make is to these lines.
If we attach an LED to pad 2 then we can write:
make buttonA digital input D5
make buttonB digital input D11
make led digital output D2
when start
.forever
..if buttonA
...set led to on
..else
...set led to off
In fact we can shorten that a bit and just write:
when start
.forever
..set led to buttonA
There's a lot of variation on this - what if you want one button to turn the led on and the other to turn if off:
when start
.forever
..if buttonA
...set led to on
..if buttonB
...set led to off
However there's another way to write that, which is more "scratch" like:
when start
.forever
..if buttonA
...set led to on
when start
.forever
..if buttonB
...set led to off
Here we've got two scripts, which checking a button and doing something. Just like Scratch, when we click the green flag/start both scripts can run at the same time. Sometimes this can really simplify things. Lets say there are two leds, and each button will turn on its respective LED for 1 second. That's really hard to do in most programming languages because if I press buttonA to turn on led1, then wait 1 second and turn it off, then buttonB/led2 will stop working for that 1 second. In Sniff this is easy:
when start
.forever
..if buttonA
...set led1 to on
...wait 1 secs
...set led1 to off
when start
.forever
..if buttonB
...set led2 to on
...wait 1 secs
...set led2 to off
Just as we had two kinds of outputs - digital (on/off) or analog (varying brightness), we have two kinds of inputs: digital (like buttons) and analog where we're measuring something that is variable.
If you connect a Light dependant resistor or a thermistor between ground and one of the Microbit pads and a 10K resistor between the pad and 3v we can read in a value which will represent either brightness or temperature. To do that we just write:
make sensor analog input D0
when start
.forever
..say [sensor]
..wait 1 secs
This will print out the value to the computer. This value will go from 0-1. It's possible to convert these measurements into calibrated measurements, but often that's not necessary. We can just use the fact that the value goes up or down to trigger some event.
when start
.forever
..if sensor >0.5
...set led to on
..else
...set led to off
You will probably need to tweak the threshold value a bit to get this working reliably, but with a bit of experimentation you could flash the LED when it gets cold. You could place a light sensor next to a door and detect when the door is opened or closed.
A favourite which I've used many times is to use two light sensors to detect an object passing over them - often a hot wheels toy car.
when start
.wait until startSensor <0.5
.reset timer
.wait until endSensor <0.5
.say [timer]
This measures the time between the two sensors being triggered. If we know how far apart the sensors are we can work out the cars speed!
No comments:
Post a Comment