十字搖桿音樂盒

 const int buzzer = 8;

const int joystickX = A1;

const int joystickY = A0;


#define NOTE_0 0

#define NOTE_1 262

#define NOTE_2 294

#define NOTE_3 330

#define NOTE_4 349

#define NOTE_5 392

#define NOTE_6 440

#define NOTE_7 494

#define NOTE_H1 523

#define NOTE_H2 587

#define NOTE_H3 659

#define NOTE_H4 698

#define NOTE_H5 784


// 小星星 & 歡樂頌(簡化版)

int songMelodies[2][16] = {

  {NOTE_1, NOTE_1, NOTE_5, NOTE_5, NOTE_6, NOTE_6, NOTE_5, NOTE_0,

   NOTE_4, NOTE_4, NOTE_3, NOTE_3, NOTE_2, NOTE_2, NOTE_1, NOTE_0}, // 小星星


  {NOTE_3, NOTE_3, NOTE_4, NOTE_5, NOTE_5, NOTE_4, NOTE_3, NOTE_2,

   NOTE_1, NOTE_1, NOTE_2, NOTE_3, NOTE_3, NOTE_2, NOTE_2, NOTE_0}  // 歡樂頌

};


int songDurations[2][16];


unsigned long lastEffectTime = 0;

const unsigned long effectInterval = 5000;


// 馬利歐主題曲(精準版)

int marioMelody[] = {

  660, 660, 0, 660, 0, 520, 660, 0, 440, 0, 0, 520, 0,

  440, 0, 0, 349, 0, 392, 0, 440, 0, 0, 349, 0, 392, 0, 440,

  0, 494, 494, 494, 0, 392, 0, 440, 0, 349, 0, 392, 0, 330,

  0, 349, 0, 294, 0, 330, 0, 262, 0, 294, 0, 220, 0, 262

};


int marioDurations[] = {

  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,

  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,

  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,

  100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100

};


// 生日快樂歌(精準版)

int birthdayMelody[] = {

  262, 262, 294, 262, 349, 330,

  262, 262, 294, 262, 392, 349,

  262, 262, 523, 440, 349, 330, 294,

  466, 466, 440, 349, 392, 349

};


int birthdayDurations[] = {

  400, 400, 800, 800, 800, 1600,

  400, 400, 800, 800, 800, 1600,

  400, 400, 800, 800, 800, 800, 1600,

  400, 400, 800, 800, 800, 1600

};


void setup() {

  pinMode(buzzer, OUTPUT);

  for (int s = 0; s < 2; s++) {

    for (int i = 0; i < 16; i++) {

      songDurations[s][i] = 4;

    }

  }

}


void loop() {

  int xValue = analogRead(joystickX);

  int yValue = analogRead(joystickY);

  int selectedSong = -1;


  if (xValue < 200) {

    selectedSong = 0; // 左 → 小星星

  } else if (xValue > 800) {

    selectedSong = 1; // 右 → 歡樂頌

  } else if (yValue > 800) {

    selectedSong = 2; // 上 → 生日快樂

  } else if (yValue < 200) {

    selectedSong = 3; // 下 → 馬利歐

  }


  if (selectedSong == 0 || selectedSong == 1) {

    for (int i = 0; i < 16; i++) {

      int note = songMelodies[selectedSong][i];

      int duration = 1000 / songDurations[selectedSong][i];

      if (note > 0) {

        tone(buzzer, note, duration);

      }

      delay(duration * 1.3);

      noTone(buzzer);


      xValue = analogRead(joystickX);

      yValue = analogRead(joystickY);

      if ((xValue < 200 && selectedSong != 0) ||

          (xValue > 800 && selectedSong != 1)) {

        break;

      }

    }

  } else if (selectedSong == 2) {

    playBirthdaySong();

  } else if (selectedSong == 3) {

    playMarioTheme();

  } else {

    unsigned long currentTime = millis();

    if (currentTime - lastEffectTime >= effectInterval) {

      playEffect();

      lastEffectTime = currentTime;

    }

  }

}


void playMarioTheme() {

  for (int i = 0; i < sizeof(marioMelody) / sizeof(marioMelody[0]); i++) {

    int note = marioMelody[i];

    int duration = marioDurations[i];

    if (note > 0) {

      tone(buzzer, note, duration);

    }

    delay(duration * 1.3);

    noTone(buzzer);


    int x = analogRead(joystickX);

    int y = analogRead(joystickY);

    if (y > 800 || y < 200 || x > 800 || x < 200) break;

  }

}


void playBirthdaySong() {

  for (int i = 0; i < sizeof(birthdayMelody) / sizeof(birthdayMelody[0]); i++) {

    int note = birthdayMelody[i];

    int duration = birthdayDurations[i];

    if (note > 0) {

      tone(buzzer, note, duration);

    }

    delay(duration * 1.3);

    noTone(buzzer);


    int x = analogRead(joystickX);

    int y = analogRead(joystickY);

    if (y < 200 || y > 800 || x < 200 || x > 800) break;

  }

}


void playEffect() {

  int effectNotes[] = {NOTE_5, NOTE_H1, NOTE_5, NOTE_H1};

  int effectDuration = 150;


  for (int i = 0; i < 4; i++) {

    tone(buzzer, effectNotes[i], effectDuration);

    delay(effectDuration * 1.3);

    noTone(buzzer);

  }

}


留言