0

I'm building a little Car with 2 Engine, one L298N driver and a Raspberry pi 3. But i need the engine run during some specific seconds and then cleanup the GPIO and the engine stop. Why? Because i'm building an API then when i do the car go forward with my actual code the car go forward in an infinite loop and it doesn't accept other request until it finished the last request(go forward) but it won't happen because is an infinite loop.

My code for the car go forward is:

import RPi.GPIO as GPIO
import os
import time

ena = 18
in1 = 23
in2 = 24
enb = 19
in3 = 6
in4 = 5
GPIO.setmode(GPIO.BCM)

GPIO.setup(ena,GPIO.OUT)
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)

GPIO.setup(enb,GPIO.OUT)
GPIO.setup(in3,GPIO.OUT)
GPIO.setup(in4,GPIO.OUT)

pwm_a = GPIO.PWM(ena,500)  
pwm_b = GPIO.PWM(enb,500) 

pwm_a.start(0)   
pwm_b.start(0)  
os.system('clear')


try:
        while True:
                GPIO.output(in1,False)
                GPIO.output(in2,True)
                GPIO.output(in3,False)
                GPIO.output(in4,True)
                pwm_a.ChangeDutyCycle(100)
                pwm_b.ChangeDutyCycle(100)


except KeyboardInterrupt:
        pwm_a.stop()
        pwm_b.stop()
        GPIO.cleanup()
        os.system('clear')
        print
        print("User finished the program")
        print
        exit()
  • Your code doesn't seem to accept input from anything, ever. Is time.sleep() what you want? – Mark Smith Oct 15 '18 at 19:13
  • Hi, thanks for your comment. I need the engine run for 4 secs only, in my code the engine run infinite time because i'm using While=True. I've thought that if i can control the time of the While=True for 4 secs and the change it for False but i don't know if it is possible – German Jr Oct 16 '18 at 9:29
1

When you run code in a while loop, the code is executed over and over again. However once you have set a GPIO or PWM, it stays set until you change it. There's no need to keep setting it and setting it forever.

So what you can do is set the GPIOs and PWMs to make your motor run, then just wait for however long you want it to run, then make the changes you need to stop the motor.

Here's an example. It starts the motor, waits five seconds, then stops it. It also stops the motor if the user presses CTRL+C -- I presume you want this since you wrote it into your original program. It does NOT do the other stuff you mention -- accepting requests. It just runs for 5 seconds, then stops. I also assume your code works perfectly as a basis -- which I can't test because I don't have the hardware.

import RPi.GPIO as GPIO
import os
import time

# GPIO pin defintions
ena = 18
in1 = 23
in2 = 24
enb = 19
in3 = 6
in4 = 5

# Configure GPIO pins
GPIO.setmode(GPIO.BCM)

GPIO.setup(ena,GPIO.OUT)
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)

GPIO.setup(enb,GPIO.OUT)
GPIO.setup(in3,GPIO.OUT)
GPIO.setup(in4,GPIO.OUT)

pwm_a = GPIO.PWM(ena,500)  
pwm_b = GPIO.PWM(enb,500) 

pwm_a.start(0)   
pwm_b.start(0)  
os.system('clear')

# Start the motor running
GPIO.output(in1,False)
GPIO.output(in2,True)
GPIO.output(in3,False)
GPIO.output(in4,True)
pwm_a.ChangeDutyCycle(100)
pwm_b.ChangeDutyCycle(100)

# Wait five seconds
try:
    time.sleep(5)

# In case the user presses CTRL+C
except KeyboardInterrupt:
        print("User finished the program")

# If we get here, either five seconds has passed, or the user pressed CTRL+C.  Either way, stop the motor and clean up
pwm_a.stop()
pwm_b.stop()
GPIO.cleanup()
os.system('clear')

exit()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.