From the course: Raspberry Pi Essential Training

Use Scratch to control the interaction

From the course: Raspberry Pi Essential Training

Use Scratch to control the interaction

- [Instructor] Scratch is a visual or block programming language. It's great for teaching programming concepts and it's included with the Raspberry Pi OS. The version of Scratch included with the Raspberry Pi includes a set of extensions for the GPIO. So you can use Scratch to control the GPIO project we just built. You'll need to start Scratch. It's located in the programming menu. When you create your own Scratch program, you'll need to add the GPIO extensions. Do this by going to the blue icon in the lower left, then selecting the Raspberry Pi GPIO extensions. I've included a Scratch program in the exercise files. Go ahead and open that file now. It already has the GPIO extensions added. It does the exact same thing as the Python program we looked at previously. When you press the green flag to start, it blinks the light, and the cat says that the button is not pressed. When you press the button, it turns on the light and the cat says the button is pressed. Scratch is easy to learn and involves dragging blocks together. There are lots of tutorials for how to do this, so let's focus on the program itself. On the right of the screen, I've placed the complete program. And on the left is the same program, but enlarged so that you can see the commands. The program starts with a block to start when the green flag is clicked. Then this Scratch program sets up three variables. You'll remember pushbutton and blinkenlight from the Python program. Notice that these are set to three and 21, which is the BCM numbering of the pins we attached the switch and LED. The third variable is used later to toggle the LED on and off. After the variables are set, the pushbutton is initialized. The Python program handled this automatically. With Scratch, we need to declare this pin as an input and tell the Raspberry Pi the pushbutton is normally pulled high. This has to do with something called floating state. And for more information, please look this up in the Raspberry Pi GPIO course in this library. Notice I've used the pushbutton variable instead of just declaring BCM 21. I do this for two reasons. First, it's a bit easier to understand what the program is doing. Second, if I decide to move the push button, I can change the value of the variable at the top of the program and not have to change anything else later on. Here you can see that the program enters a loop forever command. Note how everything nests inside this loop. The Raspberry Pi will continue to execute this series of commands until you click on the stop button in the Scratch interface. Remember how the Python program pause for one second every loop? Here's the Scratch equivalent. This command block clearly says to wait one second. After it waits, Scratch runs an if/then/else statement. The if part of the statement tests the GPIO push button. If the button is low, which is the opposite of how we initialize the button, then Scratch executes the following two commands. In this program, I assume that if the pushbutton has gone low, then the push button is pressed. Notice how I've used the pushbutton variable instead of just inserting GPIO 21. The next two commands, which are executed when the button is pressed, tell the cat to say that the button is pressed and sets the light to on. Notice that I've used the blinkenlight variable. Next is the else statement. This executes if the push button is high or not pressed. The first thing to happen is the cat is told to say, button is not pressed. Next is an if/then/else statement. This will toggle the LED to on, then off, and uses the bl_state variable. If bl_state is greater than zero, Scratch will set blinkenlight to low, which turns the light off. It then sets bl_state to zero. Then the next time this program loops, the if/then test will fail and move on to the else part of that conditional test. The else part of the test happens when bl_state is equal to zero. It simply turns blinkenlight on, then sets bl_state to one. This will toggle the light the next time the program loops. After this, Scratch returns to the top of the forever loop and waits for one second, and then retests the button status. Although the Scratch program looks substantially different than the Python program, it accomplishes the same thing. After you gain experience with different languages, you'll begin to see the same programming commands, even though the look or command might be a bit different.

Contents