-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy path07_motor.py
executable file
·45 lines (36 loc) · 1.18 KB
/
07_motor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
MotorPin1 = 17
MotorPin2 = 18
MotorEnable = 27
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by BCM
GPIO.setup(MotorPin1, GPIO.OUT) # mode --- output
GPIO.setup(MotorPin2, GPIO.OUT)
GPIO.setup(MotorEnable, GPIO.OUT)
GPIO.output(MotorEnable, GPIO.LOW) # motor stop
def loop():
while True:
print ("Press Ctrl+C to end the program...")
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
GPIO.output(MotorPin1, GPIO.HIGH) # clockwise
GPIO.output(MotorPin2, GPIO.LOW)
time.sleep(5)
GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(5)
GPIO.output(MotorEnable, GPIO.HIGH) # motor driver enable
GPIO.output(MotorPin1, GPIO.LOW) # anticlockwise
GPIO.output(MotorPin2, GPIO.HIGH)
time.sleep(5)
GPIO.output(MotorEnable, GPIO.LOW) # motor stop
time.sleep(5)
def destroy():
GPIO.output(MotorEnable, GPIO.LOW) # motor stop
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()