[ev3dev × Python] Display, audio, LED control

This article is for anyone who wants to work with ev3 in Python. This time, I would like to use intelligent blocks to display text and numerical values, play voice, and control LEDs.

table of contents

  1. What to prepare
  2. Display
  3. Voice 3 . LED
  4. Program example

0. What to prepare

◯ ev3 (intelligent block, touch sensor, gyro sensor) ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)

1. Display

1-0. Basic display program ①

display00.py


#!/usr/bin/env python3
from time import sleep

print("HELLO WORLD")
sleep(3)

** Point **: A program that displays small letters on an intelligent block for 3 seconds. The smallest but simplest program.

Point : print() Print strings, numbers and variable values

** Point **: For print (), this article is easy to understand.

** Point **: When programmed with MINDSTORMS, it looks like below. スクリーンショット 2020-06-16 18.28.52.png

1-1. Basic program of display ② (Document p.52)

display01.py


#!/usr/bin/env python3
from ev3dev2.display import Display 
from time import sleep

dsp = Display()
while True:
    dsp.update()
    dsp.text_pixels('HELLO WORLD',True,0,52,font = 'courB' + '24')

** Point **: A program that displays characters on intelligent blocks all the time.

Point : text_pixels(text, clear_screen=True, x=0, y=0, text_color=’black’, font=None) The text starts at the coordinates (x, y) in pixels. The screen of ev3 is 178x128 pixels, • The upper left corner of the screen is (0, 0) • The center of the screen is (89, 64)

Point : update() Applies pending changes to the screen. Nothing will be drawn on the screen until this function is called.

Point : font↓ fonts.png

2. Voice

2-0. Basic speech program (Document p.46)

sound00.py


#!/usr/bin/env python3
from ev3dev2.sound import Sound
spkr = Sound()

spkr.beep()
spkr.speak('lets start STAR WARS music')

spkr.play_song((
('D4', 'e3'),
('D4', 'e3'),
('D4', 'e3'),
('G4', 'h'),
('D5', 'h')
))

** Point **: A program that plays a melody by speaking a word after making a beep.

** Point **: play_song ('scale','note value')

Scale ↓ DqTLAYFV4AAsKFO.jpg

Note value (length of sound) ↓ 184978921.jpg

3.LED

3-0. Basic LED program (Document p.43)

led00.py


#!/usr/bin/env python3
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from time import sleep

led = Leds()
snd = Sound()

for i in (3):
    led.set_color("LEFT","AMBER")
    led.set_color("RIGHT","AMBER")
    sleep(1)
    led.all_off()
    sleep(1)
    snd.beep()

Point : set_color(group, color, pct=1)  group   • LEFT   • RIGHT

color   • BLACK   • RED   • GREEN   • AMBER   • ORANGE   • YELLOW

4. Program example

4-0. Program that keeps displaying the value of the gyro sensor

example00.py


#!/usr/bin/env python3
from ev3dev2.display import Display
from ev3dev2.sensor.lego import GyroSensor
from time import sleep

dsp = Display()
gy = GyroSensor()

while True:
    dsp.update()
    num = gy.angle
    dsp.text_pixels(str(num) + ' degrees',True,0,52,font = 'helvB' + '24')
    sleep(0.5)

** Point **: See page 36 of the document for the gyro sensor!

** Point **: str () is a function that converts arguments to characters

4-1. A program that changes the color by pressing the touch sensor

example01.py


#!/usr/bin/env python3
from ev3dev2.sensor import INPUT_1
from ev3dev2.sensor.lego import TouchSensor
from ev3dev2.led import Leds

ts = TouchSensor(INPUT_1)
leds = Leds()

print("Press the touch sensor to change the LED color!")
while True:
    if ts.is_pressed:
        leds.set_color("LEFT", "GREEN")
        leds.set_color("RIGHT", "GREEN")
    else:
        leds.set_color("LEFT", "RED")
        leds.set_color("RIGHT", "RED")

** Point **: A program that turns the touch sensor green when pressed and red when it is not pressed

** Point **: See p.33 of the document for touch sensors!

Point : is_pressed A boolean indicating whether the current touch sensor is being pressed.

Point : ʻIf condition: Process 1 else: Process 2`

** If the condition is true, do process 1, If false, process 2 ** Program

Point : INPUT_1~4 The sensor is used to ** get ** some value, Since the motor is used for ** output **, Each port is indicated by INPUT / OUTPUT.

** Point **: When programmed with MINDSTORMS, it looks like below. スクリーンショット 2020-06-17 8.39.02.png

4-2. A program that changes colors using a list

example02.py


#!/usr/bin/env python3
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from time import sleep

led = Leds()
snd = Sound()
color_list = ["BLACK","RED","GREEN","AMBER","ORANGE","YELLOW"]

for i in color_list:
    print(i)
    led.set_color("LEFT",i)
    led.set_color("RIGHT",i)
    sleep(1)
    led.all_off()
    sleep(1)
    snd.beep()

** Point **: Takes characters in order from the top of the list and makes them the color of the LED

Point : sleep() Function to wait for the number of seconds specified by the argument

** Point **: For the list, this article is easy to understand.

4-3. A program that changes colors by combining lists and random functions

example03.py


#!/usr/bin/env python3
from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from time import sleep
import random

led = Leds()
snd = Sound()
color_list = ["BLACK","RED","GREEN","AMBER","ORANGE","YELLOW"]

for i in color_list:
    print(c)
    c = random.choice(color_list)
    led.set_color("LEFT",c)
    led.set_color("RIGHT",c)
    sleep(1)
    led.all_off()
    sleep(1)
    snd.beep()

** Point **: Randomly extract characters from the list and make them LED colors, repeating as many times as there are elements in the list.

Point : random.choice() It takes an object with multiple elements such as a list or a character string as an argument, and randomly returns (outputs) one element from it.

** Point **: For randomness, this article is also easy to understand.

Finally

Thank you for reading! !! Next time, I would like to write about intelligent block buttons!

I want to make a better article ◯ This is easier to understand ◯ This is difficult to understand ◯ This is wrong ◯ I want you to explain more here We appreciate your opinions and suggestions.

Recommended Posts

[ev3dev × Python] Display, audio, LED control
[ev3dev × Python] Single motor control
[ev3dev × Python] Control of multiple motors
Waveform display of audio in Python
[ev3dev × Python] SSH Control (remote control with keyboard)
Install Python Control
[ev3dev × Python] Ultrasonic sensor
[ev3dev × Python] Touch sensor
Python audio input / output
[ev3dev × Python] Gyro sensor
Python control syntax (memories)
[ev3dev × Python] Color sensor
[Ev3dev] How to display bmp image on LCD with python
[ev3dev × Python] Intelligent block button
[Python tutorial] Control structure tool
Display UTM-30LX data in Python
Instrument control using Python [pyvisa]