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

#!/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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值