When the switch is on, it will play a simple melody and when it is not, the speaker will emit an annoying noise.

 

 
const int SPEAKER_PIN = 12;
const int SWITCH_PIN = 2;
// Note tones taken from https://www.arduino.cc/en/Tutorial/ToneMelody?from=Tutorial.Tone
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523

const int melody[] = {
 NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5
};

const int noteDurations[] = {
 4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
    Serial.begin(9600);
    pinMode(SWITCH_PIN, INPUT);
    pinMode(SPEAKER_PIN, OUTPUT);
}

void loop() {
    int value = digitalRead(SWITCH_PIN);
    Serial.println(value);
    if (value == 1) {
     for (int thisNote = 0; thisNote < 8; thisNote++) {
        // Duration calculation also from https://www.arduino.cc/en/Tutorial/ToneMelody?from=Tutorial.Tone
        int noteDuration = 1000 / noteDurations[thisNote];
        tone(SPEAKER_PIN, melody[thisNote], noteDuration);
        // Set minimum time between notes
        int pauseBetweenNotes = noteDuration * 1.30;
        delay(pauseBetweenNotes);
     }
    }
    else {
      tone(SPEAKER_PIN, 1000);
    }
}