What is a push-button?
The push-button is a digital input device which is activated by pressing a finger on it, enabling the current to circulate around it, and which is deactivated when the finger is removed.
It´s a type of switch which is found in many everyday devices:
- Washing machines and other domestic appliances
The push-button is an input device from the ZUM Kit that can return two states: 1 and 0. This means: pressed and not pressed.
|The push-button will enable you to control performance in many experiments very easily. Shall we learn how to use it? You will need:
We will start pushing some buttons using different examples. Let´s get on it!
Lighting up an LED on pressing the button
Before connecting the push-button, you need to remember what type of sensor it is. Analogical or digital? The button can return two values: 1->pressed and 0->not pressed, which is why it is a digital sensor to be connected to the right of the board, as you can see in the connection diagram. But be careful! Other types can work in the exact the opposite way. This is why it´s always important to know how your electronic components work before you start to use them. For this example the connection will be like this: Now we will need to create the program so that the LED lights up when we press the button.
As you can see, in the program we have declared a variable which is called Button. In this program, it´s not necessary as we will only use the variable once. However, we will see later on how it can be interesting in longer programs to declare variables in order to save time and minimise errors. Curiosity: When executing this program, it starts with the LED switched off. When the button is pressed, the LED lights up. However, when the button is released, the LED stays switched on. Why? Because my program has not instructed the LED to switch off. In the next example we will show you how, but as we have already said, it´s important to understand that the program ONLY does what we tell it to do, and not “what we meant to tell it”. Let´s now look at how we can program the board so that the LED goes off when the button is released.
Lighting up an LED on pressing the button and turning it off on letting go
We have already turned on LED when we pressed the button. We will now make it turn off when we let go. Here´s what we need to do next:
Remember that to add more option to the if, do block, you will need to press on the star and include the new option on the block. As we have already seen, it´s rare in programming that there´s only one solution to a problem. How else could this program be done?
An alternative solution would be:
This is just one of two different ways of creating this program, but there are others which are just as valid as the previous ones.
So now you know how to use a push-button, the possibilities are infinite! What other examples of using a button can you think of?