2017年1月24日 星期二

蜂鳴器音樂盒

int sp=2;
int frequency[] = { //C調音符表
  //C(1), C+(2), D(3), D+(4), E(5), F(6), F+(7), G(8), G+(9), A(10), A+(11), B(12),
  262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
  523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
  1046, 1109, 1175, 1245, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1976
};
byte magicalprincessTone[] =
{ 10, 17, 17, 15, 17, 17,   20, 15, 0,   10, 17, 17, 15, 17, 17,
  20, 0, 17, 20,   22, 22, 22, 22, 22, 22,   20, 15, 17, 0,
  15, 15, 15, 17, 15, 10,   17, 0,   10, 17, 17, 15, 17, 20,
  15, 0,   10, 17, 17, 15, 17, 17, 20,   20, 0, 17, 20,
  22, 22, 22, 22, 22,   20, 15, 17, 0,   15, 15, 15, 17, 15, 10, 13,
  13, 0,   22, 22, 22, 24, 22, 22,   25, 24, 17, 0,
  15, 20, 17, 15, 17,   24, 24, 24, 25, 24, 20,   25, 24, 17, 0, 17,
  22, 20, 18, 20,   0, 17, 20,   22, 25, 24, 17, 20,
  22, 0, 17, 20,   22, 25, 24, 17, 20,   22,  0, 0,
};
byte magicalprincessBeat[] =     //d=84
{ 1, 1, 1, 1, 3, 1,   1, 5, 2,   1, 1, 1, 1, 3, 1,
  4, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  1, 1, 1, 1, 2, 2,   6, 2,   1, 1, 1, 1, 3, 1,
  6, 2,   1, 1, 1, 1, 1, 2, 1,   4, 2, 1, 1,
  2, 1, 1, 3, 1,   2, 2, 2, 2,   1, 1, 1, 1, 1, 2, 1,
  6, 2,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  2, 2, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 1, 1,
  1, 3, 1, 4,   2, 1, 1,   2, 2, 1, 2, 1,
  4, 2, 1, 1,   2, 2, 1, 2, 1,   12,   2, 2,
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int len=sizeof(magicalprincessTone);
  for (int i=0;i<len;i++)
  {
    tone(sp,frequency[magicalprincessTone[i]]);
    delay(351*magicalprincessBeat[i] );
  }
  noTone(sp);
}
===================================
按鈕控制歌1或歌2即停止

int sp=2;
int sw1=0;
int sw2=0;

int frequency[] = { //C調音符表
  //C(1), C+(2), D(3), D+(4), E(5), F(6), F+(7), G(8), G+(9), A(10), A+(11), B(12),
  262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
  523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
  1046, 1109, 1175, 1245, 1318, 1397, 1480, 1568, 1661, 1760, 1865, 1976
};
byte magicalprincessTone[] =
{ 10, 17, 17, 15, 17, 17,   20, 15, 0,   10, 17, 17, 15, 17, 17,
  20, 0, 17, 20,   22, 22, 22, 22, 22, 22,   20, 15, 17, 0,
  15, 15, 15, 17, 15, 10,   17, 0,   10, 17, 17, 15, 17, 20,
  15, 0,   10, 17, 17, 15, 17, 17, 20,   20, 0, 17, 20,
  22, 22, 22, 22, 22,   20, 15, 17, 0,   15, 15, 15, 17, 15, 10, 13,
  13, 0,   22, 22, 22, 24, 22, 22,   25, 24, 17, 0,
  15, 20, 17, 15, 17,   24, 24, 24, 25, 24, 20,   25, 24, 17, 0, 17,
  22, 20, 18, 20,   0, 17, 20,   22, 25, 24, 17, 20,
  22, 0, 17, 20,   22, 25, 24, 17, 20,   22,  0, 0,
};
byte magicalprincessBeat[] =     //d=84
{ 1, 1, 1, 1, 3, 1,   1, 5, 2,   1, 1, 1, 1, 3, 1,
  4, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  1, 1, 1, 1, 2, 2,   6, 2,   1, 1, 1, 1, 3, 1,
  6, 2,   1, 1, 1, 1, 1, 2, 1,   4, 2, 1, 1,
  2, 1, 1, 3, 1,   2, 2, 2, 2,   1, 1, 1, 1, 1, 2, 1,
  6, 2,   1, 1, 1, 1, 3, 1,   2, 2, 2, 2,
  2, 2, 2, 1, 1,   1, 1, 1, 1, 3, 1,   2, 2, 2, 1, 1,
  1, 3, 1, 4,   2, 1, 1,   2, 2, 1, 2, 1,
  4, 2, 1, 1,   2, 2, 1, 2, 1,   12,   2, 2,
};

