In & Out Elements

Lessons covering API and Input->process->Output covered theories on how physical computing is handled. We know about our 5 senses for inputs (or maybe you have a 6th sense?!). What elements/functions can the BrainPad can handle?

There is a list of available functionality and they are available in all coding options.

All of the elements below are used through the Out() or In() methods. Out() allows the “brain” to control an output, like a buzzer. In() allows the “brain” to read an input, like a distance sensor.

The Out() API Method always needs to know what element the brain will output to, and also what value to output. The In() method only needs what element it will read from and then it returns whatever it found.

Digital()

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.

// Make a pin go high
var pin = Digital(P3)
Out (pin,1)
// Read a pin
var pin = Digital(P1)
var state = In(pin)
// Make a pin go high
var pin = Digital(P3);
Out (pin,1);
// Read a pin
var pin = Digital(P1);
var state = In(pin);
// Make a pin go high
var pin = Digital(P3)
Out (pin,1)
// Read a pin
var pin = Digital(P1)
var state = In(pin)

Learn more about Digital()...

Analog()

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

// Turn on an LED at half power
var pin = Analog(P0)
Out (pin, 50)
// Read analog pin
var pin = Analog(P0)
// Turn on an LED at half power
var pin = Analog(P0);
Out (pin, 50);
// Read analog pin
var pin = Analog(P0);
# Turn on an LED at half power
var pin = Analog(P0)
Out (pin, 50)
# Read analog pin
var pin = Analog(P0)

Learn more about Analog()

Button()

A special method 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.

// Read the A button
var btn = Button("A", 0.1)
var value = In(btn)
// Read the A button
var btn = Button("A", 0.1);
var value = In(btn);
# Read the A button
var btn = Button("A", 0.1)
var value = In(btn)

Learn more about Button()

Sound()

Generates sounds. This function only works on PWM-capable pins. The  Sound() method requires 3 values, first the pin number you’ll be using, followed by the duration, and finally the volume level. You can also play sound using the onboard buzzer by simply replacing the pin number with the word “buzzer“. We use the Out() method to generate the sound by sending the snd module and frequency, 2000, of the sound played.

// Play Sound
var snd = Sound(2,0.1,100)
Out(snd, 2000)
// Play Sound
var snd = Sound(P2,0.1,100);
Out(snd, 2000);
# Play Sound
var snd = Sound(2,0.1,100)
Out(snd, 2000)

Learn more about Sound()

Servo()

The Servo() method is used to control a servo motor. The Servo() method can use any available pin. The Servo can be positioned between 0 to 180 degrees. We use the Out() method to send the servo module and its position.

// Move Servo
var motor = Servo(1)
Out(motor, 45)
// Move Servo
var motor = Servo(P1);
Out(motor, 45);
# Move Servo
var motor = Servo(P1)
Out(motor, 45)

Learn more about Servo()

NeoPixel()

The Neopixel() method is used to control NeoPixel leds. The first value in the method 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 vs using an array which creates one color for each led on the NeoPixel.

// NeoPixel
var neo = Neopixel(1,3)
var color = new double[3]
color[0] = 0xFFFFFF
color[1] = 0xFFFFFF
color[2] = 0xF08000
Out(neo, color)
// NeoPixel
var neo = Neopixel(1,3);
var color = new double[3];
color[0] = 0xFFFFFF;
color[1] = 0xFFFFFF;
color[2] = 0xF08000;
Out(neo, color);
# NeoPixel
var neo = Neopixel(1,3)
var color = new double[3]
color[0] = 0xFFFFFF
color[1] = 0xFFFFFF
color[2] = 0xF08000
Out(neo, color)

Learn more about Neopixel()

Accel()

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() method is used. The Accel() method requires 1 string value, “x”, “y”, or “z”. The In() method is used to send accelerometer data to a variable we can use like xValue.

// Read Accelerometer
var x = Accel("x")
var y = Accel("y")
var z = Accel("z")
var xValue = In(x)
var yValue = In(y)
var yValue = In(z)
// Read Accelerometer
var x = Accel("x");
var y = Accel("y");
var z = Accel("z");
var xValue = In(x);
var yValue = In(y);
var yValue = In(z);
# Read Accelerometer
var x = Accel("x")
var y = Accel("y")
var z = Accel("z")
var xValue = In(x)
var yValue = In(y)
var yValue = In(z)

Learn more about Accel()

Distance()

When using a distance sensor the Distance() method is used. The Distance() method requires 2 values, a trigger pin and an echo pin. The In() method is used to return the sensors value in centimeters.

// Read Distance Sensor
var dist = Distance(1,2)
var centimeters = In(dist)
// Read Distance Sensor
var dist = Distance(1,2);
var centimeters = In(dist);
// Read Distance Sensor
var dist = Distance(1,2)
var centimeters = In(dist)

Learn more about Distance()


BrainStorm

Why do some elements work with In and Out and some are In only or Out only? How come some can do both?