Introduction: A Wireless Controller for Scratch!
Scratch programming is a lot of fun, and one way to make it even more fun is with a wireless controller!
We used a PocketLab and new ScratchX extensions that were just released to make a wireless controller.
We created a space blaster video game and this Instructable will go through the programming, math and logic to turn physical motion into sprite motion on the screen in Scratch. Once you get this concept, there are many other fun programs, games, and real science apps that you can program with Scratch and the PocketLab sensors!
The following steps explain the code and have links to all the downloads and instructions so you can play with the game and use the code and extensions to build your own projects.
Step 1: Here Is How PocketLab Works As a Wireless Controller
In the video, you can see the PocketLab controller is moving the spaceship sprite on the screen.
If you tilt the PocketLab, the spaceship moves intuitively in the direction that you tilt it.
There is an added feature of shooting projectilves from the spaceship by tipping the controller over quickly.
The general way in which this works is as follows:
1. There is an extension in Scratch written specifically for PocketLab that lets you read PocketLab sensor data in real time.
2. We used the 3 axis accelerometer (PocketLab has 5 different sensors, the accelerometer is one of them).
3. A Scratch program is reading the accelerometer data and moving the spaceship sprite around the screen based on this data.
This is kind of like using a Wii controller, but with Scratch.
Following steps will tell you exactly how to do this in detail and where to get all the files needed to install.
Step 2: How to Make a Scratch Sprite Move Wirelessly
Figuring how to translate motion in the air to motion on screen was really fun! There are probably many different approaches to this, and it was a challenge to narrow the choices down and to play around with the data until something started to click.
The PocketLab has 3 motion sensors that we thought were candidates: magnetometer, gyroscope, and accelerometer. We went through trial and error on each until we settled on the accelerometer.
Here is the theory of translating accelerometer data into sprite motion:
In the first notebook page, I've drawn the x and y axis directions of the PocketLab. There is a z axis as well, and that is pointing through the table. You can see the data on the computer graph. X is red, y is blue, and z is green. Looking at x and y directions, which are parallel to the table, the sensor is at rest and x and y read 0 g (gravity). The z axis is going through the table pointing up and down, and on the computer reads +1g. Why is this? Well, x and y are 0 because they are at rest and perpendicular to z. The z axis is pointing in the direction of gravity, through the table and towards the floor. On Earth, the force of gravity that people feel is about 9.8 meters per second squared, and for convenience it is named 1 g, and is always a lower case g. So the computer reads x=0g, y=0g, and z=1g, and with that you can be fairly certain I took this picture while on the planet Earth.
In the second notebook page, I have rotated the PocketLab onto it's front edge, and now the y axis is pointing up and down and will read +1g. The x axis is unchanged and will still read 0 g. The z axis now reads 0g because it is perpendicular to gravity.
I can repeat this in the other 3 sides of the sensor, and you can see that the sensor will go through the following:
x=0g y=0g when flat on the table
x=1g, y=0g when on the left side
x=-1g, y=0g on the right side
x=0g, y=1g when on the front side
x=0g, y=-1g when on the back side
When we saw that PocketLab will generate data between -1 and 1 for both x and y, all we had to do was to multiply the x and y sensor readings by the position on the screen! It turns out to be a simple and elegant solution.
Scratch lets you position sprites on screen with Cartesian coordinates. X position is between -240 and +240 and y position is between -180 and 180. If we multiply the x and y accelerometer values by this range it will position the sprite according to how the sensor is tilted.
In practical application, there were a extra few details. If you see the Scratch code snippet, there is a limit placed at 1.1:
if 1.1 > abs Get AccelY Value then
if 1.1 > abs Get AccelX Value then
[glide the sprite toward the new position]
This is because if you move the accelerometer quickly, for example by shaking it, the acceleration will go beyond 1 g (or -1g) and make the sprite try to move off screen. So this limit ignores all values greater than 1.1. The "abs" function is absolute value, meaning if the acceleration goes higher than 1 or lower than -1.
Another practical detail is in the way Scratch moves sprites. The Glide function tells the sprite to glide towards a certain location at a certain rate of speed. We had to play with the rate, and found .9 seconds to work. You can change this of course and see how it responds.
Feel free to experiment with any of the values! It's interesting the way the motion changes.
What about z data? Normally when you are holding PocketLab as a controller and tilting back and forth, z data also stays in the the range from 0 to 1. We came up with the idea of flipping the sensor over, where z would go below 0, when you wanted to shoot a projectile.
If you go through the x and y code, and figure that motion out, the z direction will be easy to understand, and the code to shoot is in the ball sprite page.
Step 3: Here Is All the Software and Links You Need to Get Going
In case you don't have much experience with Scratch or the PocketLab app, the video above will go in to detail of both these program. It cover details of the programming, and shows everything running together - it can serve as a tutorial to go through all the steps in this Instructable.
It will help if you've made some Scratch programs already, even simple ones. However, even with very little experience, the video, and the Getting Started guide on the PocketLab website will get you to the point where the Scratch game is running.
To get going, here is everything you need:
1. A Windows 10 computer - the extensions and app only run on Windows 10 for now, and requires Bluetooth for the wireless connection
2. A Chrome browser
3. A Scratch user account
4. PocketLab and the PocketLab App (from the Windows 10 store)
5. PocketLab ScratchX extensions (links are in thePocketLab Scratch Getting Started guide)
6. The 'space blaster' program so that you can start where we left off (also in the Getting Started guide)
The Getting Started guide can be found here: http://support.thepocketlab.com/topic/scratch-gett...