This is a guest post by Airblock user, software developer and Code Club New Zealand volunteer and maker Ben Chartrand, originally posted on his blog LIFT.CODE.PLAY.
In this project we are making use of the Makeblock Airblock’s tilt and LED functions, in hovercraft mode.
You will need an Airblock setup with your tablet. Checkout this project for basic instructions.
Before you jump into coding, here’s a few key concepts to understand.
If you tilt your tablet left/right or forward/back, Airblock will fire (aka invoke) a Tilt event. That means we can “capture” that event and run some code.
Unlike a button press event, which can come with it’s own code, we need to add functionality to constantly check for the tilt events. We’ll do that the moment we power on the Airblock.
One important thing to note is that you only need to tilt the tablet for a moment. If your iPad remains tilted for a prolonged period of time the event may fire many times. There may be a small delay before the event is processed.
On the main module of the Airblock there are three LEDs. You can set them to a variety of colours. In the picture below you can see #1 is set to red while #2 and 3 are green.
There are two ways to set LEDs:
- by explicitly picking a colour
- doing it programmatically (i.e. by defining the amount of red, green and blue with numbers).
My daughter felt we needed a special button, to do a finishing move. That’s why we added the turn button.
As you can see from the code, it will set the LED to purple and change.
Remember that this app uses the tilt function so, when you start the app, have your tablet level / lying flat.
This is what the app looks like in PLAY mode. Flick the Power switch on and start tilting your tablet!
Here’s it is in action. We test all four directions and, in the end, we push our special button. In each case the LEDs change colour (which is also a really handy way of knowing the command has been received, especially since we display the color before doing the movement command).