AZIBOt for Educators
Lesson Three: The Button
- November 11, 2015
- Posted by: Alex
- Category: Uncategorized
In this lesson, we will learn how to use the button, the LED, and conditionals. Let’s start with the button.
The button is the pushbutton; yours may be red. Buttons are a form of input, meaning that the Freaduino takes information from it. The signal it outputs is called a digital signal. Digital signals can take one of two states, either high (also represented by on, 1, or TRUE), or low (also represented by off, 0, or FALSE).
Next, we have the LED. The Freaduino board has one built in connected to pin 13; we will be using this one. LED’s are a form out output, meaning the Freaduino outputs information over it. It too is digital, meaning it can either be on or off, nowhere in between.
Now for the code. Pull a If-Do block from the logic menu. Next, click on the small gear in the corner of the block and pull an else into the block. It should now look like this:
Next, add the code to make it look like this:
This block first reads the digital pin number 7. It reads this pin because this is the pin that we hooked the button up to. If the digital read return high, then it does the code in the “do” part of the block. If the digital read does not return high, meaning it returns low, then it does the code in the “else” part of the code. Do you think the button pressed corresponds to high or low?
Let’s talk about what happens in the “do” section. It will set the digital pin to high. We can set the pin because it is an output. Setting this pin to high turns the LED on. The “else” section will set the pin to low, turning the LED off.
Overall, the control logic goes like this. If digital pin 7 is high, then set digital pin 13 high, else, set digital pin 13 low.
Run the code and see what happens. Did high on digital pin 7 correspond to what you thought it would?
Now add a not block from the logic menu to the code like this:
This turns the overall logic to: If digital pin 7 is NOT high, then set digital pin 13 high, else, set digital pin low.
Upload this code and see how it behaves differently than the code without the not.
- What states can a digital signal take?
- When the button is not pressed, is digital pin 7 high or low?
- How can we check if something is not true?
- Does with or without the “not” block make the LED act like how you thought it would with the button?