How do I get buzzer to stop?

Hi, newbie here :slight_smile: .

I have this code that plays 3 different notes in a row along with 3 LEDs lighting up in a row after a button has been pressed and held. The lights do what I want by staying on after they light up but the sound keeps staying on an infinite loop when the button is pressed and held which is unwanted.

I'm trying to get the buzzer to only play those three notes and stop after the button has been pushed and held and will only play again once the button has been pressed and held again.

#define Tone_G 392 // Define Tone G
#define Tone_A 440 // Define Tone A
#define Tone_B 494 // Define Tone B


const int BUTTONPIN = 10; // Assign G to Pin 10
const int A = 9; // Assign A to Pin 9
const int B = 8; // Assign B to Pin 8

const int Sound = 11; // For the speakers - sound will occur on Pin 11

const int LED1 = 4;
const int LED2 = 3;
const int LED3 = 2;

void setup()
{
  pinMode(BUTTONPIN, INPUT); // G or Pin 6 functionality to behave as an input 
  digitalWrite(BUTTONPIN, HIGH); // When button is not pressed for G or Pin 6
  
  pinMode(A, INPUT); // A or Pin 5 functionality to behave as an input 
  digitalWrite(A, HIGH); // When button is not pressed for A or Pin 5
  
  pinMode(B, INPUT); // B or Pin 4 functionality to behave as an input 
  digitalWrite(B, HIGH); // When button is not pressed for B or Pin 4
}

void loop()
{
  while(digitalRead(BUTTONPIN) == LOW) 
  {
    tone(Sound, Tone_G, LOW); // Tone to play out of speaker. This Tone is G
    digitalWrite(LED1, HIGH); // Turn built-in LED on 
    delay(200);
    tone(Sound, Tone_A, LOW); // Tone to play out of speaker. This Tone is A
    digitalWrite(LED2, HIGH); // Turn built-in LED on 
    delay(200);
    tone(Sound, Tone_B, LOW); // Tone to play out of speaker. This Tone is B
    digitalWrite(LED3, HIGH); // Turn built-in LED on 
    delay(200);
      

  }

  while(digitalRead(BUTTONPIN) == HIGH) 
  {
    tone(Sound, Tone_G, HIGH); // Tone to play out of speaker. This Tone is G
    digitalWrite(LED3, LOW); // Turn built-in LED on 
    delay(150);
    tone(Sound, Tone_A, HIGH); // Tone to play out of speaker. This Tone is A
    digitalWrite(LED2, LOW); // Turn built-in LED on 
    delay(150);
    tone(Sound, Tone_B, HIGH); // Tone to play out of speaker. This Tone is B
    digitalWrite(LED1, LOW); // Turn built-in LED on 
    delay(150);
 }

  noTone(Sound); // No Sound
  
}

You’re testing IF the button is pressed, not WHEN the button becomes pressed
Read up on state change detection.

I'm puzzled. Where did you find a version of tone() that takes HIGH or LOW for the 3rd parameter and what do HIGH/LOW mean in that context?

Steve

slipstick:
I'm puzzled. Where did you find a version of tone() that takes HIGH or LOW for the 3rd parameter and what do HIGH/LOW mean in that context?

My guess is that they are using "tone(pin, frequency, duration)" with a duration of 0 (LOW) or 1 (HIGH) milliseconds.
Yes, this makes no sense.
I wonder if writing 'HIGH' to the INPUT pins (turning them into INPUT_PULLUP pins) was intentional.