Multiple Tones on Button Press

Hi,

I have a completed code, and I've read about the tone library and it seems like it would be a bit difficult for me to do what I'd like. Basically, I have a code for a hockey game already set up. So far, I haven't found out a way to play a short goal scoring tone with different frequencies and note lengths, only a buzz to indicate a goal being scored on a button press. After a button press and the corresponding tone ends, how could I add another tone say (buzzer, 1000, 500)? Thanks. Here is my code as is:

//+5 turns LED on
#define LEDon  HIGH
#define LEDoff LOW
#define Buzzeron HIGH
#define Buzzeroff LOW

//+5 turns LED off
//#define LEDon  LOW
//#define LEDoff HIGH

//=========================================
const unsigned long MyTime12 = 5*1000ul;
unsigned long Millis12;
const byte ledPin12          = 12;
const byte buttonPin2        = 2; //pushed = LOW
boolean flag12               = false;

unsigned long MyTime13;
unsigned long Millis13;
const byte    ledPin13       = 13;
const byte    buttonPin3     = 3; //pushed = LOW
boolean       flag13         = false;
byte          myState        = 0;

const unsigned long MyTime11 = 5*1000ul;
unsigned long Millis11;
const byte ledPin11          = 11;
const byte buttonPin4        = 4; //pushed = LOW
boolean flag11               = false;

const byte buzzer = 8;

//=========================================

//variable for reading the pushbutton status
int buttonState = 0;

void setup()
{
  pinMode(ledPin12, OUTPUT);
  //LED off at power on
  digitalWrite(ledPin12, LEDoff);
  pinMode(buttonPin2, INPUT_PULLUP);
  
  pinMode(ledPin11, OUTPUT);
  //LED off at power on
  digitalWrite(ledPin11, LEDoff);
  pinMode(buttonPin4, INPUT_PULLUP);
  
  pinMode(ledPin13, OUTPUT);
  //LED off at RESET
  digitalWrite(ledPin13, LEDoff);
  pinMode(buttonPin3, INPUT_PULLUP);

  pinMode (buzzer, OUTPUT);
  digitalWrite(buzzer, Buzzeroff);
 
}

void loop()
{
  //=========================================
  // read the state of the push button value:
  buttonState = digitalRead(buttonPin2);
  //LOW is pressed
  //if button pressed, start timing LED sequence
  if (flag12 == false && buttonState == HIGH)
  {
    //enable timing
    flag12 = true;
    Millis12 = millis();
    //LED on
    digitalWrite(ledPin12, LEDon);
    tone(buzzer, 400, 1500);
   }

  // read the state of the push button value:
  buttonState = digitalRead(buttonPin4);
  //LOW is pressed
  //if button pressed, start timing LED sequence
  if (flag11 == false && buttonState == HIGH)
  {
    //enable timing
    flag11 = true;
    Millis11 = millis();
    //LED on
    digitalWrite(ledPin11, LEDon);
    tone(buzzer, 400, 1500);
  }  

  //=========================================
  // read the state of the push button value
  buttonState = digitalRead(buttonPin3);    
  //LOW is pushed
  //if button pressed, start LED timing sequence
  if (flag13 == false && buttonState == HIGH)
  {
    //enable timing
    flag13 = true;
    myState = 0;
    Millis13 = millis();
    MyTime13 = 5*1000UL; //LED OFF for 180 seconds  180*1000
    digitalWrite(ledPin13, LEDoff);
    //LED will remain OFF for 180 more seconds before it comes ON for 10 sec
  }

  //=========================================
  //Time to toggle LED?
  if(flag12 == true && millis() - Millis12 >= MyTime12)
  {   //toggle ledPin12
    digitalWrite(ledPin12, !digitalRead(ledPin12));
    flag12 = false;
  }

  //Time to toggle LED?
  if(flag11 == true && millis() - Millis11 >= MyTime11)
  {   //toggle ledPin11
    digitalWrite(ledPin11, !digitalRead(ledPin11));
    flag11 = false;
  }  

  //=========================================
  //Time to toggle LED?
  if(flag13 == true && millis() - Millis13 >= MyTime13)
  {
    //get ready for next iteration
    Millis13 += MyTime13;

    switch(myState)
    {
    case 0:
      digitalWrite(ledPin13, LEDon);
      myState = 1;
      MyTime13 = 10*1000UL; //LED ON for 10 seconds
      tone(buzzer, 300, 1500);
      break;
    case 1:
      //we are now finished with this button press
      digitalWrite(ledPin13, LEDoff);
      flag13 = false;      
      break;
    }//End of switch
  }

} //END of loop()

Your debounce code doesn’t match the description. Add some debug output to find out more.

I looked up the debounce tutorial, but I'm not sure exactly how that would help me. As is, the code works fine, it is only when I try to add a second tone for each button that there are problems. Basically, the second tone won't work at all. Just to use this as an example, here is a part of my code:

digitalWrite(ledPin12, LEDon);
tone(buzzer, 400, 1500);
}

That works fine. I thought that once the millis are counted, that the buzzer would simply play a different pitch for time indicated. So, why doesn't this work properly? Thanks.

digitalWrite(ledPin12, LEDon);
tone(buzzer, 400, 1500);
tone(buzzer, 800, 1500);
}

The tone() function doesn't maintain a list of tones to play. You have to wait for the duration of a tone, before you play the next one.

The state machine switch(myState) can be extended for tone sequences. When the duration of a tone has elapsed, play the next tone (next state) and set MyTime13 to the duration of that tone. Follow the case 0 pattern for playing every tone, and the case 1 pattern as the last state (stop sound).

You also can hold the tones (pitch and duration) in an array, and increment the array index whenever a tone has been played. The last entry should contain a stop condition, like duration==0, meaning that playback should be turned off when reached.