DUE 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?
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.
In & Out Methods
Functions reflect the Input -> Process -> Output concept. Some functions control things, like move a robot, using Move(), others are input methods, like Sense().
Let’s assume the user wants to read button A. As you can imagine, a button is an input to the processor. And then we need want to control the LED. The LED is an output from the processor.
Let us check if a button is pressed to flash the LED 3 times. Button and LED functions will be detailed in future lessons.
BtnEnable('A', 1) @Loop x=BtnDown('A') If x=1 Led(200,200,3) End Wait(100) Goto Loop
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?
You can now start wiring the BrainPad to the physical world and experiment with available elements.
The previous example used a loop that constantly checked for the button press and if the LED was pressed, it continued to blink the LED. What if we want to flash the LED 3 times with a press and not flash again until a button is released? Read the previous sentence again. We specifically said “not flash again until” and computers are black and white and follow ever instruction literally.
How do you do it? 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?!
BtnEnable('A', 1) @Loop If BtnDown('A')=1 Led(200,200,3) @hold: If BtnDown('A')=1: Goto hold: End End Wait(200) Goto Loop