Sample programs
Some sample programs for your convenience. Feel free to copy paste, use or edit however you need. Play with it, explore. You can also download a sample SD card with these programs and a few more here.
Read EV3 touch sensor value in a loop
import brian.sensors as sensors
import time
print("Opening S1 as a touch sensor")
touch = sensors.EV3.TouchSensorEV3(sensors.SensorPort.S1)
counter = 0
while True:
status = "disconnected"
if touch.is_ready():
if touch.is_pressed():
status = "pressed"
else:
status = "released"
print(f"{counter}: [S1] is {status}")
time.sleep(1)
counter = counter + 1
Rotate motor there and back
import brian.motors as motors
import time
print("Using A as EV3 large motor")
large = motors.EV3LargeMotor(motors.MotorPort.A)
while True:
print("CW[A]...")
large.rotate_by_angle(90, 200)
print("Wait...")
time.sleep(1)
print("CCW[A]...")
large.rotate_by_angle(-90, 200)
print("Wait...")
time.sleep(1)
Motor as a spring
Import multiple files
Read status and type of connected sensor to the port
import brian.sensors as sensors
import brian.sensors.sensor_port_probe as port_probe
import time
print("probe testing on port S1")
while True:
probe=port_probe.probe_sensor_with_autodetect_hint(sensors.SensorPort.S1, port_probe.AutoDetect.PROTOCOL_UART_EV3)
print("is connected ", probe.is_connected)
print(probe.auto_detect)
print("sensor type name ", probe.info.sensor_type_name)
time.sleep(1)
Use builtin LED buttons
from brian.uicontrol import *
import time
color = LedColor(0, 128, 255)
knob_color = LedColor(0, 255, 0)
def sequence(i):
return [LedButtonAnimation.SELECTED, LedButtonAnimation.SELECTABLE, LedButtonAnimation.STANDBY, LedButtonAnimation.OFF][i % 4]
i = 0
while True:
set_button_led(ButtonId.TOP_LEFT, sequence(i), color)
set_button_led(ButtonId.TOP_RIGHT, sequence(i - 1), color)
set_button_led(ButtonId.BOTTOM_RIGHT, sequence(i - 2), color)
set_button_led(ButtonId.BOTTOM_LEFT, sequence(i - 3), color)
set_button_led(ButtonId.KNOB, sequence(i), knob_color)
i = i+1
time.sleep(0.5)
Use buttons
import sys
from brian.uicontrol import *
import time
print("Turn off by pressing the knob")
color = LedColor(0, 128, 255)
color_pressed = LedColor(0, 255, 0)
color_released = LedColor(255, 0, 0)
listener = UiEventsListener()
listener.enable_turn_off_by_buttons(False)
set_button_led(ButtonId.TOP_LEFT, LedButtonAnimation.STANDBY)
set_button_led(ButtonId.TOP_RIGHT, LedButtonAnimation.STANDBY)
set_button_led(ButtonId.BOTTOM_RIGHT, LedButtonAnimation.STANDBY)
set_button_led(ButtonId.BOTTOM_LEFT, LedButtonAnimation.STANDBY)
while True:
knob = listener.knob_event_since_last()
btns = listener.buttons_event_since_last()
if knob.turn_delta > 0:
set_button_led(ButtonId.TOP_LEFT, LedButtonAnimation.STANDBY, color)
set_button_led(ButtonId.TOP_RIGHT, LedButtonAnimation.SELECTED, color)
set_button_led(ButtonId.BOTTOM_RIGHT, LedButtonAnimation.SELECTED, color)
set_button_led(ButtonId.BOTTOM_LEFT, LedButtonAnimation.STANDBY, color)
elif knob.turn_delta < 0:
set_button_led(ButtonId.TOP_LEFT, LedButtonAnimation.SELECTED, color)
set_button_led(ButtonId.TOP_RIGHT, LedButtonAnimation.STANDBY, color)
set_button_led(ButtonId.BOTTOM_RIGHT, LedButtonAnimation.STANDBY, color)
set_button_led(ButtonId.BOTTOM_LEFT, LedButtonAnimation.SELECTED, color)
if btns.top_left.just_pressed:
set_button_led(ButtonId.TOP_LEFT, LedButtonAnimation.SELECTED, color_pressed)
elif btns.top_left.just_released:
set_button_led(ButtonId.TOP_LEFT, LedButtonAnimation.SELECTED, color_released)
if btns.top_right.just_pressed:
set_button_led(ButtonId.TOP_RIGHT, LedButtonAnimation.SELECTED, color_pressed)
elif btns.top_right.just_released:
set_button_led(ButtonId.TOP_RIGHT, LedButtonAnimation.SELECTED, color_released)
if btns.bottom_left.just_pressed:
set_button_led(ButtonId.BOTTOM_LEFT, LedButtonAnimation.SELECTED, color_pressed)
elif btns.bottom_left.just_released:
set_button_led(ButtonId.BOTTOM_LEFT, LedButtonAnimation.SELECTED, color_released)
if btns.bottom_right.just_pressed:
set_button_led(ButtonId.BOTTOM_RIGHT, LedButtonAnimation.SELECTED, color_pressed)
elif btns.bottom_right.just_released:
set_button_led(ButtonId.BOTTOM_RIGHT, LedButtonAnimation.SELECTED, color_released)
if knob.just_pressed:
set_button_led(ButtonId.KNOB, LedButtonAnimation.SELECTED, color)
elif knob.just_released:
sys.exit()
time.sleep(0.05)
Compose a song from tones
import brian.audio as audio
import time
C4 = 261.63
C4_plus = 277.18
D4_minus = C4_plus
D4 = 293.66
D4_plus = 311.13
E4_minus = D4_plus
E4 = 329.63
F4 = 349.23
F4_plus = 369.99
G4_minus = F4_plus
G4 = 392.00
G4_plus = 415.30
A4_minus = G4_plus
A4 = 440.00
A4_plus = 466.16
B4_minus = A4_plus
B4 = 493.88
C5 = 523.25
C5_plus = 554.37
D5_minus = C5_plus
D5 = 587.33
D5_plus = 622.25
E5_minus = D5_plus
E5 = 659.26
F5 = 698.46
F5_plus = 739.99
G5_minus = F5_plus
G5 = 783.99
G5_plus = 830.61
A5_minus = G5_plus
A5 = 880.00
A5_plus = 932.33
B5_minus = A5_plus
B5 = 987.77
def play(freq, duration):
"""
:param freq: note freq Hz
:param duration: duration in s
:return:
"""
audio.play_tone(round(freq), round(duration * 1000)) # hz, ms
time.sleep(duration)
bpm = 45
f = 60 / bpm / 1
h = 60 / bpm / 2
q = 60 / bpm / 4
s = 60 / bpm / 8
time.sleep(1)
def bbcd():
play(B4, q)
play(B4, q)
play(C5, q)
play(D5, q)
def dcba():
play(D5, q)
play(C5, q)
play(B4, q)
play(A4, q)
def ggab():
play(G4, q)
play(G4, q)
play(A4, q)
play(B4, q)
def baa():
play(B4, q + s)
play(A4, s)
play(A4, h)
def agg():
play(A4, q + s)
play(G4, s)
play(G4, h)
def aabg():
play(A4, q)
play(A4, q)
play(B4, q)
play(G4, q)
def abcbg():
play(A4, q)
play(B4, s)
play(C5, s)
play(B4, q)
play(G4, q)
def abcba():
play(A4, q)
play(B4, s)
play(C5, s)
play(B4, q)
play(A4, q)
def gad():
play(G4, q)
play(A4, q)
play(D4, h)
# --- start ---
bbcd()
dcba()
ggab()
baa()
bbcd()
dcba()
ggab()
agg()
aabg()
abcbg()
abcba()
gad()
bbcd()
dcba()
ggab()
agg()