2

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

4

You don't want to relaunch your program like that - every time you do, you fork the process. See fork bomb.

If you construct your program so that you have a block of code you can re-run when the button is pressed, that would work much better.

from gpiozero import Button

btn = Button(17)

while True:
    btn.wait_for_press()
    print("button was pressed")
0

Is the following what you are trying to do?

Note: Pin11 (board pin number) is GPIO17 (BCM number) https://pinout.xyz/pinout/pin11_gpio17

#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN,pull_up_down=GPIO.PUD_DOWN) 

while True:
    if GPIO.input(17):
        print('Pressed')
    time.sleep(0.1)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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