這次將Arduino結合了音樂,利用簡單的開關或是光敏電阻來達到很不錯的效果。
演奏的歌曲是倫敦鐵橋,因為開關不夠所以只能到la音階。
不然原本想要彈奏更多音階的歌曲的!而且覺得挑歌比實作還困難啊!XD
程式碼部分:(對不起我寫的結構不太好= =)
#define c 3830 // 261 Hz #define d 3400 // 294 Hz #define e 3038 // 329 Hz #define f 2864 // 349 Hz #define g 2550 // 392 Hz #define a 2272 // 440 Hz #define b 2028 // 493 Hz #define C 1912 // 523 Hz #define R 0 int speakerOut = 10; int tone; int inPin1 = 5; int inPin2 = 6; int inPin3 = 7; int inPin4 = 8; int inPin5 = 9; int inPin6 = 11; int val1 = 0; int val2 = 0; int val3 = 0; int val4 = 0; int val5 = 0; int val6 = 0; void setup() { pinMode(speakerOut, OUTPUT); pinMode(inPin1, INPUT); pinMode(inPin2, INPUT); pinMode(inPin3, INPUT); pinMode(inPin4, INPUT); pinMode(inPin5, INPUT); pinMode(inPin6, INPUT); Serial.begin(9600); // 設定序列埠的速度為9600bps } void loop() { val1 = digitalRead(inPin1); val2 = digitalRead(inPin2); val3 = digitalRead(inPin3); val4 = digitalRead(inPin4); val5 = digitalRead(inPin5);a val6 = digitalRead(inPin6); // Serial.println(val); // delay(100); //tone = 2028; if(val1==HIGH){ digitalWrite(speakerOut,HIGH); delayMicroseconds(3830 / 2); digitalWrite(speakerOut, LOW); delayMicroseconds(3830 / 2); } if(val2==HIGH){ digitalWrite(speakerOut,HIGH); delayMicroseconds(3400 / 2); digitalWrite(speakerOut, LOW); delayMicroseconds(3400 / 2); } if(val3==HIGH){ digitalWrite(speakerOut,HIGH); delayMicroseconds(3038 / 2); digitalWrite(speakerOut, LOW); delayMicroseconds(3038 / 2); } if(val4==HIGH){ digitalWrite(speakerOut,HIGH); delayMicroseconds(2864 / 2); digitalWrite(speakerOut, LOW); delayMicroseconds(2864 / 2); } if(val5==HIGH){ digitalWrite(speakerOut,HIGH); delayMicroseconds(2550 / 2); digitalWrite(speakerOut, LOW); delayMicroseconds(2550 / 2); } if(val6==HIGH){ digitalWrite(speakerOut,HIGH); delayMicroseconds(2272 / 2); digitalWrite(speakerOut, LOW); delayMicroseconds(2272 / 2); } }
0 回應:
張貼留言