/**
Mini piano for Arduino.
You can control the colorful buttons with your keyboard:
After starting the simulation, click anywhere in the diagram to focus it.
Then press any key between 1 and 8 to play the piano (1 is the lowest note,
8 is the highest).
Copyright (C) 2021, Uri Shaked. Released under the MIT License.
*/
// 2017/5/13
// Faya-Nugget 範例程式 (EMBuzzer_3.ino)
// 單元: 模組介紹-faya蜂鳴器模組
// 網址: http://fayalab.blogspot.com/2017/05/faya_11.html
// 目標 (1) 利用Arduino所提供的pitch.h音階列表,撥放小星星
// 接線: Arduino ==> faya模組
// 8 ==> BZ (蜂鳴器模組)
#include "pitches.h"
// 蜂鳴器腳位設定
int Buzzer_Pin_BZ = 8;
//小星星的音階
int star[]={NOTE_C4,NOTE_C4,NOTE_G4,NOTE_G4,NOTE_A4,NOTE_A4,NOTE_G4, \
NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_C4, \
NOTE_G4,NOTE_G4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4, \
NOTE_G4,NOTE_G4,NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4, \
NOTE_C4,NOTE_C4,NOTE_G4,NOTE_G4,NOTE_A4,NOTE_A4,NOTE_G4, \
NOTE_F4,NOTE_F4,NOTE_E4,NOTE_E4,NOTE_D4,NOTE_D4,NOTE_C4};
//每個音階的長度
int beat[]={ 2,2,2,2, 2,2,1, \
2,2,2,2, 2,2,1, \
2,2,2,2, 2,2,1, \
2,2,2,2, 2,2,1, \
2,2,2,2, 2,2,1, \
2,2,2,2, 2,2,1,};
void setup()
{
pinMode(Buzzer_Pin_BZ,OUTPUT);
}
void loop() {
for(int i=0; i<=41; i++)
{
int duration = 1000/beat[i];
tone(Buzzer_Pin_BZ, star[i], duration); // 播放C5,D5,E5,F5,G5,A5,B5,C6,各0.5秒
int pauseBetweenNote=duration * 1.15;
delay(pauseBetweenNote); // 每個音間隔0.2秒
}
}