Guest User

Untitled

a guest
Jul 1st, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3.  
  4. # Define GPIO pins
  5. IN1 = 17
  6. IN2 = 18
  7. IN3 = 22
  8. IN4 = 23
  9.  
  10. # Set GPIO mode
  11. GPIO.setmode(GPIO.BCM)
  12.  
  13. # Set up GPIO pins
  14. GPIO.setup(IN1, GPIO.OUT)
  15. GPIO.setup(IN2, GPIO.OUT)
  16. GPIO.setup(IN3, GPIO.OUT)
  17. GPIO.setup(IN4, GPIO.OUT)
  18. import RPi.GPIO as GPIO
  19. import time
  20.  
  21. # Define GPIO pins
  22. IN1 = 17
  23. IN2 = 18
  24. IN3 = 22
  25. IN4 = 23
  26.  
  27. # Set GPIO mode
  28. GPIO.setmode(GPIO.BCM)
  29.  
  30. # Set up GPIO pins
  31. GPIO.setup(IN1, GPIO.OUT)
  32. GPIO.setup(IN2, GPIO.OUT)
  33. GPIO.setup(IN3, GPIO.OUT)
  34. GPIO.setup(IN4, GPIO.OUT)
  35.  
  36. def set_step(w1, w2, w3, w4):
  37. GPIO.output(IN1, w1)
  38. GPIO.output(IN2, w2)
  39. GPIO.output(IN3, w3)
  40. GPIO.output(IN4, w4)
  41.  
  42. # Example sequence for full step
  43. seq = [
  44. [1, 0, 1, 0],
  45. [0, 1, 0, 1]\
  46. #,
  47. #[0, 0, 1, 1],
  48. #[1, 0, 0, 1]
  49. ]
  50.  
  51. import RPi.GPIO as GPIO
  52. import time
  53.  
  54. # Define GPIO pins
  55. IN1 = 17
  56. IN2 = 18
  57. IN3 = 22
  58. IN4 = 23
  59.  
  60. # Set GPIO mode
  61. GPIO.setmode(GPIO.BCM)
  62.  
  63. # Set up GPIO pins
  64. GPIO.setup(IN1, GPIO.OUT)
  65. GPIO.setup(IN2, GPIO.OUT)
  66. GPIO.setup(IN3, GPIO.OUT)
  67. GPIO.setup(IN4, GPIO.OUT)
  68. import RPi.GPIO as GPIO
  69. import time
  70.  
  71. # Define GPIO pins
  72. IN1 = 17
  73. IN2 = 18
  74. IN3 = 22
  75. IN4 = 23
  76.  
  77. # Set GPIO mode
  78. GPIO.setmode(GPIO.BCM)
  79.  
  80. # Set up GPIO pins
  81. GPIO.setup(IN1, GPIO.OUT)
  82. GPIO.setup(IN2, GPIO.OUT)
  83. GPIO.setup(IN3, GPIO.OUT)
  84. GPIO.setup(IN4, GPIO.OUT)
  85.  
  86. def set_step(w1, w2, w3, w4):
  87. GPIO.output(IN1, w1)
  88. GPIO.output(IN2, w2)
  89. GPIO.output(IN3, w3)
  90. GPIO.output(IN4, w4)
  91.  
  92. # Example sequence for full step
  93. seq = [
  94. [1, 0, 1, 0],
  95. [0, 1, 1, 0],
  96. [0, 1, 0, 1],
  97. [1, 0, 0, 1]
  98. ]
  99.  
  100. # Rotate stepper motor clockwise
  101. def clockwise(delay, steps):
  102. for _ in range(steps):
  103. for halfstep in range(4):
  104. set_step(seq[halfstep][0], seq[halfstep][1], seq[halfstep][2], seq[halfstep][3])
  105. time.sleep(delay)
  106.  
  107. # Rotate stepper motor counterclockwise
  108. def counterclockwise(delay, steps):
  109. for _ in range(steps):
  110. for halfstep in range(4):
  111. set_step(seq[3 - halfstep][0], seq[3 - halfstep][1], seq[3 - halfstep][2], seq[3 - halfstep][3])
  112. time.sleep(delay)
  113.  
  114. # Example usage
  115. try:
  116. while True:
  117. print('clockwise')
  118. clockwise(0.5, 10) # Rotate clockwise 200 steps with 0.01s delay between steps
  119. time.sleep(0.5)
  120. print('counter clockwise')
  121. counterclockwise(0.5, 10) # Rotate counterclockwise 200 steps with 0.01s delay between steps
  122. time.sleep(0.5)
  123. time.sleep(5)
  124.  
  125. except KeyboardInterrupt:
  126. print("Program stopped by user")
  127.  
  128. finally:
  129. GPIO.cleanup() # Clean up GPIO on exit
  130.  
  131.  
  132. # Example usage
  133. try:
  134. while True:
  135. print('clockwise')
  136. clockwise(0.1, 100) # Rotate clockwise 200 steps with 0.01s delay between steps
  137. time.sleep(0.5)
  138. # print('counter clockwise')
  139. # counterclockwise(0.1, 100) # Rotate counterclockwise 200 steps with 0.01s delay between steps
  140. # time.sleep(0.5)
  141. # time.sleep(5)
  142.  
  143. except KeyboardInterrupt:
  144. print("Program stopped by user")
  145.  
  146. finally:
  147. GPIO.cleanup() # Clean up GPIO on exit
  148.  
Advertisement
Add Comment
Please, Sign In to add comment