[ev3dev × Python] Intelligent block button

This article is for anyone who wants to work with ev3 in Python. This time, I would like to perform various operations using the intelligent block button.

table of contents

  1. What to prepare
  2. Intelligent block button program

0. What to prepare

◯ ev3 (tank) ◯ Personal computer (VS Code) ◯ bluetooth ◯ microSD ◯ Material (It is recommended to proceed while watching this.)

1. Intelligent block button program (Document p.42)

1-0. A program that starts when a specific button is pressed

button00.py


#!/usr/bin/env python3
from ev3dev2.button import Button
from ev3dev2.sound import Sound

btn = Button()
snd = Sound()

while True:
    if btn.left:
        snd.beep()

** Point **: A program that makes a sound each time the left button is pressed

Point : ʻIf condition: Process 1` ** If the condition is true, process 1 **

** Point **: Button type ・ Up: Upper button ・ Down: Lower button ・ Right: Right button ・ Left: Left button ・ Enter: Center button ・ Backspace: Upper left button

1-1. A program that starts when you press any button

button01.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveSteering,SpeedPercent
from ev3dev2.button import Button
from ev3dev2.sound import Sound

steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)
btn = Button()
snd = Sound()

snd.beep()
while True:
    steering_drive.on(0,SpeedPercent(50))
    
    if btn.any():
        steering_drive.stop()
        exit()

** Point **: A program that stops running and stops the program when any button is pressed

Point : ʻIf condition: Process 1` ** If the condition is true, process 1 **

1-2. Programs whose processing contents differ depending on whether or not the button is pressed ①

button02.py


#!/usr/bin/env python3
from ev3dev2.button import Button
from ev3dev2.sound import Sound
from ev3dev2.display import Display

btn = Button()
snd = Sound()
dsp = Display()

while True:
    dsp.update()
    
    if btn.any():
        dsp.clear()
        snd.beep()
    else:
        dsp.text_pixels('push the button!',True,0,52,font = 'charB' + '18')

** Point **: When you press any button, you will hear a sound, otherwise you will see a statement on the screen saying press the button.

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

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

Point : clear() Reset the screen display update() Reflect the unoutput display on the screen. Without this function, it cannot be displayed on the screen at all.

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 : font↓ fonts.png

1-3. Programs whose processing contents differ depending on whether or not the button is pressed ②

button03.py


#!/usr/bin/env python3
from ev3dev2.button import Button
from ev3dev2.sound import Sound
from ev3dev2.display import Display

btn = Button()
snd = Sound()
dsp = Display()

while True:
    dsp.update()
    
    if btn.any():
        dsp.clear()
        button_name = btn.buttons_pressed
        dsp.text_pixels(str(button_name) + ' is pressed!',True,0,52,font = 'charB' + '10')
        snd.beep()
    else:
        dsp.text_pixels('push the button!',True,0,52,font = 'charB' + '18')

** Point **: When you press any button, you will hear a sound and the pressed button will be displayed, and if you do not press it, the message "Please press the button" will be displayed on the screen.

Point : buttons_pressed Returns a list of the names of the buttons being pressed

Point : str() Returns the argument (list in this case) as a character

ev3dev-2020-06-18-10-55-19.png

ev3dev-2020-06-18-10-51-08.png

1-4. Programs that do ~ while the button is not pressed

button04.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveSteering,SpeedPercent
from ev3dev2.button import Button
from ev3dev2.sound import Sound

steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)
btn = Button()
snd = Sound()

snd.beep()
while not btn.any():
    steering_drive.on(100,SpeedPercent(40))
snd.beep()
exit()   

** Point **: A program that keeps spinning on the spot until you press any button.

Point : while not ~: Process 1

** Continue process 1 while not ~ ** Program

Point : exit() End of program

1-5. A program that waits until the button is pressed / released / bumped.

button05.py


#!/usr/bin/env python3
from ev3dev2.motor import OUTPUT_A,OUTPUT_B,MoveSteering,SpeedPercent
from ev3dev2.button import Button
from ev3dev2.sound import Sound

steering_drive = MoveSteering(OUTPUT_A,OUTPUT_B)
btn = Button()
snd = Sound()

btn.wait_for_bump('enter')
steering_drive.on_for_degrees(0,SpeedPercent(60),1080)
snd.beep()
exit()   

** Point **: A program that waits until you press the center button and runs when pressed.

Point : wait_for_bump(buttons, timeout_ms=None) → Wait until the button is bumped (pressed and released). The time limit from pressing to releasing can be set with the second argument wait_for_pressed(buttons, timeout_ms=None) → Wait until pressed. wait_for_released(buttons, timeout_ms=None) → Wait until released.

Point : exit() End of program

1-6. A program that waits until the buttons are pressed at the same time.

button06.py


#!/usr/bin/env python3
from ev3dev2.button import Button
from ev3dev2.sound import Sound

btn = Button()
snd = Sound()

while True:
    btn.wait_for_pressed(['right','left','down'])
    snd.beep()

    if btn.up:
        exit()
  

** Point **: A program that makes a sound when both the right and left buttons are pressed at the same time. If the button above is pressed, the program will end.

Point : wait_for_pressed(timeout_ms=None, sleep_ms=10) → Wait until pressed. wait_for_released(timeout_ms=None, sleep_ms=10) → Wait until released. wait_for_bump(timeout_ms=None, sleep_ms=10) → Wait until the button is bumped (pressed and released). The time limit from pressing to releasing can be set with the first argument

Point : Since you can make the buttons you want to press at the same time as elements of the list, If you want to wait until you press three at the same time btn.wait_for_pressed['left','right','down']

Finally

Thank you for reading! !! Next time, I would like to write about touch sensors!

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] Intelligent block button
[ev3dev × Python] Ultrasonic sensor
[ev3dev × Python] Touch sensor
[ev3dev × Python] Gyro sensor
[ev3dev × Python] Color sensor
[ev3dev × Python] Build ev3dev development environment
[ev3dev × Python] Single motor control
[Python] The stumbling block of import
[ev3dev × Python] Display, audio, LED control