查看: 4075|回复: 0

[原创] 【一网打尽】用PWM实现多个呼吸灯间歇发光

[复制链接]
  • TA的每日心情

    2015-8-29 20:27
  • 签到天数: 5 天

    连续签到: 1 天

    [LV.2]偶尔看看I

    发表于 2015-3-14 09:47:57 | 显示全部楼层 |阅读模式
    分享到:

    程序:
    #!/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间歇性呼吸的效果了。
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-4-25 12:57 , Processed in 0.110155 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.