Buttons
Most electronic devices use buttons. The keyboard for a computer is nothing more than a bunch of buttons, with each performing a different task. The buttons on the BrainPad can be programmed to perform tasks within the code we create. Let’s demonstrate how to use the buttons on the BrainPad to turn the LED ON and OFF. We’ll also use the screen to tell us which button was pressed.
Button()
Now we’ll use the Button() function to create our A and B buttons. The Button() function requires 2 arguments. The first argument relates to which button to use. The second argument sets the scan time limit; we’ll explain more about that in a moment and for this first code example we can set it to 0.
Button(pin,scanTime)
We use “A” or “B” depending on the button we want to use. In this case we’ll use both.
var btnA = Button(ButtonA,0);
var btnB = Button(ButtonB,0);
btnA = Button(ButtonA, 0)
btnB = Button(ButtonB, 0)
Now that the btnA & btnB elements (objects) have been created, they can now be used to print A or B on the screen. And to slow things down, we will add a Wait(0.1) so that we are doing this 10 times a second. We’ll also add code to turn the LED on when A is pressed and off when B is pressed.
var led = Digital(LED);
var btnA = Button(ButtonA,0);
var btnB = Button(ButtonB,0);
while(true){
if(In(btnA) == 1){
Print("A");
Out(led,1);
}
if(In(btnB) == 1){
Print("B");
Out(led,0);
}
Wait(0.1);
}
led = Digital(LED)
btnA = Button(ButtonA, 0)
btnB = Button(ButtonB, 0)
while True:
if(In(btnA) == 1):
Print("A")
Out(led,1)
if(In(btnB) == 1):
Print("B")
Out(led,0)
Wait(0.1)
You will notice that a quick press on the button may not be detected. This is because you pressed and released the button before In() got a chance to see it, especially that we check 10 times a second. The solution for that is found in Scan Time, which is explained next.
Scan Time
When the computer checks if a button is pressed, it is checking the current state at that very moment. For the sake of explaining this, let us assume the computer is busy processing something and it is only able to check the button press state once a second. This means the user may press and release the button, but the computer may not see it because by the time it checked for a button press, the user had already released the button. Yes, the button was pressed at some point, but it is not pressed now. The solution is by asking the user to press the button for a long time, until the computer finally checks the button state. The BrainPad solves this by giving you a peek into the past (there is a time machine built in!) and letting you select how long into the past should the button read go into. If you set it to one second, then a button read using In() will return true if the button was pressed now or anytime in the past second.
A reasonable scan time is 50ms, but you can change this to fit your needs.
var btnA = Button(ButtonA, 0.050);
btnA = Button(ButtonA, 0.050)
BrainStorm
How does the BrainPad know a button is pressed? A button is a mechanical switch that connects a pin to ground. A pin is either left unconnected to anything when a button is not pressed, and then when the button is pressed a connection is made between a pin and ground (zero voltage). A pin that is connected to ground will result in 0 always, but what is the voltage on a pin that is not connected? You may think it will be zero, but that is not true as the surrounding electrical noise may give the pin enough charge to make it a 1 or a 0.
The solution for this is easy and found in most processors. The pin used usually has a “pull up” feature. This pull up tries to pull the pin to a charged state when not connected to keep it reading a 1. This is not making the pin high but only trying to pull it high. If nothing is connected to the pin, then the pin will read 1 (high – on). But then the button creates a connection between that particular pin and ground (zero), which forces the pin to be 0 (low – off).