Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. from time import sleep
  2. import pigpio
  3.  
  4. DIR = 20 # Direction GPIO Pin
  5. STEP = 21 # Step GPIO Pin
  6. SWITCH = 16 # GPIO pin of switch
  7.  
  8. # Connect to pigpiod daemon
  9. pi = pigpio.pi()
  10.  
  11. # Set up pins as an output
  12. pi.set_mode(DIR, pigpio.OUTPUT)
  13. pi.set_mode(STEP, pigpio.OUTPUT)
  14.  
  15. # Set up input switch
  16. pi.set_mode(SWITCH, pigpio.INPUT)
  17. pi.set_pull_up_down(SWITCH, pigpio.PUD_UP)
  18.  
  19. MODE = (14, 15, 18) # Microstep Resolution GPIO Pins
  20. RESOLUTION = {'Full': (0, 0, 0),
  21. 'Half': (1, 0, 0),
  22. '1/4': (0, 1, 0),
  23. '1/8': (1, 1, 0),
  24. '1/16': (0, 0, 1),
  25. '1/32': (1, 0, 1)}
  26. for i in range(3):
  27. pi.write(MODE[i], RESOLUTION['Full'][i])
  28.  
  29. # Set duty cycle and frequency
  30. pi.set_PWM_dutycycle(STEP, 128) # PWM 1/2 On 1/2 Off
  31. pi.set_PWM_frequency(STEP, 500) # 500 pulses per second
  32.  
  33. try:
  34. while True:
  35. pi.write(DIR, pi.read(SWITCH)) # Set direction
  36. sleep(.1)
  37.  
  38. except KeyboardInterrupt:
  39. print ("\nCtrl-C pressed. Stopping PIGPIO and exiting...")
  40. finally:
  41. pi.set_PWM_dutycycle(STEP, 0) # PWM off
  42. pi.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement