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.

Thursday, 12 May 2016

Skylanders Vs Infinity on the Microbit

It's just been announced that Disney Infinity is dead. This isn't so much of a surprise - V1 showed some promise, but V2 didn't include any Disney characters, alienating the market they'd grown with V1 (I would have bought it if the selection of Marval characters included Jessica Jones, just to unlock the empty whiskey bottle in the toy box), while lacking depth to appeal to older players.

Skylanders has also jumped the shark... I quite liked Swap Force, but Super Chargers was just dull - probably because as a parent my role was to hold the second controller and press fire, while offspring got to do all the driving. 

So the only think left to do with all those figures - let them battle it out one last time with your Microbit! Both Skylanders and Disney Infinity use "RFID" tags inside the figures, so that the base station can identify them. It's the same technology that's used for contactless credit cards.


To teach your Microbit to read RFID tags you need a "Mifare RC522", sometimes called MF522. These cost about £2.50 on eBay, and you can use them to read all sorts of cards. You can even buy "blank" cards, which you can store data on. Inifiity/Skylanders use this feature to record the progress on the figure rather than in the game, but they have a security code so you can't easily write to them yourself. All we're going to do is read the ID of the card.

Hook up is really easy: If you've checked out our other microbit posts you'll have used SPI. Just connect the power, and the 4 SPI wires from the MF522 to equivalent ones on your microbit, and you're good to go. There may also be a "reset" pin but just ignore that.

make spi device
make cardReader rfid device D16
make cardData string
make cardAddr number

make message string
make scrollDone boolean

First thing to do is make an SPI device and then an rfid device. I used D16 for "CS/SS" though you could use something else if you wanted to. I'm going to display the data on the screen using a script called scroll, so I also set up some variables for that.

when start
.set message to "ready"
.broadcast scroll and wait
.
.forever
..repeat until not cardData = ""
...tell cardReader to "get card"
..set message to cardData
..
..if message="12,5c,20,10"
...set message to "Bash"
..if message="88,4,7e,eb"
...set message to "Elsa"
..
..say message
..broadcast scroll
..tell cardReader to "release card"
..wait until scrollDone

To use the "scroll" script we set the message and run the script. We kick of by saying "ready". Now we're going going to tell the reader to "get card", which tells it to start talking to a card. If it finds one it puts its card ID into cardData, so when we find one we can set that as the message.

The card ID will look like four hexadecimal numbers. I check them against the figures I already know about, and if they match, I replace the ID with the characters name. I picked Elsa and Legendary Bash to use (remember these are the numbers for my figures - yours will be different). You can add as many as you like. If you have a lot you could write some code to check them against a list rather than testing teach one. You could even add an SD card, and have a file of known figures.

Once I know the character I "say" it to the computer, and run "scroll". We don't "and wait" this time as we need to tell the card reader that we're now done with this card, and it should disconnect. Then we wait until the scrolling is complete.

The code to display/scroll text is something we've also seen before:

make display microbitDisplay device
make displayX number
make displayY number
make displayColor number
make displayFlush boolean

when start
.forever
..tell display to "tick"

make offset number
when scroll
.set scrollDone to no
.set displayColor to 0
.tell display to "clear"
.set displayX to 1
.set displayY to 1
.set offset to 8
.repeat until displayX<0
..set displayColor to 0
..tell display to "clear"
..set displayColor to 777
..set displayX to offset
..tell display to "show"
..change offset by -1
..wait 0.08 secs
.set scrollDone to yes

The only new thing I've added is that it sets scrollDone to no at the beginning and scrollDone to yes when its finished. This allows the main script to wait until the full text is displayed before going again.

And that's it - really easy. You could use figures to control anything you can connect to your microbit. Use them to unlock doors, control lights, make your buggy move. You could write your own "credit card" system and pretend to shop! When you've done that have a look at examples/Embedded/rfid which includes reading and writing data to programmable cards. 

No comments:

Post a Comment