byte CastleintheSkyTone[] =  
{ 22, 24,
  25, 24, 25, 29,   24, 17,   22, 20, 22, 25,   20, 17, 17,
  18, 17, 18, 25,   17, 0, 25, 25, 25,   24, 19, 18, 24,   24, 0, 22, 24,
  25, 24, 25, 29,   24, 17, 17,   22, 20, 22, 25,   20, 0, 17,
  18, 25, 24, 25,   27, 27, 29, 25,   25, 24, 22, 22, 24, 21,   22, 0, 25, 27,
  29, 27, 29, 32,   27, 20, 20,   25, 24, 25, 29,   29, 0,
  22, 24, 25, 24, 25, 27, 27,   25, 20, 20, 0,   30, 29, 27, 25,   29,
  0, 29,   34, 34, 32, 32,   29, 27, 25, 0, 25,   27, 25, 27, 32,
  29, 0, 29,   34, 34, 32,   29, 27, 25, 0, 25,   27, 25, 27, 24,
  22,   0,
};
byte CastleintheSkyBeat[] =     //d=100
{ 1, 1,
  3, 1, 2, 2,   6, 2,   3, 1, 2, 2,   6, 1, 1,
  3, 1, 2, 2,   4, 1, 1, 1, 1,   3, 1, 2, 2,   4, 2, 1, 1,
  3, 1, 2, 2,   6, 1, 1,   3, 1, 2, 2,   6, 1, 1,
  2, 1, 3, 2,   2, 1, 1, 4,   1, 1, 1, 1, 2, 2,   4, 2, 1, 1,
  3, 1, 2, 2,   6, 1, 1,   3, 1, 2, 2,   6, 2,
  1, 1, 2, 1, 1, 2, 2,   3, 1, 2, 2,   2, 2, 2, 2,   12,
  2, 2,   3, 1, 3, 1,   1, 1, 4, 1, 1,   2, 1, 3, 2,
  4, 2, 2,   3, 1, 4,   1, 1, 4, 1, 1,   2, 1, 3, 2,
  14,   2,
};
byte SpiritedAwayTone[] =
{ 13,15,
17,13,20,17,   15,20,15,   13,10,17,13,
12,0,12,   10,12,13,15,   8,13,15,17,   18,18,17,15,13,
15,13,15,   17,13,20,17,   15,20,15,   13,10,17,13,
12,0,12,   10,12,13,15,   8,13,15,17,   18,18,17,15,13,
13,   13,0,17,18,   20,20,20,
20,20,22,20,18,   17,17,17,   17,17,18,17,15,   13,13,13,12,
10,12,12,13,   15,15,17,15,17,   15,17,18,   20,20,20,
20,20,22,20,18,   17,17,17,   17,18,17,15,13,12,   10,10,12,13,15,
8,13,15,17,   15,15,15,13,   13,
};
byte SpiritedAwayBeat[] =     //d=116
{1,1,
1,1,3,1,   2,2,2,   1,1,3,1,
2,2,2,   2,2,1,1,   2,2,1,1,   2,1,1,1,1,
4,1,1,   1,1,3,1,   2,2,2,   1,1,3,1,
2,2,2,   2,2,1,1,   2,2,1,1,   2,1,1,1,1,
6,   2,2,1,1,   2,2,2,
2,1,1,1,1,   2,2,2,   2,1,1,1,1,   2,2,1,1,
2,2,1,1,   2,1,1,1,1,   4,1,1,   2,2,2,
2,1,1,1,1,   2,2,2,   1,1,1,1,1,1,   2,1,1,1,1,
2,2,1,1,   3,1,1,1,   6,
};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
    sw1 = digitalRead(4);
    sw2=digitalRead(5);

    Serial.print(sw1);
    Serial.println();
    Serial.print(sw2);
   noTone(sp);
   if (sw1==1)
   {
       int len=sizeof(magicalprincessTone);
   
      for (int i=0;i<len;i++)
      {
        int kp=digitalRead(3);
        if(kp==1)
        {
          break;
        }
        tone(sp,frequency[magicalprincessTone[i]]);
        delay(351*magicalprincessBeat[i] );
      }
      noTone(sp);
   }
   else if (sw2==1)
   {
    int len=sizeof(SpiritedAwayTone);
  for (int i=0;i<len;i++)
  {
        int kp=digitalRead(3);
        if(kp==1)
        {
          break;
        }
    tone(sp,frequency[SpiritedAwayTone[i]]);
    delay(351* SpiritedAwayBeat[i] );
  }
  noTone(sp);
   }
}

沒有留言:

張貼留言