DUE Buttons

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.

We did read button ‘A’ using digital, which can still be used, however, the button function has a bit more internal work to make our life easier. For example, BtnUp() will tell you when a button was released, but then it will not say a button is release again until it was first pressed.

To activate a button, we first need to enable it. This enables internal scanning of that pin. Note that not all pins support this feature. See hardware info for list of button-capable-pins.

BtnEnable('A', 1) # Enable scanning on button 'A'

We can now check if an enabled pin has BtnUp() or BtnDown() events.

BtnEnable('A',1)
@Loop
If BtnDown('A') = 1
    PrintLn("Button A down")
End

If BtnUp('A') = 1
    PrintLn("Button A up")
End
Wait(100)
Goto Loop

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).

Content Licensing
Newsletter

Twitter Feed
Hot off the press!
January 30, 2024
December 14, 2023
December 11, 2023
December 8, 2023
November 24, 2023