Input -> Process -> Output

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?

This page covers the In() and Out() functions available in the API.

Prerequisite

This lesson assumes you understand the language basics of your choice, you can create projects and you have started with the API Lesson first. Like the API Lesson, this is a generic lesson meant to work with different languages. Don’t forget to add the C# semicolons, for example.

Devices Input and Output

Just like computers, smart devices have inputs and outputs. Take a microwave oven for example, it takes user input from the keyboard and has a 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, spinning the 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. These methods are in (read) or out (write) any of the available elements. The In & Out Element Lesson lists all available elements.

Let’s assume the user wants to read button A. The Button() element is one of the available options. As you can imagine, a button is an input to the processor, and so it needs an In(). The only thing In() needs is to know what to actually read, which in this case is a Button() element.

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

var btn = Button(ButtonA,0);
btn = Button(ButtonA, 0)

The Button() object needs to know what button to use, ButtonA or ButtonB. Or it can be P2 if you have an external button wired to pin P2. The second argument sets the scan time limit. More on that 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);
state = In(btn)

While you may think computers are smart and they just know when the button is pressed with the statement above, 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. But how many times a second will we check the buttons? 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(ButtonA,0);
while (true){ 
    state = In(btn);
    Print(state);
    Wait(0.1);
}
btn = Button(ButtonA, 0)
while True:
    state = In(btn)
    Print (state)
    Wait (0.1)
 

What’s Next?

You can now start wiring the BrainPad to the physical world and experiment with available elements. They are all covered in the In & Out Elements Lesson.


BrainStorm

The previous example showed the current button state on the screen repeatedly. But what if we want to show it only once with every press?

Hint: You need to wait for the button to be released before looping and checking for press again.  Observe the code… do you still think computers are smart?!

var btn = Button(ButtonA,0);
var state = In(btn);
while(true){
    state = In(btn);
    if(In(btn) == 1){
        Print(state);
        while(In(btn) == 1){
            // do nothing and wait for 
            // release
        }
    Wait(0.1);
    }
}          
btn = Button(ButtonA,0)
while True:
    state = In(btn)
    if(In(btn) == 1)
        Print(state)
        while(In(btn) == 1)
            # do nothing and wait for     
            # release
    Wait(0.1)