{
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(2,INPUT);//do
pinMode(3,INPUT);//re
pinMode(4,INPUT);//mi
pinMode(9,OUTPUT);
/* 這里用了一個sizeof函數(shù), 可以查出tone序列里有多少個音符 */
length = sizeof(tune)/sizeof(tune[0]);
pinMode(buttonPin, INPUT_PULLUP);
lcd.clear();
}
int p;
void loop()
{
// set the cursor to column 0, line 1
lcd.setCursor(0, 0);
lcd.print("MUSIC PLAYER");//print name
//print name
delay(750);//delay of 0.75sec
// set the cursor to column 0, line1
noTone(9);
if(digitalRead(2)==HIGH)
{
tone(9,523);
lcd.setCursor(0, 1);
lcd.print("DO");
}
else if(digitalRead(3)==HIGH)
{
tone(9,587);
lcd.setCursor(0, 1);
lcd.print("RE");
}
else if(digitalRead(4)==HIGH)
{
tone(9,659);
lcd.setCursor(0, 1);
lcd.print("MI");
}
delay(5);
for (int x = 0; x < length; x++)
{
if (digitalRead(buttonPin) == HIGH) {//如果讀取到按鍵被按壓改變ledStart
ledState = !ledState ;
delay(500);//這個延遲是你按壓按鍵的大概時間
p = x;//記錄當(dāng)前播放的音樂的位置
}