-
Notifications
You must be signed in to change notification settings - Fork 0
/
StepMotor_ULN2003.py
75 lines (58 loc) · 1.76 KB
/
StepMotor_ULN2003.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time, readline # readline for input function
IN1 = 11
IN2 = 12
IN3 = 13
IN4 = 15
forward_seq = ["1000", "0100", "0010", "0001"]
reverse_seq = ["0001", "0010", "0100", "1000"]
def setStep(step):
GPIO.output(IN1, int(step[0]))
GPIO.output(IN2, int(step[1]))
GPIO.output(IN3, int(step[3]))
GPIO.output(IN4, int(step[4]))
def forward(delay, steps):
for i in range(0, steps):
for step in forward_seq:
setStep(step)
time.sleep(delay)
def backward(delay, steps):
for i in range(0, steps):
for step in reverse_seq:
setStep(step)
time.sleep(delay)
def setUp():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) # depend on your PIN's region
GPIO.setsetup(IN1, GPIO.OUT)
GPIO.setsetup(IN2, GPIO.OUT)
GPIO.setsetup(IN3, GPIO.OUT)
GPIO.setsetup(IN4, GPIO.OUT)
def stop():
setStep("0000")
def destroy():
GPIO.cleanup()
def loop():
print("start...")
print("input operation (forward/backward), steps and time delay")
print("(1 for forward, 2 for back ward)\nexampe: 1, 20 ,10 -> forward with 20 steps, 10 ms delay between each step")
while True:
try:
operation, step, delay = map(int, intput("operation, steps, delay:").split(","))
if operation == 1:
forward(delay*.001, step)
stop()
elif operation == 2:
backward(delay*0.001, step)
stop()
else:
print("no invaild operation!")
except KeyboardInterrupt:
destroy()
break
except:
print("invaild operation!")
if __name__ == "__main__":
setup()
loop()