Weird buzzing sound after loop

So this code is set so that when the song finishes, it waits 2 seconds, than replays it. But for some reason, after it's done playing, the buzzer plays a very annoying buzzing sound before going silent for the 2 seconds, than replaying the song, and the cycle continues. Does anyone know what the problem might be? Could it be a wiring issue?

The Code

#include <Servo.h>

const byte buzzerPin = 8;
const byte pinSwitch1 = 2;
const byte pinSwitch2 = 3;
const byte ledPin = 13;
const byte ledPin1 = 12;
const byte pinServo = 9;

// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)
const int songLength = 31;
const char notes[songLength] = "eee eee egcde fff ffee eeggfdc " ; // a space represents a rest
const int  beats[songLength] = { 2,2,3,1,2,2,3,1,2,2,3,1,4,4,2,2,3,0,1,2,2,2,0,1,1,2,2,2,2,4,4 };
const int tempo = 150;

Servo 
    servo;
    


void setup() 
{
    pinMode( buzzerPin, OUTPUT );
    pinMode( ledPin, OUTPUT );
    pinMode( ledPin1, OUTPUT );
  
    pinMode( pinSwitch1, INPUT );   //you'll need pull-down resistors on these inputs; closing switch sets it high
    pinMode( pinSwitch2, INPUT );
    
    servo.attach( pinServo ); //other
  servo.write(180);

}//setup

void loop() 
{
    PlaySong();
    MoveServo();
    
}//loop

#define GET_NOTE        0
#define PLAY_NOTE       1
#define SONG_PAUSE      2
void PlaySong( void )
{
    static byte
        statePlay = GET_NOTE;
    static int
        noteNumber = 0;
    static unsigned long
        noteDuration = 0,
        noteTime = 0;
    unsigned long
        timeNow;

    switch( statePlay )
    {
        case    GET_NOTE:
            if( notes[noteNumber] != ' ' )
            {
                digitalWrite( ledPin, HIGH ); 
                digitalWrite( ledPin1, HIGH ); 
                tone( buzzerPin, frequency( notes[noteNumber] ) );
                
            }//if
            
            noteDuration = beats[noteNumber] * tempo;  // length of note/rest in ms
            noteTime = millis();
            statePlay = PLAY_NOTE;
            
        break;
        case    PLAY_NOTE:
            if( millis() - noteTime < noteDuration )
                return;
                
            tone( buzzerPin, 0 );
            pinMode( buzzerPin, INPUT );
            digitalWrite( ledPin, LOW ); 
            digitalWrite( ledPin1, LOW ); 
            if( noteNumber < songLength )
            {
                noteNumber++;
                statePlay = GET_NOTE;
                
            }//if
            else
            {
                //after song has finished, wait 2-seconds then play it again
                noteNumber = 0;
                noteTime = millis();
                statePlay = SONG_PAUSE;
                
            }//else
            
        break;

        case    SONG_PAUSE:
            if( millis() - noteTime < 2000 )
                return;
                
            statePlay = GET_NOTE;
            
        break;
        
    }//switch
    
}//PlaySong

void MoveServo( void )
{
    static int
        pos = 0;
    static unsigned long
        timeServo = 0;
    unsigned long
        timeNow;

    timeNow = millis();
    if( (timeNow - timeServo) < 5 )
        return;
    timeServo = timeNow;
    
    if( digitalRead( pinSwitch1 ) == HIGH )
    {
      servo.write(180);
        
    }//if

    if( digitalRead( pinSwitch2 ) == HIGH )
    {
      servo.write(0);
        
    }//if
    
}//MoveServo

int frequency( char note ) 
{
    switch( note )
    {
        case    'c': return 262; break;
        case    'd': return 294; break;
        case    'e': return 330; break;
        case    'f': return 349; break;
        case    'g': return 392; break;
        case    'a': return 440; break;
        case    'b': return 494; break;
        case    'C': return 523; break;
        default: return 0; break;
        
    }//switch
    
}//frequency

My Wiring

The index of the first note is note zero, and the index of the last is note 30, for a total of 31 notes.

Consider this section of your code, when the program finishes playing the last note:

        case    PLAY_NOTE:
            if( millis() - noteTime < noteDuration )
                return;
                  <turn off buzzer and LEDs>
            if( noteNumber < songLength )
            {
                noteNumber++;
                statePlay = GET_NOTE;
                
            }//if
            else
            {
               <other stuff>
            }//else
           
        break;

When the duration has elapsed, the buzzer and LED turn off. Then, noteNumber is tested against songLength. noteNumber is 30, the index of the last note, and songlength is 31, the length of the note string, and of the duration array. noteNumber is less than songLength , so the test passes. noteNumber is incremented to 31 - beyond the end of the note string and the duration array - and the program soon tries to get note 31. That's random data beyond the end of the array, so the note and the duration will be some unexpected value. I think that's what's wrong.

Try incrementing noteNumber before testing it against songLength. That way, when you finish the last note, noteNumber will increment beyond the end of its arrays and be equal to songlength. The test will fail, and control will pass to the else block, setting the state variable to SONG_PAUSE, as we'd expect.

Thank you! Your solution works great!