Transcript
Tilt Sensor Maze Game How to Setup the tilt sensor This describes how to set up and subsequently use a tilt sensor. In this particular example, we will use the tilt sensor to control a maze game, but it is suitable for a large number of other applications. This tilt sensor is very cheap, easy and quick to make.
Required Components:
1 ExperiSense board + Arduino (UNO) + cable 5 crocodile clips (double-ended) 3 pins (fairly long) Plastic water bottle Water (preferably hot, and with salt added) 2/3 elastic bands Button
Safety: Please note that you use these resources at your own risk. Correct use of some components requires care.
Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources
Step 1: Carefully secure the ExperiSense board onto the Arduino UNO; the prongs of the ExperiSense board should fit perfectly into the holes on the Arduino (see images below). Step 2: Wrap the elastic bands round the bottle at two different heights, and pierce the plastic bottle with the pins going through the elastic bands. You need two pins opposite each other, but at the same height, and one a bit lower down (see images below). Step 3: Connect one crocodile clip from one of the top two pins to port A on the ExperiSense board. Connect one of the crocodile clips from the other top pin to port B. Connect a third crocodile clip from the lower pin to ground (GND). Step 4: Fill the water bottle about half way with (preferably hot and salted) water such that the pin connected to GND is fully submerged in water, but neither of the other two pins are. Step 5: Finally, connect the Arduino to the computer via a (type A to B) USB cable.
Safety: Please note that you use these resources at your own risk. Correct use of some components requires care.
Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources
Tilt Sensor Maze Game How to Use… In this example, ExperiSense can be used as a much cheaper alternative to a Picoboard to create a tilt sensor. ScratchX, which can only be accessed online, grants you the ability to play with experimental extensions to Scratch, allowing the creation of Scratch projects that connect with external hardware (such as electronic devices and robotics) and online resources (including web data and web services). The tilt sensor is formed from two different circuits, which are completed by the water in the bottle. The circuits contain the pin connected to ground, and one of the top two pins. Depending on which way the bottle is tilted will determine which of the two circuits is complete, and so determines which direction is registered.
In this worksheet, we will use a tilt sensor to control a maze, which a ball will then fall through. Step 1: Before you can start using the ultrasonic sensor you’ll first need to get the Arduino UNO, and hence the ExperiSense board, communicating with ScratchX – see “How-to Guide” for the ExperiSense board. Step 2: Draw your own maze. Circular mazes work best, and make sure any walls have 2 different colours, one colour which the ball cannot pass through (i.e. the floor), and obstacles which will cause the game to restart if touched (see the image below for an example). You may find it easier to create the maze in PowerPoint, and then import it as an image.
Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources
Step 3a: Now we want to start controlling the maze with the tilt sensor. To do this, we want to figure out what range of values the sensor is detecting. To do this you want to start by creating two sprites to read values. Step 3b: Add the following scripts to the sprites, and then tilt the tilt sensor in each direction and record the values you see. You can then remove the sprites afterwards.
Step 4: Add the following scripts to the maze sprite. This allows you to rotate the maze using the tilt sensor. You can replace the number with any appropriate value as determined from step 3b.
Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources
Step 5: Before we start the ball moving, you will need to determine the starting position of the ball. To do this, drag the ball into the centre of the maze, on the stage and then look at the position (shown in the right
Step 6: We want the ball to move (vertically, as it is the maze that is changing direction) when not touching black, and to stop moving when it is touching black (or whichever colour you have used for the main component of your maze). We also want the ball to return to the starting position if it touches the colour blue (the colour I have used for the obstacles). Again, you should replace this colour with whichever colour you have used. To do this, you want to replace the blocks in step 5 with the following set of blocks. Remark: You may find it convenient, particularly if you intend to continue working on this beyond what is suggested in this worksheet, to use variables to write down the starting position of the ball, as I have done below.
Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources
Step 7: It’s unlikely that it will be possible for anyone to actually finish your maze as it currently is. To finish it, players will need to be able to have the ball travel upwards and downwards. In this worksheet we will have it such that players can reverse the direction by pressing a button. To incorporate this, you will first want to create a suitably named variable (i.e. gravity) that tells you which direction gravity is currently acting in. Step 8: Now you need to replace the ‘1’ in the ‘move 1 step’ block with ‘1*gravity’ blocks as illustrated below. You will also need to place a block at the start of the script setting gravity to 1.
Step 9: Finally, you need to let the player change which direction gravity is acting in. As mentioned before, we will achieve by pressing a button. Connect the button with one crocodile clip going to port c, and the other going to GND, as shown in the image below.
Step 10: To actually change the direction, you just need to multiply the value stored in the gravity variable by -1 every time the button is pressed. You can achieve thisby adding the blocks shown below to the scripts tab for the ball.sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources
Step 10: To allow players to change the direction, you just need to multiply the value stored in the gravity variable by -1 every time the button is pressed. You can achieve this by adding the blocks shown below to the scripts tab for the ball.
Extension Task – …
1. How about including difficulty levels; have an ‘easy’, ‘medium’ and ‘hard’ difficulty settings whereby the speed at which the maze rotates, or the ball moves increases as the difficulty increases. Alternatively, you could make the speed increase over time. [Hint: make use of Variables.] 2. Maybe add some collectables, such as coins for the players to pick up as they go through the maze. You could either associate a score to them, or make it so that the coins have to be collected for the player to proceed. 3. Adapt the tilt sensor so that it can detect four different directions (so it can act as a joystick). [Hint: you will need more crocodile clips.]
Produced by Alexander Holmes (
[email protected]) Technology Volunteers: go.warwick.ac.uk/techvolunteers Scratch Resources: go.warwick.ac.uk/scratchresources