python gpiozero_Python gpiozero 模块,LED 实例源码 - 编程字典

本文展示了如何使用python gpiozero模块创建交通灯控制系统。包括启动绿色、稳定琥珀色、红色、行人行走和禁止行走等不同状态的实现,以及完整的交通灯序列操作。代码中包含了一系列函数,用于控制LED灯和蜂鸣器的工作状态,根据按钮输入进行相应操作。
摘要由CSDN通过智能技术生成

def startgreen():

# Remember all code in the function is indented

# Turn the green off and the amber on for 3 seconds

# ('Pedestrian' red LED stays lit)

def steadyamber():

# Remember all code in the function is indented

# Turn the amber off, and then the red on for 1 second

def steadyred():

# Remember all code in the function is indented

# Sound the buzzer for 4 seconds

# (If you have the 'pedestrian' LEDs, turn the red off and green on)

def startwalking():

# Make the buzzer buzz on and off, half a second of

# sound followed by half a second of silence

# Turn the buzzer off and wait for 2 seconds

# (If you have a second green 'pedestrian' LED, make it flash on and

# off for the two seconds)

def dontwalk():

# Remember all code in the function is indented

# Flash the amber on and off for 6 seconds

# (And the green 'pedestrian' LED too)

def flashingambergreen():

# Remember all code in the function is indented

# Flash the amber for one more second

# (Turn the green 'pedestrian' LED off and the red on)

def flashingamber():

# Remember all code in the function is indented

# Go through the traffic light sequence by calling each function

# one after the other.

def trafficlightqequence():

# Remember all code in the function is indented

os.system('clear') # Clears the terminal

print("Traffic Lights")

# Initialise the traffic lights

startgreen()

# Here is the loop that waits at lease 20 seconds before

# stopping the cars if the button has been pressed

while True: # Loop around forever

buttonnotpressed = True # Button has not been pressed

start = time.time() # Records the current time

while buttonnotpressed: # While the button has not been pressed

time.sleep(0.1) # Wait for 0.1s

if button.ispressed: # If the button is pressed

now = time.time()

buttonnotpressed = False # Button has been pressed

if (now - start) <= 20: # If under 20 seconds

time.sleep(20 - (now - start)) # Wait until 20s is up

trafficlightqequence() # Run the traffic light sequence

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值