Brain#

Initializing the Brain Class#

To use the EXP Brain in your project, start by initializing it as follows:

# Create a new object "brain" with the Brain class.
brain = Brain()

This brain object will be used in all subsequent examples throughout this API documentation when referring to Brain class methods and attributes.

The Brain class comes with six pre-defined attributes:

  • battery - Provides information about the EXP Brain’s battery, including capacity, temperature, voltage, and current.

  • button - Allows you to detect button presses and assign functions to button events on the EXP Brain.

  • timer - Provides functionality to measure elapsed time and assign functions to time-based events on the EXP Brain.

  • screen - Enables you to display text and draw images on the EXP Brain’s screen.

  • sdcard - Provides access to files stored on the EXP Brain’s SD card.

  • three_wire_port - Provides access to 3-Wire Sensors connected to the EXP Brain’s 3-Wire Ports. Each port is identified by a letter (a through h) and can be accessed using the format brain.three_wire_port.x, where x is the port letter. For example, brain.three_wire_port.a refers to the Sensor connected to port A.

Class Methods#

In addition to its attributes, the Brain class provides several methods that can be used directly.

play_sound()#

The brain.play_sound(sound, volume) command plays a sound on the Brain.

Parameters

Description

sound

A valid SoundType.

volume

Optional. The sound volume, maximum is 100.

Returns: None.

# Play a sound of type TADA.
brain.play_sound(SoundType.TADA)

play_note()#

The brain.play_note(octave, note, duration, volume) command plays a note on the Brain.

Parameters

Description

octave

The octave to use.

note

The note to play.

duration

Optional. The duration in milliseconds to play the note for.

volume

Optional. The sound volume. The maximum is 100.

Returns: None.

# Play a note on the Brain.
brain.play_note(1, 4, 200, 50)

play_file()#

The brain.play_file(filename, volume) command plays a sound file in .wav format on the Brain.

Parameters

Description

filename

The sound file to play.

volume

Optional. The sound volume, maximum is 100.

Returns: None.

# Play the sound file titled "myfile.wav" on the Brain.
brain.play_file('myfile.wav', 50)

sound_off()#

The brain.sound_off() command stops any sound that is playing.

Returns: None.

# Turn off the sound on the Brain.
brain.sound_off()

sound_is_active()#

The brain.sound_is_active() command checks whether a sound is playing.

Returns: True if a sound is playing. False if a sound is not playing.

sound_duration()#

The brain.sound_duration() command gets the total duration of the currently playing sound.

Returns: The duration in milliseconds.

sound_remaining()#

The brain.sound_remaining() command gets the remaining duration of the currently playing sound.

Returns: The duration in milliseconds.