1234567891011121314151617181920212223242526 |
- import RPi.GPIO as GPIO
- import time
- import sys
- class PWM:
- def __init__(self, pin):
- self.pin = pin
- GPIO.setup(self.pin, GPIO.OUT)
- GPIO.output(self.pin, 0)
- self.pwm = GPIO.PWM(self.pin, 100)
- self.pwm.start(0)
- def setDutyCycle(self, val):
- self.pwm.ChangeDutyCycle(val)
- if __name__ == '__main__':
- try:
- GPIO.setmode(GPIO.BCM)
- p = PWM(32)
- while True:
- p.setDutyCycle(int(time.time()*100 % 100))
- time.sleep(.03)
- except KeyboardInterrupt:
- GPIO.cleanup()
- sys.exit(0)
|