Buzzer only beeps once after ending the loop with a button

Hey, I’m new and not really an expert at coding with Python so I need some help with using while loops for my school project. Currently, I have a buzzer that will be triggered and beeps forever when a vibration switch registers a vibration. I have a button that will stop the beeping of the buzzer. However, the next time the vibration switch does record a vibration, the buzzer will only beep once instead of always beeping. What should I do to make the buzzer always beep every time the vibration switch is triggered? Here is my code:

import RPi.GPIO as GPIO
import time

buzzer = 15    # Original is 11 (GPIO17), we use 15 (GPIO22)
BtnPin = 16
VibratePin = 11

GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
GPIO.setup(BtnPin, GPIO.IN, pull_up_down = GPIO.PUD_UP) #Button
GPIO.setup(buzzer, GPIO.OUT)
GPIO.setup(VibratePin, GPIO.IN, pull_up_down=GPIO.PUD_UP) 

b=0 #Define b

def beep(x): #Configure how the beeping works
    on()
    time.sleep(x)
    off()
    time.sleep(x)

def on():
    GPIO.output(buzzer, GPIO.LOW)

def off():
    GPIO.output(buzzer, GPIO.HIGH)

while True:
    
    loop_beep = True
    state = 0
    #GPIO.output(buzzer, GPIO.HIGH) #Buzzer is OFF
    
    if GPIO.input(VibratePin)==0: #Vibration Switch triggered
        print("Triggered")
        
        while loop_beep: # Beep forever till button is pressed
            beep(0.5)
                    
            button_state = GPIO.input(16)
                
            if button_state == 0:
                
                if b == 1:
                    b = 0
                        
                else:
                    b = 1
                        
            if b == 1: #When button is pressed
                print("Pressed button")
                loop_beep = False
        
        state = state + 1
        if state > 1:
            state = 0
        loop_beep = True    
        
    else:
        print("Running")

Initially, you set b to 0.

When it detects a vibration, it beeps and checks the button. If that’s not pressed, b is still 0, so it just go around and beep again.

When the button is pressed, it sets b to 1.

b is now 1, so it leaves the loop.

Next time it detects a vibration, it beeps and checks the button. If that’s not pressed, b remains unchanged at 1 because you haven’t set it back to 0.

b is 1, so it leaves the loop.

The question I have is why you have b, and loop_beep for that mattter, at all. I think that just over complicates it. You can just have while True and leave the loop when the button is pressed by using break.

1 Like

To be honest, I wasn’t really clear on how the button coding worked (especially the b variable and all) since I got it from friends/online without really understanding it so your explanation really helped me so thank you for that! It’s now working since I set b as 0 when the button is pressed now.

If you don’t mind, could you explain how I can use while True and break instead of b and loop_beep? Thank you so much :smiley:

Replace while loop_beep: with while True: and then have:

    if button_state == 0: #When button is pressed
        print("Pressed button")
        break

All the other stuff about b and loop_beep can then be removed.

Also, I don’t know what the purpose of state is it has no effect on the rest of the code.