push button Unicorn shield EN
The push Button
As you can now tell, loops are a great tool, and we can use them for way more than just blinking LEDs. Have you ever asked yourself how a computer mouse is actually working?
The operating system (including Windows, Android or often Raspbian on the Pi) is constantly checking if the mouse was moved or if there has been a mouse click. For this, “always” is a loop like the while loop..
In this chapter we discuss and work with the button component. Similar to the PC Mouse, a button is something that requires us to constantly be checking if it was pressed down. For this we will use again the while loop. You can find the push button on upper right side on one of the ears of the unicorn. You can push it and test if it works but until we have written a program for the push button nothing should have happened.
The following program will shine the world of the push button.
import unicornshield as unicorn import time while True: if unicorn.buttonPressed() == True: unicorn.leftEyeOn() unicorn.rightEyeOn() else: unicorn.leftEyeOff() unicorn.rightEyeOff()
If we have a look at the program we are seeing a new function with
unicorn.buttonPressed() which we haven’t used before. This function will give us a so called return value, this value depends on the state of push button and can tell us if the button is pressed or not. If the push button is pressed we will get the return value
True and if not the return value of
False. Please note that the first letter of this return value is in capital letters because they are not strings but booleans. A boolean only can have two values, True or False. This is really practical for using it with the push button bcause the button also has only states which are really interesting for us pressed or not pressed. A half pressed button wouldn’t be really interesting.
We know if the button is pressed because of the connection from the Unicorn Shield to the gpio pins on the Raspberry Pi. These are not only really useful for turning leds on and off but also for measuring if there is a closed electronic circuit. The push button is in simpe turns two metal plate which a seperated by a spring. If you push on the button the two plates are connected to each other and the electronic circuit is closed.
You can find the second new thing in line 9. Normally we would have a changing condition which would get checked in line 9 but in this case we simply have a
True their. That means that the while loop is always running because the condition will be always
True. This is really cool? This loop will run all day until the stop the program. By the way: You can do that with the short
Cmd + C.
Inside the while loop there is a an if condition in line 6. This condition says translated that if the return value of the function
True then we will execute the intended code.
This means that we can turn of the eyes if the push button is pressed. The next step is to turn off the eyes then the button isn’t pressed anyway. For this we have an
else: condition in line 8. The
else condition is true if all the other if condition before haven’t been true. In our case the program from line 9 to 10 will always be executed if in line 5 the return value of
buttonPressed() is not True.
It’s your turn! Can you change your program so that the left eye is turned on if the button pressed and the right eye is off and the exact opposite if the button is not pressed?