The program itself is as follows:
Code: Select all
#!/usr/bin/env python # -*- coding: utf-8 -*- import RPi.GPIO as gpio import time actuator1_in1 = 17 actuator1_in2 = 22 def gpio_init(): gpio.setmode(gpio.BCM) gpio.setup(actuator1_in1, gpio.OUT) gpio.setup(actuator1_in2, gpio.OUT) def open_hatch(sec): gpio.output(actuator1_in1, gpio.LOW) gpio.output(actuator1_in2, gpio.LOW) cnt = 1 while cnt < 2: cnt += 1 gpio.output(actuator1_in2, gpio.HIGH) time.sleep(sec) def main(): sec = input("Keep it running for (seconds): ", ) open_hatch(sec) if __name__ == "__main__": try: gpio_init() main() except: print("Something went wrong") finally: gpio.cleanup()
What I'm trying to do here: ask from the user for how many seconds to run the motor, in function open_hatch make sure pins are set to False before setting gpio22 to True, hold it for a certain count of seconds, clean up the pins, exit program.
What really happens: when the program is executed, a numerical input is prompted, after I insert, say, 3, engine runs for 3 seconds and program stops. So far, so good. But when I execute it for the second time, while I'm prompted for numerical input, the engine starts running backward without me entering anything.
Why so? I expected that gpio.cleanup() takes care of setting all used pins to default again. How come it starts running at once? And where comes the knowledge to move in opposite direction?
As I'm new to both RPi and programming, in general, all other suggestions how to operate my motor more politely are more than welcome.