Input -> Process -> Output

With computers, everything is either an input that feeds info into the “brain” or an output that is controlled by the “brain”. The keyboard is an input, the monitor is an output. Can we say this is similar to our brains?Smart devices are the same way. Take a microwave oven for example, it takes user input from keyboard and has sensor to detect if the door is open. Those feed info into the processor inside. Depending on what buttons were pressed and the state of the door, the processor then controls the display, the spinning motor and heating device.

The In & Out Methods

To reflect the Input -> Process -> Output concept, all BrainPad coding options include the same In and Out methods. The API Lesson has more details on other available methods.

Let us assume the user wants to read button A. The Button is an input to the processor and so it is an In. The only thing “In” needs is to know what to actually read, which in this case is a button.

We start by creating the Button object, which we will call btn. Feel free to call it MySuperButton or BP_Button or whatever you like!

var btn = Button("A", 0)

Button object needs to know if it is “A” or “B”. Or it can be “P2” if you have a external button wired to pin P2. The second argument sets the scan time limit. More on that on in the Button lesson, for now just set to 0.

We now can check if the button is pressed. We will read the button status and hold it in a variable called state.

var state = In(btn)

While you may think computers are smart and they just know when the button is pressed with the statement above, but that is not entirely true. That statement checks the button one time and never again. Say we want to show the button state on the display/leds. We would have to repeatedly check the button and update the status in a loop. We will also add a tenth of a second (0.1) delay so we are checking the button status 10 times every second.

var btn = Button("A", 0)
var state = In(btn)
while(true)
    state = In(btn)
    print (state)
    wait (0.1)
end
var btn = Button("A",0);
var state = In(btn);
while(true) {
    state = In(btn);
    Print(state);
    Wait(0.1);
}
var btn = Button("A", 0)
var state = In(btn):
    while(true)
    state = In(btn)
    print (state)
    wait (0.1)

BrainStorm

The previous example showed the current button state on the screen. Meaning,  push = 1 and release = 0. But what if we want to change the output to toggle? Meaning, press button to make the output 1 and then press again change to 0, push-on push-off.

Hint: You need to only check if button is pressed and ignore if button is released. You do that by waiting for the button to be released.

var btn = Button(BtnA, 0)

while(true)
// wait for a button to be pressed
while(In(btn) == 0)
// do nothing!
end

print In(btn)

// wait for button to be released
while(In(btn) == 1)
// do nothing!
end

// we will do this 10 times every second
Wait(0.1)
end