0

I want to implement a function that does not abort my program but wait until I press the button on channel 11. And start the program again.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11,GPIO.IN) #GPIO17

while GPIO.input(11) == GPIO.LOW:
  GPIO.input(11) == GPIO.LOW
  os.system("python /home/pi/gpio.py")
  if not (GPIO.input(11) == GPIO.HIGH):
      break

![enter image description here] (https://i.sstatic.net/UOzdW.jpg)

2
  • I Uploaded a picture to realise what i want to do Commented Feb 5, 2019 at 10:11
  • your quesion would get more attention here... raspberrypi.stackexchange.com Commented Feb 5, 2019 at 10:15

2 Answers 2

1

I very like to use gpiozero library for its event handling. I post example with this library below:

from gpiozero import Button
from signal import pause
import os

buttonPin = 4

def ButtonPressedCallback():
    #do what you need when button is pressed
    os.system("python /home/pi/gpio.py")

button = Button(buttonPin)
button.when_pressed = ButtonPressedCallback



pause()

Or with RPi.GPIO library:

import RPi.GPIO as GPIO
import time
import os
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(11,GPIO.IN) #GPIO17

def my_callback():
    #do something
    print("button pressed")

GPIO.add_event_detect(11, GPIO.RISING, callback=my_callback, bouncetime=200)
#You can use GPIO.RISING, GPIO.FALLING, GPIO.BOTH

while True:
   time.sleep(0.01)
#Or you can use pause() from signal package
Sign up to request clarification or add additional context in comments.

6 Comments

Traceback (most recent call last): File "/home/pi/python.py", line 11, in <module> button.when_pressed = ButtonPressedCallback NameError: name 'ButtonPressedCallback' is not defined This is the error which he shows me up on the first program which you has send to me
Sorry, I corrected code. Callback function must be defined before it is assigned to event
It doesn´t do anythin if i hit my button .. my putton is connected to pin 11 and 17. Which number contains buttonPin= ?
OK. Connect one wire of button to pin 11 and other wire to GROUND. buttonPin=11 You had bad wiring. Now it must works
Okey i have the solution now
|
0

What you want to do is to use interrupts. Click for details here.

In short an interrupt is something that, well, interrupts normal program flow and passes control an interrupt procedure. When writing programs with GUI you have all sort of onButtonClick() methods that do exactly this: they handle interrupts passed by operating system. Int the example in the link this line:

GPIO.add_event_detect(BTN_G, GPIO.BOTH, handle)

add event detection and passes flow control to handle() function.

Comments

Your Answer

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