-
Notifications
You must be signed in to change notification settings - Fork 3
/
motor.py
105 lines (88 loc) · 2.69 KB
/
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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Interface a Stepper Motor with Raspberry Pi and L298N
# pip install RPi.GPIO
import RPi.GPIO as GPIO
import time
out1 = 13
out2 = 11
out3 = 15
out4 = 12
speed = .03 # Higher values equal slower speeds. 1 is slow, .001 is very fast
# Lower speeds have higher torque. High speeds have a tendency to strip the motor.
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(out1, GPIO.OUT)
GPIO.setup(out2, GPIO.OUT)
GPIO.setup(out3, GPIO.OUT)
GPIO.setup(out4, GPIO.OUT)
def motor_dispense_candy(num_candy):
i = 0
positive = 0
negative = 0
y = 0
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.LOW)
# A complete rotation is 400; 180 degree rotation is 200
x = abs(400 * num_candy)
for y in range(x, 0, -1):
if i == 0:
GPIO.output(out1, GPIO.HIGH)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.LOW)
time.sleep(speed)
GPIO.output(out1, GPIO.HIGH)
GPIO.output(out2, GPIO.HIGH)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.LOW)
time.sleep(speed)
elif i == 2:
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.HIGH)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.LOW)
time.sleep(speed)
elif i == 3:
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.HIGH)
GPIO.output(out3, GPIO.HIGH)
GPIO.output(out4, GPIO.LOW)
time.sleep(speed)
elif i == 4:
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.HIGH)
GPIO.output(out4, GPIO.LOW)
time.sleep(speed)
elif i == 5:
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.HIGH)
GPIO.output(out4, GPIO.HIGH)
time.sleep(speed)
elif i == 6:
GPIO.output(out1, GPIO.LOW)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.HIGH)
time.sleep(speed)
elif i == 7:
GPIO.output(out1, GPIO.HIGH)
GPIO.output(out2, GPIO.LOW)
GPIO.output(out3, GPIO.LOW)
GPIO.output(out4, GPIO.HIGH)
time.sleep(speed)
if i == 7:
i = 0
continue
i = i+1
def cleanup():
GPIO.cleanup()
def motor_main():
try:
dispense_candy(1)
except KeyboardInterupt:
cleanup()
if __name__ == "__main__":
motor_main()