#import RPIO import RPi.GPIO as GPIO import time from datetime import datetime from datetime import timedelta switch = 15 pwm = 14 fan_speed =18 #RPIO.setup(switch, RPIO.OUT) #RPIO.output(switch, True) # RPIO.PWM.setup() #fan = RPIO.PWM.Servo(0, 40, 1) GPIO.setmode(GPIO.BCM) GPIO.setup(switch, GPIO.OUT) GPIO.setup(pwm, GPIO.OUT) fan = GPIO.PWM(pwm, 25000) GPIO.setup(fan_speed, GPIO.IN, pull_up_down=GPIO.PUD_UP) global last_ts global current_ts global diff diff = timedelta() last_ts = datetime.now() current_ts = 0 global counter counter = 0 def speed_callback(channel): global last_ts global current_ts global diff global counter current_ts = datetime.now() if current_ts != 0 and last_ts != 0: diff = current_ts - last_ts if diff.seconds < 1: counter+=1 else: rpm = (counter/2)*60 print("{} ||| {} ".format(counter, rpm), end="\r") last_ts = current_ts counter = 0 # print("{} ".format(rpm), end="\r") # print("{:d} | \t{:.2f} | \t{} | \t{} | \t{}".format(diff.microseconds, rpm, diff, current_ts, last_ts), end="\r") GPIO.add_event_detect(fan_speed, GPIO.FALLING, callback=speed_callback) try: GPIO.output(switch, True) step = 10 c = 0 direction = 1 diff=timedelta() fan.start(c) while True: if c == 100: direction = -1 elif c == 0: direction = 1 c += step*direction fan.ChangeDutyCycle(c) # print("{} ".format(c)) time.sleep(5) except KeyboardInterrupt: pass finally: fan.stop() GPIO.output(switch, False) GPIO.cleanup()