配線
LEDbar(27, 22, 5, 6,13, 19, 26, 21, 20, 16)
赤led (18)
緑led (23)
前button(4, hold_time=0.5)
後button (12, hold_time=0.5)
プログラム
わしは、/home/pi/projects/led_barに作った。
- cd /home/pi/projects
- mkdir led_bar
- cd led_bar
- nano flow.py
from gpiozero import LEDBoard
from gpiozero import LED
from time import sleep
from signal import pause
from gpiozero import Button
def fw():
for led in leds:
fw_p()
sleep(0.2)
def rv():
for led in reversed(leds):
rv_p()
sleep(0.2)
def fw_p():
global led_p
g_led.on()
r_led.off()
led = leds[led_p]
led.off()
led_p = led_p + 1
if led_p > len(leds)-1:
led_p = 0
led = leds[led_p]
led.on()
def rv_p():
global led_p
r_led.on()
g_led.off()
led = leds[led_p]
led.off()
led_p = led_p - 1
if led_p < 0:
led_p = len(leds)-1
led = leds[led_p]
led.on()
leds = LEDBoard(27, 22, 5, 6,13, 19, 26, 21, 20, 16)
r_led = LED(18)
g_led = LED(23)
fw_button = Button(4, hold_time=0.5)
rv_button = Button(12, hold_time=0.5)
led_p = 0
led = leds[led_p]
led.on()
fw_button.when_held = fw
rv_button.when_held = rv
fw_button.when_pressed = fw_p
rv_button.when_pressed = rv_p
pause()
ctrl+zで保存
実行
- python3 flow.py
停止
ctrl+c
動作の様子
以上!