6
6
import atexit
7
7
import threading
8
8
import random
9
- from adafruit_motorkit import MotorKit
10
9
from adafruit_motor import stepper as STEPPER
10
+ from adafruit_motorkit import MotorKit
11
11
12
12
# create a default object, no changes to I2C address or frequency
13
13
kit = MotorKit ()
@@ -31,33 +31,39 @@ def stepper_worker(stepper, numsteps, direction, style):
31
31
stepper .onestep (direction = direction , style = style )
32
32
#print("Done")
33
33
34
- while ( True ) :
34
+ while True :
35
35
if not st1 .isAlive ():
36
36
randomdir = random .randint (0 , 1 )
37
- print ("Stepper 1" ),
38
- if ( randomdir == 0 ) :
39
- dir = STEPPER .FORWARD
40
- print ("forward" ),
37
+ print ("Stepper 1" )
38
+ if randomdir == 0 :
39
+ direction = STEPPER .FORWARD
40
+ print ("forward" )
41
41
else :
42
- dir = STEPPER .BACKWARD
43
- print ("backward" ),
42
+ direction = STEPPER .BACKWARD
43
+ print ("backward" )
44
44
randomsteps = random .randint (10 ,50 )
45
45
print ("%d steps" % randomsteps )
46
- st1 = threading .Thread (target = stepper_worker , args = (kit .stepper1 , randomsteps , dir , stepstyles [random .randint (0 ,3 )],))
46
+ st1 = threading .Thread (target = stepper_worker , args = (kit .stepper1 ,
47
+ randomsteps ,
48
+ direction ,
49
+ stepstyles [random .randint (0 ,3 )],))
47
50
st1 .start ()
48
51
49
52
if not st2 .isAlive ():
50
- print ("Stepper 2" ),
53
+ print ("Stepper 2" )
51
54
randomdir = random .randint (0 , 1 )
52
- if ( randomdir == 0 ) :
53
- dir = STEPPER .FORWARD
54
- print ("forward" ),
55
+ if randomdir == 0 :
56
+ direction = STEPPER .FORWARD
57
+ print ("forward" )
55
58
else :
56
- dir = STEPPER .BACKWARD
57
- print ("backward" ),
59
+ direction = STEPPER .BACKWARD
60
+ print ("backward" )
58
61
randomsteps = random .randint (10 ,50 )
59
62
print ("%d steps" % randomsteps )
60
- st2 = threading .Thread (target = stepper_worker , args = (kit .stepper2 , randomsteps , dir , stepstyles [random .randint (0 ,3 )],))
63
+ st2 = threading .Thread (target = stepper_worker , args = (kit .stepper2 ,
64
+ randomsteps ,
65
+ direction ,
66
+ stepstyles [random .randint (0 ,3 )],))
61
67
st2 .start ()
62
68
63
69
time .sleep (0.1 ) # Small delay to stop from constantly polling threads (see: https://forums.adafruit.com/viewtopic.php?f=50&t=104354&p=562733#p562733)
0 commit comments