[ev3dev×Pythhon] 스마트 블록 버튼
이번에 나는 스마트 블록 버튼을 사용하여 각종 조작을 하고 싶다.
카탈로그
0 . 준비물
1 . 지능 블록 단추 프로그램
준비물
◯ ev3 (캔)
컴퓨터(VScode)
◯ bluetooth
◯ microSD
◯ 자료. (이걸 보면서 진행하는 걸 추천합니다.)
1. 스마트 블록 버튼 프로그램(자료p.42)
1-0 . 특정 버튼을 누른 후 진행하는 프로그램
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: 왼쪽 버튼을 누를 때마다 소리가 나는 프로그램
Point : if 条件:
処理1
만약 조건이 사실이라면 1을 처리하는 프로그램
Point:버튼 유형
・up: 위 단추
・down: 아래 단추
・ right: 우클릭
・left: 좌클릭
/enter: 중앙 단추
・백스페이스: 왼쪽 위 단추
1-1 . 버튼 하나만 누르면 시작하는 프로그램.
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: 버튼을 누른 후 실행을 중지하고 프로그램을 중지하는 프로그램
Point : if 条件:
処理1
만약 조건이 사실이라면 1을 처리하는 프로그램
1-2 . 버튼 처리 여부에 따라 내용이 다른 프로그램 ①
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: 어떤 단추를 누르면 소리가 납니다. 누르지 않으면 버튼을 누르십시오. 이런 문장이 화면에 나타납니다.
Point : if 条件:
処理1
else:
処理2
만약 조건이 사실이라면 처리를 진행한다.
가령
이런 프로그램
Point :
clear()
화면 표시 재설정
update()
화면에 출력되지 않은 디스플레이를 반영합니다.
만약 이 함수가 없다면 화면에 모든 것을 표시할 수 없다.
Point : text_pixels(text, clear_screen=True, x=0, y=0, text_color=’black’, font=None)
텍스트는 pixel 단위의 좌표(x, y)에서 시작합니다.
ev3의 화면은 178 x 128 pixels입니다.
• 화면의 왼쪽 상단은(0,0)
• 화면 중앙은 (8964)
※ y 좌표는 아래로 이동하면서 숫자가 커집니다.
Point : font↓
1-3 . 버튼으로 내용을 처리하는지 여부에 따라 ②
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: 어떤 단추를 누르면 소리가 납니다. 누르는 단추를 표시합니다. 누르지 않으면 버튼을 누르십시오. 이런 문장이 화면에 나타납니다.
Point : buttons_pressed
버튼 이름 목록으로 돌아가기
Point : str()
매개변수(이 경우 목록)를 문자로 변환
1-4 . 버튼 안 누르는 동안 하는 거예요.
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: 버튼을 누르기 전에 제자리에서 계속 회전하는 프로그램입니다.
Point : while not ~:
処理1
이 아닌 동안 계속 처리하다
이런 프로그램
Point : exit()
프로그램 종료
1-5 . 버튼 [누르기/놓기/풀기]까지 대기하는 프로그램
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: 가운데 버튼을 누르기 전에 대기하고 눌린 후에 실행되는 프로그램입니다.
Point :
wait_for_bump(buttons, timeout_ms=None)
→버튼이 풀릴 때까지 기다린다.
두 번째 파라미터로 누르고 떠나는 시간 제한을 설정할 수 있습니다
wait_for_pressed(buttons, timeout_ms=None)
→눌릴 때까지 기다린다.
wait_for_released(buttons, timeout_ms=None)
→헤어질 때까지 기다린다.
Point : exit()
프로그램 종료
1-6 . 버튼이 동시에 눌릴 때까지 기다리는 프로그램
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: 사운드를 내는 프로그램을 마우스 오른쪽 버튼과 왼쪽 버튼으로 동시에 누릅니다.
위의 버튼을 누르면 프로그램이 끝납니다.
Point :
wait_for_pressed(timeout_ms=None, sleep_ms=10)
→눌릴 때까지 기다린다.
wait_for_released(timeout_ms=None, sleep_ms=10)
→헤어질 때까지 기다린다.
wait_for_bump(timeout_ms=None, sleep_ms=10)
→버튼이 풀릴 때까지 기다린다.
첫 번째 파라미터로 누르고 떠나는 시간 제한을 설정할 수 있습니다
Point :
동시에 누르고 싶은 단추를 목록의 요소로 삼으면 된다
동시에 3개를 누르기 전에 기다리는 경우.btn.wait_for_pressed['left','right','down']
최후
읽어주셔서 감사합니다!!
다음에는 터치 센서를 쓰고 싶어요!
더 좋은 기사를 하고 싶어서.
이렇게 하면 더욱 이해하기 쉽다
여기 이해하기 어렵다.
○여기가 틀렸다
여기
등의 의견을 제시해 주십시오.
Reference
이 문제에 관하여([ev3dev×Pythhon] 스마트 블록 버튼), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/masterkeaton12/items/223218434745cd770045
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
◯ ev3 (캔)
컴퓨터(VScode)
◯ bluetooth
◯ microSD
◯ 자료. (이걸 보면서 진행하는 걸 추천합니다.)
1. 스마트 블록 버튼 프로그램(자료p.42)
1-0 . 특정 버튼을 누른 후 진행하는 프로그램
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: 왼쪽 버튼을 누를 때마다 소리가 나는 프로그램
Point : if 条件:
処理1
만약 조건이 사실이라면 1을 처리하는 프로그램
Point:버튼 유형
・up: 위 단추
・down: 아래 단추
・ right: 우클릭
・left: 좌클릭
/enter: 중앙 단추
・백스페이스: 왼쪽 위 단추
1-1 . 버튼 하나만 누르면 시작하는 프로그램.
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: 버튼을 누른 후 실행을 중지하고 프로그램을 중지하는 프로그램
Point : if 条件:
処理1
만약 조건이 사실이라면 1을 처리하는 프로그램
1-2 . 버튼 처리 여부에 따라 내용이 다른 프로그램 ①
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: 어떤 단추를 누르면 소리가 납니다. 누르지 않으면 버튼을 누르십시오. 이런 문장이 화면에 나타납니다.
Point : if 条件:
処理1
else:
処理2
만약 조건이 사실이라면 처리를 진행한다.
가령
이런 프로그램
Point :
clear()
화면 표시 재설정
update()
화면에 출력되지 않은 디스플레이를 반영합니다.
만약 이 함수가 없다면 화면에 모든 것을 표시할 수 없다.
Point : text_pixels(text, clear_screen=True, x=0, y=0, text_color=’black’, font=None)
텍스트는 pixel 단위의 좌표(x, y)에서 시작합니다.
ev3의 화면은 178 x 128 pixels입니다.
• 화면의 왼쪽 상단은(0,0)
• 화면 중앙은 (8964)
※ y 좌표는 아래로 이동하면서 숫자가 커집니다.
Point : font↓
1-3 . 버튼으로 내용을 처리하는지 여부에 따라 ②
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: 어떤 단추를 누르면 소리가 납니다. 누르는 단추를 표시합니다. 누르지 않으면 버튼을 누르십시오. 이런 문장이 화면에 나타납니다.
Point : buttons_pressed
버튼 이름 목록으로 돌아가기
Point : str()
매개변수(이 경우 목록)를 문자로 변환
1-4 . 버튼 안 누르는 동안 하는 거예요.
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: 버튼을 누르기 전에 제자리에서 계속 회전하는 프로그램입니다.
Point : while not ~:
処理1
이 아닌 동안 계속 처리하다
이런 프로그램
Point : exit()
프로그램 종료
1-5 . 버튼 [누르기/놓기/풀기]까지 대기하는 프로그램
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: 가운데 버튼을 누르기 전에 대기하고 눌린 후에 실행되는 프로그램입니다.
Point :
wait_for_bump(buttons, timeout_ms=None)
→버튼이 풀릴 때까지 기다린다.
두 번째 파라미터로 누르고 떠나는 시간 제한을 설정할 수 있습니다
wait_for_pressed(buttons, timeout_ms=None)
→눌릴 때까지 기다린다.
wait_for_released(buttons, timeout_ms=None)
→헤어질 때까지 기다린다.
Point : exit()
프로그램 종료
1-6 . 버튼이 동시에 눌릴 때까지 기다리는 프로그램
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: 사운드를 내는 프로그램을 마우스 오른쪽 버튼과 왼쪽 버튼으로 동시에 누릅니다.
위의 버튼을 누르면 프로그램이 끝납니다.
Point :
wait_for_pressed(timeout_ms=None, sleep_ms=10)
→눌릴 때까지 기다린다.
wait_for_released(timeout_ms=None, sleep_ms=10)
→헤어질 때까지 기다린다.
wait_for_bump(timeout_ms=None, sleep_ms=10)
→버튼이 풀릴 때까지 기다린다.
첫 번째 파라미터로 누르고 떠나는 시간 제한을 설정할 수 있습니다
Point :
동시에 누르고 싶은 단추를 목록의 요소로 삼으면 된다
동시에 3개를 누르기 전에 기다리는 경우.btn.wait_for_pressed['left','right','down']
최후
읽어주셔서 감사합니다!!
다음에는 터치 센서를 쓰고 싶어요!
더 좋은 기사를 하고 싶어서.
이렇게 하면 더욱 이해하기 쉽다
여기 이해하기 어렵다.
○여기가 틀렸다
여기
등의 의견을 제시해 주십시오.
Reference
이 문제에 관하여([ev3dev×Pythhon] 스마트 블록 버튼), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/masterkeaton12/items/223218434745cd770045
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
#!/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()
処理1
#!/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()
処理1
#!/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')
処理1
else:
処理2
#!/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')
#!/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()
処理1
#!/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()
#!/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()
읽어주셔서 감사합니다!!
다음에는 터치 센서를 쓰고 싶어요!
더 좋은 기사를 하고 싶어서.
이렇게 하면 더욱 이해하기 쉽다
여기 이해하기 어렵다.
○여기가 틀렸다
여기
등의 의견을 제시해 주십시오.
Reference
이 문제에 관하여([ev3dev×Pythhon] 스마트 블록 버튼), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/masterkeaton12/items/223218434745cd770045텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)