2

I am stuck on this work related project. What I am trying to accomplish is, after a specific amount of button presses, I want my program to end. I am thinking along the lines of a loop.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:

    input_state = GPIO.input(18)
    if input_state == False:
            print('Button Pressed')
            time.sleep(0.2)

Max_Presses = 100
button_pressed  = 0

def button_pressed():
    return input_state()

 while button_pressed < Max_Presses:

    print 'still going'
    if button_pressed():
            button_pressed += 1
2
  • Did you already try something? Show the code Commented Jun 18, 2015 at 13:58
  • i posted the code above Commented Jun 18, 2015 at 15:35

1 Answer 1

1

Yes, a while loop sounds like a necessary thing to use for that. You will need a counter that keeps track of the number of button presses. Check the counter as your loop condition, e.g.

MAX_PRESSES = 10
button_presses = 0

def button_pressed():
     return poll_GPIO_pin_or_whatever()   # something that detects button presses

while button_presses < MAX_PRESSES:
    print 'still going'
    if button_pressed():
        button_presses += 1

The check function might work by polling a GPIO pin, or there might be some sort of callback mechanism for that that works in your environment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.