Go Down

Topic: One piezo buzzer with two buttons and two melodies (Read 184 times) previous topic - next topic

elifrederick

Hello. First time posting. Beginner.

I was just wondering if anyone could help me program one piezo buzzer to play two different melodies triggered by two different buttons, one is Halloween theme and the other is Star Wars theme. I'm also a little unsure of how exactly to build the circuit. Right now, it starts immediately playing the Star Wars theme when the code is sent over without buttons being pressed.

Thanks to anyone who can help.

Photo of the circuit

Code: [Select]
/*
  Melody

  Plays a melody

  circuit:
  - 8 ohm speaker on digital pin 8
  - push button on digital pin 2
  - push button on digital pin 7

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone
*/

#include "pitches.h"



// notes in the melody:
int hallomelody[] = {
  NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4
};

int starmelody[] = {
  NOTE_G3, NOTE_G3, NOTE_G3, NOTE_D4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_D4
};

int halloween = 2;
int starwars = 7;

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
};

int counterDurations[] = {
  6, 6, 6, 1, 1, 6, 6, 6, 1, 2, 6, 6, 6, 1, 2, 6, 6, 6, 1
};

void setup(){
  //make the button's pin input
  pinMode(halloween, INPUT);
  pinMode(starwars, INPUT);
}

void loop() {

  //read the input pin 
  int buttonState = digitalRead(halloween);

  //if the button is pressed
  if (buttonState == 1) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <20; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / noteDurations [thisNote];
      tone(8, hallomelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
   
 
  //read the input pin 
  int buttonState = digitalRead(starwars);

  //if the button is pressed
  if (buttonState == 1) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <19; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / counterDurations [thisNote];
      tone(8, starmelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    } } } } }

slipstick

You're creating two variables both called buttonState. Not a good idea. Instead of that just use digitalRead(halloween) or digitalRead(starwars) directly in the if statements. Also the } to end your two for loops are in the wrong places. Each loop should just play its notes and then end.

How are your buttons wired? Pull up or pull down resistors fitted?

Steve

elifrederick

Thank you so much, Steve. Got it working!

I'm using pull-down resistors. I fixed the brackets and threw in digitalRead in place of buttonState and that fixed everything.

Thank you.

Final product:

Code: [Select]
/*
  Two melodies on two buttons and one speaker

  Plays a melody determined by button pushed

  circuit:
  - 8 ohm speaker on digital pin 8
  - push button on digital pin 2
  - push button on digital pin 7

  created 21 Jan 2010
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Tone
*/

#include "pitches.h"



// notes in the melody:
int halloweenMelody[] = {
  NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_FS4, NOTE_CS5, NOTE_FS4, NOTE_D5, NOTE_FS4
};

int starwarsMelody[] = {
  NOTE_G3, NOTE_G3, NOTE_G3, NOTE_D4, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_C5, NOTE_G4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_D4
};

int halloween = 2;
int starwars = 7;

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6
};

int counterDurations[] = {
  6, 6, 6, 1, 1, 6, 6, 6, 1, 2, 6, 6, 6, 1, 2, 6, 6, 6, 1
};

void setup(){
  //make the button's pin input
  pinMode(halloween, INPUT);
  pinMode(starwars, INPUT);
}

void loop() {


  //if the button is pressed
  if (digitalRead(halloween) == 1) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <20; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / noteDurations [thisNote];
      tone(8, halloweenMelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    } }
 

  //if the button is pressed
  if (digitalRead(starwars) == 1) {

    //iterate over the notes of the melody
    for (int thisNote=0; thisNote <19; thisNote++) {

      //to calculate the note duration, take one second. Divided by the note type
      int noteDuration = 1000 / counterDurations [thisNote];
      tone(8, starwarsMelody [thisNote], noteDuration);

      //to distinguish the notes, set a minimum time between them
      //the note's duration +30% seems to work well
      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      //stop the tone playing
      noTone(8);
    }}}

Go Up