In this class, you'll start playing with movement and how movement can close a switch. Build a tilt sensor out of foil and a toilet paper tube and use it to control the Scratch cat!
|Build a Toilet Tube Tilt Sensor!|
Grab your recyclables, foil, a marble, a glue-stick, and your Makey Makey kit, and let's get making!
What you are building
A sensor is an input device that senses input from the physical world (like water, light, motion, sound) and then signals that information to an output device. Today we are building a tilt sensor as our input device and Scratch cat is going to read that information and perform the output!
The tilt sensor device you are building works by sensing angular movement. When you tilt your tilt sensor to the left, the metal ball inside will connect and complete the circuit and signal to the computer that the device is angled left. When you tilt to the right, the ball makes a different electrical connection and signals that the input device is at a right angle.
We can replicate a real tilt sensor with simple everyday materials like a toilet tube, foil, and paperclips. The foil inside your tube will be connected to EARTH, and the paperclips will be left and right inputs. Inside the tube, a metal marble will move when you tilt your sensor. When the marble tilts to the right, it will bridge the connection of the right input and EARTH and complete the circuit. This will signal the right arrow of your Makey Makey. When you tilt the tube to the left, the marble will touch the left input and EARTH and signal the left arrow key press.
You can follow along with this video to build your toilet tube tilt sensor or follow the steps below the video.
How to Build It
Cut a piece of aluminum foil to fit inside your toilet tube and use a glue-stick to make it adhere to the inside of the toilet tube.
Unfold a paperclip to be your right input and use a piece of Duct Tape to hold the paper clip to the tube. Make sure the paper clip DOES NOT touch the foil on the inside of the tube.
Use a piece of Duct Tape to hold the paperclip onto the sensor. Before we put the paperclip o the right side, we need to add our marble that will make the electrical connections when we tilt our device.
Let's make your marble conductive! Simply wrap a marble in foil and make sure to smooth it onto the marble by rubbing it between your hands.
Place the metal marble inside and make sure it doesn't roll out of the tube. You might have to manipulate your paperclip to keep the marble in. Also check to see if your marble will connect the paperclip to the foil you placed inside.
Use a second paperclip to be the left input. Unfold and tape the paperclip to the tube. Again, make sure the paperclip does not touch the foil on the inside of the tube.
Use Duct Tape to secure your paperclip, and now you are ready to clip to Makey Makey and test your tilt sensor device!
|Testing Your Tilt Sensor|
Connect an alligator clip to EARTH and the foil on the inside of your tilt sensor.
Make sure the metal on your clip connects to the foil! (The foil inside your tube must also be one continuous piece of foil for this sensor connection to work!)
Plug an alligator clip from the RIGHT arrow to the right paperclip and another alligator clip from the LEFT arrow to left paperclip.
Now you are ready to plug the red USB from the Makey Makey into your computer and connect your Tilt Sensor with your computer! We built this Scratch Project to test your connections.
If you tilt your sensor to the left, the virtual Makey Makey left arrow should light up, and the virtual tilt sensor will tilt left. Don't forget to hit the green flag to focus your computer's attention to this Scratch project. The Scratch cat won't move yet! You have to code it Scratch cat to work with your toilet paper tube tilt sensor in the next step.
If your tilt sensor is working the next step is to code Scratch cat to move with your handmade tilt sensor!
|Coding Scratch Cat to move with Tilt Sensor|
Getting Scratch cat to move with a left tilt and right tilt is actually pretty easy! Click on the cat sprite to code it. To move Scratch cat to the left when the left arrow is pressed, you need to nest this code inside an "if statement."
Open this Scratch Project, then hit REMIX, to code along with us!
This code signals to Scratch cat to point to the left by and move to the left by changing where the sprite is on the x axis. The x axis points changes the left and right direction of the sprite. To change up and down, you would change the coordinates on the y-axis.
To move left when the left arrow is pressed make sure the variable inside the "change x by" block is a negative number. This will make the cat move to the left.
To move right when the right arrow is pressed, duplicate this code and have the Scratch cat point in direction 90 and change the location on the x axis by positive 10.
Place both of these if statements into a forever loop so that Scratch will constantly be checking to see if a left arrow or right arrow key is pressed. If Scratch gets a signal the arrow is pressed, then the cat will move accordingly.
The event to start all of this will be under the "When green flag is clicked" hat. If you want this to be triggered by a different event, you can have this code only work on a certain background, or after a sprite is clicked, etc.
It's always good to have your sprite know where to start on the screen when the green flag is clicked as well. To start the cat along the x/y axis, place the "go to x: ___ y: ___" block under the "When green flag is clicked."
The variables we suggest here will start Scratch cat standing on the black line sprite to the far left of the screen. Try changing these coordinates !
Coordinates measure the distance of this point from the x- axis and y- axis. Where does Scratch cat land if you plot these points:
|Coding Raining Tacos!|
Let's motivate Scratch cat by coding some raining tacos!
Click on the taco sprite to code it. To start at the top of the screen, set the taco on the y axis with the coordinate: 180. To have the taco fall from this point, you want to change the y coordinate with a negative number. If you wanted your tacos to float up, what number would you use for changing the y coordinate?
Once the taco reaches the black line, we want the taco to head back to the top of the screen! To have the taco populate at different spots on the x coordinate plane, just add the "go to random" block, but make sure you still set the y coordinate to 180.
Try your code! Does the taco appear at the top and fall to the bottom like rain? Need more taco rain drops? Before you duplicate this code, you need to create a scoring variable and have something happen when the taco sprite touches the cat sprite!
Create a scoring variable in the variables palette. When the green flag is clicked, you want to set the score to 0. The score will increase if the taco sprite is touching the cat sprite. Find the "touching sprite" reporting block in the sensing palette.
Add a sound, change the score, and change the xy coordinates of the taco so that it starts at the top of the screen again!
Need more rain? Right click on the taco sprite to duplicate the sprite. The more taco sprites you create, the more tacos will rain! Change the wait time on taco sprites so they don't all start raining at the same time.
Full Video for Coding the Scratch Cat and Tacos is available on our Youtube channel and below!
|More Ideas and Next Class|
In this class, you've learned to make a tilt sensor and use it to code and control the Scratch cat.
In our next class, you'll be building and coding a secret code project! With a secret code, you'll have to really think about sequence.
Head to the next class (Link coming soon)