arduino buzzer and pushbutton?

I’ve re-created the “STOP IT” game with Arduino, and I am currently running an LCD display to display the score. Me and my engineering teacher were confused about the buzzer. We want the buzzer to play a “ding” when you stop the led in the middle, and play a “buzzer” when you stop the led on any of the led’s but the correct one. We are struggling on how to write the code. Can anyone help us with our problem?

Here is our code that includes the stop it game and the lcd,

int Led1Pin = 6; //The pin of the first LED.
int Led2Pin = 7; //The pin of the second LED.
int Led3Pin = 8; //etc.
int Led4Pin = 9;
int Led5Pin = 10;

int ButtonPin = 13; //The pin of the button.

//The state of the button the last time we checked.
boolean old_val = LOW;

//Stores which LED is on.
int LightPosition = 0;

//How much time in between LED light changes.
int pause = 1000;

//What time it was when we last moved the light.
long lastMove = millis();

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup()
{
  lcd.begin(16, 2);
 
  pinMode(Led1Pin, OUTPUT);
  pinMode(Led2Pin, OUTPUT);
  pinMode(Led3Pin, OUTPUT);
  pinMode(Led4Pin, OUTPUT);
  pinMode(Led5Pin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  
  Serial.begin(9600);
  
  //Start a new game.
  newGame();
}

void loop()
{
  //Move the light.
  if(millis() - lastMove >= pause)
  {
    lastMove = millis(); //Remember when we switched LED
    
    LightPosition++; //increment the Light position.
    if(LightPosition >= 10) LightPosition = 5;
    
    move(LightPosition);//Update the light position.
  }
  
  //When the player presses the button...
  if(digitalRead(ButtonPin) == HIGH && old_val == LOW)
  {
    //If the pressed it when the light was in the middle, speed up and continue.
    if(LightPosition == 7)
      //{buzzer code for winner}
    {
      digitalWrite(Led3Pin, LOW);
      delay(50);
      digitalWrite(Led3Pin, HIGH);
      //Speed up the game.
      if(pause > 700)
      {
        pause -= 100;
      } else if (pause > 500)
      {
        pause -= 50;
      } else if (pause > 300)
      {
        pause -= 25;
      } else if (pause > 10)
      {
        pause -= 10;
      } else if (pause > 1)
      {
        pause -= 1;
      }
      
      while(digitalRead(13) == HIGH) {lcd.clear();};
   
      lcd.print("Score: ");
      
      lcd.println(pause);
      //lcd.clear();
      
    } else //If the pressed it at the wrong time, show their final score and start a new game.
    
    {
      //Game over
      //buzzer code for loser
      lcd.clear();
      lcd.println("GAME OVER ");
      lcd.setCursor(0, 1);
      lcd.print("Final Score ");
      lcd.println(pause);

   
      
      //Blink the Led that the player stopped on.
      for (int x = 0; x <= 10; x++)
      {
        if(digitalRead(LightPosition + 1) == LOW)
        {
          digitalWrite(LightPosition + 1, HIGH);
        }
        else
        {
          digitalWrite(LightPosition + 1, LOW);
        }
        delay(200);
      }
      
      //Show a LED bar based on how well the player did.
      digitalWrite(Led1Pin,HIGH);
      delay(500);
      if (pause < 800)
      {
        digitalWrite(Led2Pin, HIGH);
        delay(500);
      }
      if (pause < 600)
      {
        digitalWrite(Led3Pin, HIGH);
        delay(500);
      }
      if (pause < 250)
      {
        digitalWrite(Led4Pin, HIGH);
        delay(500);
      }
      if (pause < 100)
      {
        digitalWrite(Led5Pin, HIGH);
        delay(500);
      }
      delay(3000);
      
      newGame();
    }
  }
  
  old_val = digitalRead(ButtonPin);

}

//Updates the light's position.
void move(int LightPosition)
{
 //Turn off all LEDs
for (int x = Led1Pin; x <= Led5Pin;x++)
{
  digitalWrite(x, LOW);
}

//Turn on the LED
digitalWrite(LightPosition + 1, HIGH);
}

void newGame()
{
  LightPosition = 0;
  pause = 1000;
  lcd.clear();
}

First, you need a piezo "speaker" or "transducer", not a "buzzer".

A "buzzer" or "beeper" has an internal circuit that makes noise when you apply DC voltage. You can sometimes change the loudness (by varying the voltage) but it can't make different sounds.

A speaker/transducer will play whatever tone you feed-in* and it will just make a little "click" when you apply (or when you remove) DC voltage.

If you're not sure if you have a buzzer or transducer, connect it to 5V.
Try experimenting with the [u]tone()[/u] function to see what kinds of sounds you can get. You can get high & low pitch tones, but the sounds you can get from a digital-output are limited (square and rectangle waves), so you may not be able to get exactly what you are looking for.

(And, it's probably easier to write a little sound-test program rather than adding experimental code in your existing program.)

...When I was doing something similar (on an older computer with a built-in speaker and no real soundcard) I made a rising-pitch tone-sequence for "pass" using some musically-related tones (i.e. the tones that would make-up a chord) and an decreasing-pitch tone-sequence for "fail" with tones that sound "sour" when played in sequence.

  • Piezo speakers are basically tweeters. They only work with upper-mid and high frequencies (no low frequencies or bass). But, the tone() function puts-out a square wave which has lots of harmonics, so if you play a low-frequency tone you will hear the harmonics.

I meant to say that I am using a piezo speaker… sorry :frowning:

I am working on the code now, I get the piezo to constantly beep, but I haven’t been able to get it to only work when I press the button. My wiring is fine, its just in the code that I need help. I got the button to work with the buzzer on its own, but I need to incorporate the game with the buzzer now, we are terrible with coding…

Here is my code so far.

int Led1Pin = 6; //The pin of the first LED.
int Led2Pin = 7; //The pin of the second LED.
int Led3Pin = 8; //etc.
int Led4Pin = 9;
int Led5Pin = 10;
const int buttonPin = 13;     // the number of the pushbutton pin
int Buzzer1 = 1;

int ButtonPin = 13; //The pin of the button.

//The state of the button the last time we checked.
boolean old_val = LOW;

//Stores which LED is on.
int LightPosition = 0;

//How much time in between LED light changes.
int pause = 1000;

//What time it was when we last moved the light.
long lastMove = millis();

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);


void setup()
{
  pinMode(Buzzer1, OUTPUT);  
  pinMode(buttonPin, INPUT);     

  
  lcd.begin(16, 2);
 
  pinMode(Led1Pin, OUTPUT);
  pinMode(Led2Pin, OUTPUT);
  pinMode(Led3Pin, OUTPUT);
  pinMode(Led4Pin, OUTPUT);
  pinMode(Led5Pin, OUTPUT);
  pinMode(ButtonPin, INPUT);
  
  Serial.begin(9600);
  
  //Start a new game.
  newGame();
}

void loop()
{
  //Move the light.
  if(millis() - lastMove >= pause)
  {
    lastMove = millis(); //Remember when we switched LED
    
    LightPosition++; //increment the Light position.
    if(LightPosition >= 10) LightPosition = 5;
    
    move(LightPosition);//Update the light position.
  }
  
  //When the player presses the button...
  if(digitalRead(ButtonPin) == HIGH && old_val == LOW)
  {
    //If the pressed it when the light was in the middle, speed up and continue.
    if(LightPosition == 7)
      //{buzzer code for winner}
    {
      digitalWrite(Led3Pin, LOW);
      delay(50);
      digitalWrite(Led3Pin, HIGH);
      //Speed up the game.
      if(pause > 700)
      {
        pause -= 100;
      } else if (pause > 500)
      {
        pause -= 50;
      } else if (pause > 300)
      {
        pause -= 25;
      } else if (pause > 10)
      {
        pause -= 10;
      } else if (pause > 1)
      {
        pause -= 1;
      }
      
      while(digitalRead(13) == HIGH) {lcd.clear();};
   
      lcd.print("Score: ");
      
      lcd.println(pause);
      //lcd.clear();
      
    } else //If the pressed it at the wrong time, show their final score and start a new game.
    
    {
      //Game over
      //buzzer code for loser
      lcd.clear();
      lcd.println("GAME OVER ");
      lcd.setCursor(0, 1);
      lcd.print("Final Score ");
      lcd.println(pause);

   
      
      //Blink the Led that the player stopped on.
      for (int x = 0; x <= 10; x++)
      {
        if(digitalRead(LightPosition + 1) == LOW)
        {
          digitalWrite(LightPosition + 1, HIGH);
        }
        else
        {
          digitalWrite(LightPosition + 1, LOW);
        }
        delay(200);
      }
      
      //Show a LED bar based on how well the player did.
      digitalWrite(Led1Pin,HIGH);
      delay(500);
      if (pause < 800)
      {
        digitalWrite(Led2Pin, HIGH);
        delay(500);
      }
      if (pause < 600)
      {
        digitalWrite(Led3Pin, HIGH);
        delay(500);
      }
      if (pause < 250)
      {
        digitalWrite(Led4Pin, HIGH);
        delay(500);
      }
      if (pause < 100)
      {
        digitalWrite(Led5Pin, HIGH);
        delay(500);
      }
      delay(3000);
      
      newGame();
    }
  }
  
  old_val = digitalRead(ButtonPin);

}

//Updates the light's position.
void move(int LightPosition)
{
 //Turn off all LEDs
for (int x = Led1Pin; x <= Led5Pin;x++)
{
  digitalWrite(x, LOW);
}

//Turn on the LED
digitalWrite(LightPosition + 1, HIGH);
}

void newGame()
{
  LightPosition = 0;
  pause = 1000;
  lcd.clear();
}

This is the beep code that I am copying and trying to put into my lcd code…

const int buttonPin = 13;     // the number of the pushbutton pin
int Buzzer1 = 1;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the piezo as output:
  pinMode(Buzzer1, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // play the Music
      tone(Buzzer1,400,200);
      delay(500);
      tone(Buzzer1,400,200);
      delay(500);
  } 
}