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.
◯ ev3 (intelligent block, touch sensor, gyro sensor) ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)
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.
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↓
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 ↓
Note value (length of sound) ↓
3.LED
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
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
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.
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.
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.
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