In the first lesson, you learned to use the Finch outputs: the motors, lights, and buzzer on the Finch. The Finch also has sensors that provide input to the robot. A sensor makes measurements and sends them to the program that the Finch is running. The program can use the sensor information to set outputs or make a decision. The Finch has two light sensors, an acceleration sensor (accelerometer), two obstacle sensors, and a temperature sensor.
Drag a light sensor block into the Scripts area. Click on the block to see the current value of the sensor. Next, cover the light sensor with your hand and click on the block again. You should see the value of the light sensor change. What is the value of the block when you shine a flashlight on the sensor?
To see the value of a sensor change, it is helpful to create a variable. A variable is just a name that represents a value. You will now create a variable named “left light” that will hold the value of the left light sensor and display that value on the screen.
First, go to the Variables menu. Then click Make a Variable.
We want to set the value of our variable equal to the value of the left light sensor. To do this, we can use the set to block. We will place this block inside a forever loop to continually set the variable to the value of the sensor.
You can use the sensor blocks to make the robot respond to its environment. For example, you can make the robot follow a flashlight using the script shown below. The Move Finch block set the speed of the robot using the values of the light sensors, and the forever loop repeats this action over and over.
Create a variable named “temperature” that is equal to the value of the temperature sensor. What is the temperature in your room in Celsius? Can you raise the temperature reading of the sensor?
You can place arithmetic blocks inside one another. When you do this, the operation in the innermost block will be performed first. In the expression below, the 2 and 4 will be added together first, and then the result will be multiplied by 10. What will be value of this expression be? Make a hypothesis and then test it in Snap!. Remember, you can click on a block to find the value of the block.
The last Finch blocks that we will use in this lesson measure acceleration. These blocks measure how much the Finch is tilted (or changing its speed). When the Finch is sitting on a flat surface, these blocks will not change. There are three different blocks because the Finch can measure tilt in three different directions. The value of each block is a decimal number between -1.5 and 1.5.
You can use the accelerometer to move a sprite around the stage in Snap!. This will enable you to make your own video games with the Finch as the controller!
Declare another variable named xPaddlePosition. You will use this variable to determine the x -position of the paddle on the stage. As you tilt the Finch, the value of the y -acceleration is between -1 and 1. What number will you need to put in the blank below to make the paddle move from -240 to 240?
Place a go to block at the bottom of your forever loop. Use this block to continually set the x -position of the paddle sprite to xPaddlePosition. The y -coordinate for the sprite can be -180; this is the bottom of the stage. When you run your script, you should be able to move your sprite by tilting the Hummingbird!
You will notice that when you tilt the Finch to the left, the paddle moves to the right. When you tilt the Finch to the right, the paddle moves to the left. How can you adjust your code to fix this problem?
Now that you can control a sprite with the Finch, you can add other elements to create your own video game! For more details, see the Finch Pong I activity; you have already done the first part of this activity.