refactor loop
@@ -40,7 +40,7 @@ ADAFRUIT_IO_USERNAME = ''
aio = Client(ADAFRUIT_IO_USERNAME, ADAFRUIT_IO_KEY)
# Delay between checking for new stepper slider value on Adafruit IO, in seconds
-ADAFRUIT_IO_DELAY = 15
+ADAFRUIT_IO_DELAY = 1
# Stepper 1 Adafruit IO Feeds
feed_step_1_steps = aio.feeds('stepper1steps')
@@ -52,6 +52,7 @@ feed_step_2_direction = aio.feeds('stepper2direction')
feed_step_2_step_size = aio.feeds('stepper2stepsize')
# Shutdown Feed
feed_stepper_shutdown = aio.feeds('steppershutdown')
+feed_steppers_status = aio.feeds('stepperstart')
# create a default object, no changes to I2C address or frequency
kit = MotorKit()
@@ -86,23 +87,21 @@ def stepper_worker(stepper, numsteps, direction, stepper_name, style, show_steps
aio.send(feed_step_2_steps.key, 0)
while True:
- # Retrieve Data from Adafruit IO
-
- # Stepper Shutdown
- is_shutdown = aio.receive(feed_stepper_shutdown.key)
- if is_shutdown.value == 1:
- print('* SHUTTING OFF BOTH STEPPER MOTORS *')
- turnOffMotors()
+ try:
+ stepper_start = aio.receive(feed_steppers_status.key)
+ print('Stepper Button Pressed? ', stepper_start.value)
+ except Adafruit_IO.errors.ThrottlingError:
+ print('Exceeded the limit of Adafruit IO requests, delaying 30 seconds...')
+ time.sleep(30)
# Stepper 1
- if not st1.isAlive(): # if thread is killed
- print('Retrieving Stepper 1 Config from IO...')
- stepper_1_steps = aio.receive(feed_step_1_steps.key)
- stepper_1_steps = int(stepper_1_steps.value)
- if stepper_1_steps is not 0: # check against if the feed value is 0
+ if not st1.isAlive() and int(stepper_start.value):
+ print('Stepper 1 Configuration (from IO)')
stepper_1_direction = aio.receive(feed_step_1_direction.key)
stepper_1_step_size = aio.receive(feed_step_1_step_size.key)
- print('Stepper 1 Configuration (from IO)')
+ time.sleep(1)
+ stepper_1_steps = aio.receive(feed_step_1_steps.key)
+ stepper_1_steps = int(stepper_1_steps.value)
print("%d steps" % stepper_1_steps)
print('Step Size: ', stepper_1_step_size.value)
print('Stepper Direction: ', stepper_1_direction.value)
@@ -123,11 +122,9 @@ while True:
st1.start()
# Stepper 2
- if not st2.isAlive(): # if thread is killed
- print('Retrieving Stepper 2 Config from IO...')
+ if not st2.isAlive() and int(stepper_start.value):
stepper_2_steps = aio.receive(feed_step_2_steps.key)
stepper_2_steps = int(stepper_2_steps.value)
- if stepper_2_steps is not 0: # check against if the feed value is 0
stepper_2_direction = aio.receive(feed_step_2_direction.key)
stepper_2_step_size = aio.receive(feed_step_2_step_size.key)
print('Stepper 2 Configuration (from IO)')
