0

I have this script which runs a motor until a button is released. It was working fine up until making some changes to the setup earlier but now the motor doesn't stop when the button is released -

import RPi.GPIO as GPIO
import time
import os
from time import sleep

GPIO.setmode(GPIO.BOARD)

GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
GPIO.setup(11,GPIO.OUT)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(15,GPIO.OUT)

print "Screen up"
GPIO.output(11,GPIO.HIGH)
GPIO.output(13,GPIO.LOW)
GPIO.output(15,GPIO.HIGH)

while True:
    input_state = GPIO.input(10)
    if input_state == True:
        GPIO.output(15,GPIO.LOW)
        time.sleep(1)
        exit()

message = input("Press enter to quit\n\n") 

GPIO.cleanup()

I've tested the button with the following simple button script and it seems to be working fine so I'm very puzzled as to why the motor script isn't now working.

from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

GPIO.setup(10,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while(1):
        if GPIO.input(10)==0:
                print "Button pressed"
                sleep(1)

Thanks

  • roll back the changes and re-apply changes incrementally – jsotola Jan 10 at 16:20
  • the two "button test" code blocks are not the same ..... and the "pin setup" commands are not the same – jsotola Jan 10 at 16:22
  • BTW: you have not actually asked a question – jsotola Jan 10 at 16:25
  • Why don't you go back to the code from your previous posts that seemed to work. – CoderMike Jan 10 at 18:55
  • @CoderMike - Thanks Mike. Yes I am having some success now with the door alarm script from a previous post. – Nick C Jan 11 at 7:13
1

I see a couple of potential issues here:

First, you changed the resistor to pull down, when your working code was up. Also, you never turn the motor off.

# TODO You may need to change this to GPIO.PUD_UP
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 
GPIO.setup(15, GPIO.OUT)

while True:
    input_state = GPIO.input(10)
    if input_state == True:
        # I swtched this to high, as I assume the motor runs when this is on.
        # If for some reason your wiring is different, switch it back
        GPIO.output(15, GPIO.HIGH)
        # exit() The means your code only runs one time.  Don't do it.
    else:
        # you need to turn the motor off when the input state is false
        GPIO.output(15, GPIO.LOW)
    # this is outside the if, so that it will run whether the button up or down
    time.sleep(1)


# Your while loop never ends, which is fine for what you want,
# But that means this code is unreachable.  Comments below
message = input("Press enter to quit\n\n") 
GPIO.cleanup()

If you want to be able to receive keyboard input and button presses, you're going to need to get fancier than a while loop. Look into button interrupts: One Example

  • Thanks Eric. I tried your code but the motor doesn't run at all now whether the button is pressed or not. Perhaps I should study the button interrupts link instead especially if you think that is the best solution for this. – Nick C Jan 10 at 18:32
  • In the long run, that will give you more options, but nothing wrong with prototyping off the basic solution. Also, as mentioned in my code, I switch the HIGH and LOW, which may have been incorrect for your setup. Try switching those around -- although I would only expect that to change it to running when the button is up insteady.... – Eric G Jan 10 at 18:54
  • Actually, put back all the other GPIO lines I removed. You may not have the motor connected to GPIO 15 like I assumed. – Eric G Jan 10 at 18:56
  • Thanks. actually I am just using an LED for testing purposes now. When the script is run I want the LED to be on and the button will be in a pushed state. When I release the button I want the LED to go off. Is that possible using your script? – Nick C Jan 10 at 19:22
  • It’s only possible if you understand it. If my code doesn’t match how it was wired, then it won’t work. And consider my code an example. You may need to tweak things for your setup, but only you can know some of those things. Make sure you understand what every line of this is trying to do. – Eric G Jan 10 at 19:46
0

I managed to do what I want by modifying a door alarm script I remembered having -

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(10,GPIO.OUT)

# function for the door opening
def door_open():
    GPIO.output(10,GPIO.LOW)
    exit()

#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
while True:
  #take a reading
  input = GPIO.input(7)
  GPIO.output(10,GPIO.HIGH)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    door_open()
  #update previous input
  prev_input = input
  #slight pause to debounce
  time.sleep(0.1)

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.