In & Out Elements
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.
Sense and Control
We know about our 5 senses for inputs (or maybe you have a 6th sense?!). What elements/functions can the BrainPad handle?
All of the elements below are used through the Out() or In() functions. Out() allows the “brain” to control an output, like in sounding the buzzer. In() allows the “brain” to read an input, like in reading the distance sensor.
The Out() API functions always needs to know what element the brain will output to, and also what value to write. The In() function only needs what element it will read from and then it returns whatever it found.
Creates a Digital() pin element. It can be input or output. Since this is a digital (binary) value, it can only be 0 or 1.
Let us set a pin high
pin = Digital(P3) Out(pin,1)
Or read the pin to a “state” variable.
pin = Digital(P1) state = In(pin)
Analog() function creates an analog pin element. It can be input or output. Since this is an analog value, it can be any value from 0 to 100.
Below is how to turn an LED at half power. Note that any of the pins can be set to an analog output.
pin = Analog(P0) Out(pin, 50)
Some pins can also read analog values. Those pins are labeled “ANALOG” on the pinout located on the product’s page.
pin = Analog(P0) value = In(pin)
A special function is available for reading the buttons. It automatically sets a scan period. If the scan period is set to 0.1 second then In() will return true (1) if the button was pressed anytime during the last 0.1 seconds. You can use Digital() to read a button but the Button() element’s scan time come in handy.
btn = Button(ButtonA, 0.1) value = In(btn)
Generates sounds. This function only works on PWM-capable pins. Tose pins are marked with ~ symbol on the pinout on the product’s page. The Sound() function requires 3 values, first the pin number you’ll be using, followed by the duration, and finally the volume level. Duration of 0.1 will play the sound for 0.1 second; however, a duration of 0 means play the sound forever, till another Sound() Out() is called. the volume level is 0 to 100.
You can also play sound using the onboard buzzer (if available) by simply replacing the pin number with the word Buzzer.
Here we use the Out() method to generate the sound by sending the snd element and frequency of 2000hz to the buzzer.
speaker = Sound(Buzzer,0.1,50) Out(speaker, 2000)
The Servo() function is used to control a servo motor. The Servo() function can use any available pin. The Servo can be positioned between 0 to 180 degrees. We use the Out() function to send the servo module and its position, like 45 degrees for example.
motor = Servo(P1) Out(motor, 45);
The Neopixel() function is used to control NeoPixel leds. The first value in the function is the pin number, the second value is the number of leds. We can send different colors to each led by using an array of color which holds the hex color value for each color desired. If you want all the LEDs to be same color you can just send a single hex color value.
Lets assume there are 3 LEDs on pin P1 and we want to set them to Red.
neo = Neopixel(P1,3) Out(neo, 0xFF0000)
If the BrainPad being used has an onboard accelerometer we can read the x, y, and z values from it, to do this the Accel() function is used. The Accel() method requires 1 argument, AccelX, AccelY, or AccelZ. The In() function is used to send accelerometer data to a variable we can use like xValue.
x = Accel(AccelX) xValue = In(x)
When using a distance sensor the Distance() function is used. The Distance() function requires 2 pins, a trigger pin and an echo pin. The In() function is used to return the sensors value in centimeters.
dist = Distance(P1,P2) centimeters = In(dist)
The Touch() function uses something called capacitive touch. It requires 2 arguments. First the pin we want to use for touching and second the sensitivity of that pin. When we touch the selected pin it returns 1. When not touched the value returned is 0.
touch = Touch(P0,50) t = In(touch)
The Infrared() function requires 1 argument. Which pin an IR receiver will be connected to. The IR device will return a value we can use in our code.
remote = Infrared(P0) value = In(remote)
The I2cBus() function creates an I2C bus. It takes one argument which is the bus address, a 7-bit number. An array named ‘dataOut’ is created to hold values which are sent to the I2C bus using the Out() function. The In() function is used to retrieve a single byte from the I2C bus. The I2cBus() function also uses the special OutIn() function.
bus = new I2cBus(0x4C) dataOut = new double dataOut = 0x20 dataOut = 0x01 Out(bus,dataOut) id = In(bus)
Why do some elements work with In() and Out() and some are In() only or Out() only? How come some can do both?