I am new to programming and am using Python/pyfirmata to control a little servo motor via my arduino. I have the motor working as I want it to: it spins x degrees every 3 seconds. The problem is that I'd like it to do so continuously until user input tells it to stop.
The code below will continuously ask for user input, which (if 1+ENTER is pressed) causes the motor to turn 3 times before asking for user input again. I can't figure out a way to get the motor running through those positions continuously while simultaneously being able to take user input to stop the motor.
Said another way, this is what I want to be able to do: Motor is off and user input is requested. User presses 1+ENTER and motor starts cycling through positions. If user presses 2+ENTER, motor stops. If user presses 1+ENTER again, motor starts cycling again. Seems simple, hopefully it is!
My current code:
from pyfirmata import SERVO
from pyfirmata import Arduino
board = Arduino("COM4")
board.digital[10].mode = SERVO
servo1 = board.get_pin('d:10:0')
while True:
user_control = input('Enter 1 to start motor and 2 to stop motor \n')
if user_control == '1':
servo1.write(45)
time.sleep(3)
servo1.write(90)
time.sleep(3)
servo1.write(180)
time.sleep(3)
elif user_control == '2':
servo1.write(45)
print('motor paused')