You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
758 B

  1. #! /usr/bin/env python3
  2. import RPi.GPIO as GPIO
  3. import time
  4. servoPIN = 12
  5. GPIO.setmode(GPIO.BCM)
  6. GPIO.setup(servoPIN, GPIO.OUT)
  7. print("Start");
  8. p = GPIO.PWM(servoPIN, 50) # GPIO 12 for PWM with 50Hz
  9. dutyMin=2.1
  10. dutyMid=6.0
  11. dutyMax=10.9
  12. dutyStep=-0.1
  13. dutySet=dutyMid
  14. print("Center");
  15. p.start(dutyMid)
  16. time.sleep(3.0)
  17. try:
  18. while True:
  19. print("Duty cycle " + str(dutySet));
  20. p.ChangeDutyCycle(dutySet)
  21. time.sleep(0.1)
  22. dutySet += dutyStep
  23. if dutySet > dutyMax or dutySet < dutyMin:
  24. dutyStep = -dutyStep
  25. dutySet += dutyStep
  26. dutySet += dutyStep
  27. print("");
  28. time.sleep(1)
  29. except KeyboardInterrupt:
  30. print("\nCenter");
  31. p.ChangeDutyCycle(dutyMid)
  32. time.sleep(1.0)
  33. p.stop()
  34. GPIO.cleanup()