用PWM实现多个呼吸灯间歇发光

   日期:2024-01-22     浏览:32    评论:0    
核心提示:电路连接

之前介绍过RPI.GPIO 模块的脉宽调制(PWM)功能,并且实现了呼吸灯的效果。本文更进一步,加入了多个呼吸灯并且用不同的步调间歇发光。

电路连接
20150121232813821-0

程序源码

#!/usr/bin/env python2.7

import RPi.GPIO as GPIO # always needed with RPi.GPIO
from time import sleep  # pull in the sleep function from time module

GPIO.setmode(GPIO.BOARD)  # choose BCM or BOARD numbering schemes. I use BCM

GPIO.setup(26, GPIO.OUT)# set GPIO 25 as output for white led
GPIO.setup(24, GPIO.OUT)# set GPIO 24 as output for red led

white = GPIO.PWM(26, 100)    # create object white for PWM on port 25 at 100 Hertz
red = GPIO.PWM(24, 100)      # create object red for PWM on port 24 at 100 Hertz

white.start(0)              # start white led on 0 percent duty cycle (off)
red.start(100)              # red fully on (100%)

# now the fun starts, we'll vary the duty cycle to 
# dim/brighten the leds, so one is bright while the other is dim

pause_time = 0.01           # you can change this to slow down/speed up

try:
    while True:
        for i in range(0,101):      # 101 because it stops when it finishes 100
            white.ChangeDutyCycle(i)
            red.ChangeDutyCycle(100 - i)
            sleep(pause_time)
        for i in range(100,-1,-1):      # from 100 to zero in steps of -1
            white.ChangeDutyCycle(i)
            red.ChangeDutyCycle(100 - i)
            sleep(pause_time)

except KeyboardInterrupt:
    white.stop()            # stop the white PWM output
    red.stop()              # stop the red PWM output
    GPIO.cleanup()          # clean up GPIO on CTRL+C exit

保存为 led.py 之后,运行

sudo python led.py

就能看到两个LED间歇性呼吸的效果了。

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服