0

I have a raspberry pi wich i'm using to monitor an input from the GPIO ports, to do this i need to run an infinite loop.

When i receive a LOW in my input i want to execute a system command usint subprocess.call. The problem is that it executes this command for as long as the input is receiving LOW i have tried this to make it execute only once but i can't make it work.

while 1:
    if (GPIO.input(11) != GPIO.HIGH ):
         puerta_abierta = 1
         if(puerta_abierta == 1 ):
              call(["mpg123", "file.mp3"])
              puerta_abierta = 0
    else:
        puerta_abierta = 0

2 Answers 2

2

You could do something like this:

called = False

while True:
    if GPIO.input(11) != GPIO.HIGH:
        if not called:
            call(["mpg123", "file.mp3"])
            called = True

You could also break out of the loop, but that might not be what you intend to happen:

while True:
    if GPIO.input(11) != GPIO.HIGH:
        call(["mpg123", "file.mp3"])
        break
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks your fist option does work, i guess i didn't explained myself good enough. I want that after it has played it doesn't activate untill my input is LOW again
1

Like this:

puerta_abierta = 0
while 1:
    if (GPIO.input(11) != GPIO.HIGH ):
        puerta_abierta += 1
        if(puerta_abierta == 1 ):
            call(["mpg123", "file.mp3"])
    else:
        puerta_abierta = 0

4 Comments

This solution will call mpg123 every time the input goes HIGH and then LOW again because it resets puerta_abierta. Not sure if this is good or bad because its how the original code works, but violates the posters request that mpg123 only be called once.
Yes, it is. When I wrote the code, I knew it. And I think it is what the asker wants.
I like (and upvoted) your answer but wanted to toss in a caveat for the original poster in case it wasn't obvious.
@tdelaney Thank you! I should write this in case of need.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.