1
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16, GPIO.OUT) #forward
GPIO.setup(26, GPIO.OUT) #forward
GPIO.setup(5, GPIO.OUT)  #backward
GPIO.setup(6, GPIO.OUT) #backward
GPIO.setup(25, GPIO.OUT) #Motor


print ("Car is moving Forward.")

GPIO.output(16, 1)
GPIO.output(26, 1)
GPIO.output(25, GPIO.HIGH)
GPIO.output(5, 0)
GPIO.output(6, 0)

time.sleep(4)





print ("Sprinkling Water.")
GPIO.output(25, GPIO.LOW)
GPIO.output(16, 0)
GPIO.output(26, 0)
GPIO.output(5, 0)
GPIO.output(6, 0)


time.sleep(2)


print("Car is moving backward.")
GPIO.output(5,1)
GPIO.output(6, 1)
GPIO.output(25, GPIO.HIGH)
GPIO.output(16, 0)
GPIO.output(26, 0)

time.sleep(3)

GPIO.cleanup()

Now I want is that my car moves forward, stops and then sprinkles water and then moves backward. But, with this code..My car is moving forward while sprinkling water and then stopes while the sprinkling motor is on and then backward while the motor is on (the motor never stops).

What am I doing wrong here?

2

Try this:

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(16, GPIO.OUT) #forward
GPIO.setup(26, GPIO.OUT) #forward
GPIO.setup(5, GPIO.OUT)  #backward
GPIO.setup(6, GPIO.OUT) #backward
GPIO.setup(25, GPIO.OUT) #Motor


print ("Car is moving Forward.")

GPIO.output(16, 1)
GPIO.output(26, 1)
GPIO.output(25, GPIO.HIGH)
GPIO.output(5, 0)
GPIO.output(6, 0)

time.sleep(4)

    print ("Stopping the motors.")
    GPIO.output(16, 0)
    GPIO.output(26, 0)
    GPIO.output(5, 0)
    GPIO.output(6, 0)

    time.sleep(1)
    GPIO.output(25, GPIO.LOW) #turn on pump
    time.sleep(1)
    GPIO.output(25, GPIO.HIGH) #turn off pump

    print("Car is moving backward.")
    GPIO.output(5,1)
    GPIO.output(6, 1)
    GPIO.output(16, 0)
    GPIO.output(26, 0)
  • It didn't work, brother. I just did the same....my sprinkling water motor never stops. Just to clear, this (sprinkling water) motor is being controlled through relay and one of the wire is going in the digital pin which is being low. – spiCkyyy Jun 19 '17 at 21:35
  • I am assuming that the relay is controlled by pin25? Is this correct? Does the relay click? – Steve Robillard Jun 19 '17 at 21:37
  • Yes, you're right – spiCkyyy Jun 19 '17 at 21:38
  • You did not answer my question if the relay clicks. These 3 lines GPIO.output(25, GPIO.LOW) #turn on pump time.sleep(1) GPIO.output(25, GPIO.HIGH) #turn off pump should turn the pump on for 1 seconf then off. You can try changing low to high and vice cersa, but you say that the relay is active low. Also, how do you have the relay connected to the pump? – Steve Robillard Jun 19 '17 at 21:40
  • Your given piece of code worked brother. Thanks a lot. <3 – spiCkyyy Jun 19 '17 at 21:44

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.