心得:
我們成功做出五個按鈕,個代表五個音階,簡直就是簡易型的小鋼琴,
一開始有些瓶頸,後來詢問修改後,成功的做出來了,
很有趣也很好玩,感覺又學到了一些東西!
曲子是小蜜蜂~
int val[6]; int sensorPin = A0; int sensorValue = 0; void setup(){ for(int i=2;i<7;i++) pinMode(i, INPUT); } void loop(){ sensorValue = analogRead(sensorPin); for(int i=2;i<7;i++) val[i-2] = digitalRead(i); for(int i=0;i<6;i++) if (val[i] == HIGH) { switch(i){ case 0 : tone(8,262+sensorValue,4); break; case 1 : tone(8,294+sensorValue,4); break; case 2 : tone(8,330+sensorValue,4); break; case 3 : tone(8,349+sensorValue,4); break; case 4 : tone(8,392+sensorValue,4 ); break; } } }
*/ #include "pitches.h" const int buttonPin1 = 3; // the number of the pushbutton pin const int buttonPin2 = 4; const int buttonPin3 = 5; const int buttonPin4 = 6; const int buttonPin5 = 7; const int ledPin = 8; // the number of the LED pin // variables will change: int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; // notes in the melody: int melody[] = { NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4}; // note durations: 4 = quarter note, 8 = eighth note, etc.: int noteDurations[] = { 4, 8, 8, 4,4,4,4,4 }; void setup() { // iterate over the notes of the melody: // initialize the LED pin as an output: pinMode(8, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); for (int thisNote = 0; thisNote < 8; thisNote++) { // to calculate the note duration, take one second // divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000/noteDurations[thisNote]; // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); // stop the tone playing: noTone(8); } } void loop() { buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); buttonState4 = digitalRead(buttonPin4); buttonState5 = digitalRead(buttonPin5); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState1 == HIGH) { // turn LED on: tone(8,NOTE_C3,4); digitalWrite(8, HIGH); } else if (buttonState2 == HIGH) { // turn LED on: tone(8,NOTE_D3,4); digitalWrite(8, HIGH); } else if (buttonState3 == HIGH) { // turn LED on: tone(8,NOTE_E3,4); digitalWrite(8, HIGH); } else if (buttonState4 == HIGH) { // turn LED on: tone(8,NOTE_F3,4); digitalWrite(8, HIGH); } else if (buttonState5 == HIGH) { // turn LED on: tone(8,NOTE_G3,4); digitalWrite(8, HIGH); } else { // turn LED off: digitalWrite(8, LOW); } }
4.本周上課心得
今天利用到五個按鈕,做出鋼琴的樣子,其實一開始還是有卡關, 在於程式部分和案件硬體的理解稍微慢了一些些,
硬體的輸出腳以及程式的搭配,要需要小心一點,
因為很容易就被卡住,所以我們這次學到了必須先建立好程式邏輯才開始動手
我們之後練習的步驟就打算利用這樣的做法來試試看,
已有最有效率的方法達到最好的學習成效,
很開心我這周又學到了一點東西。
int val[6]; void setup(){ for(int i=2;i<7;i++) pinMode(i, INPUT_PULLUP); } void loop(){ for(int i=2;i<7;i++) val[i-2] = digitalRead(i); for(int i=0;i<6;i++) if (val[i] == LOW) { switch(i){ case 0 : tone(8,262,4); break; case 1 : tone(8,294,4); break; case 2 : tone(8,330,4); break; case 3 : tone(8,349,4); break; case 4 : tone(8,392,4); break; } } }
int val[6]; int sensorPin = A0; int sensorValue = 0; void setup(){ for(int i=2;i<7;i++) pinMode(i, INPUT_PULLUP); } void loop(){ sensorValue = analogRead(sensorPin); for(int i=2;i<7;i++) val[i-2] = digitalRead(i); for(int i=0;i<6;i++) if (val[i] == LOW) { switch(i){ case 0 : tone(8,262+sensorValue,4); break; case 1 : tone(8,294+sensorValue,4); break; case 2 : tone(8,330+sensorValue,4); break; case 3 : tone(8,349+sensorValue,4); break; case 4 : tone(8,392+sensorValue,4 ); break; } } }
int speakerPin = 9; int length = 24; // the number of notes char notes[] = "geefddcdefggggeefddceggc "; // a space represents a rest int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4}; int tempo = 300; void playTone(int tone, int duration) { for (long i = 0; i < duration * 1000L; i += tone * 2) { digitalWrite(speakerPin, HIGH); delayMicroseconds(tone); digitalWrite(speakerPin, LOW); delayMicroseconds(tone); } } void playNote(char note, int duration) { char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 }; // play the tone corresponding to the note name for (int i = 0; i < 8; i++) { if (names[i] == note) { playTone(tones[i], duration); } } } void setup() { pinMode(speakerPin, OUTPUT); } void loop() { for (int i = 0; i < length; i++) { if (notes[i] == ' ') { delay(beats[i] * tempo); // rest } else { playNote(notes[i], beats[i] * tempo); } // pause between notes delay(tempo / 2); } }2.可用按鍵彈音階
const int buttonPin1 = 2; const int buttonPin2 = 3; const int buttonPin3 = 4; const int buttonPin4 = 5; const int buttonPin5 = 6; int speakerPin = 9; int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; void setup() { pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); } void loop() { buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); buttonState4 = digitalRead(buttonPin4); buttonState5 = digitalRead(buttonPin5); int duration; if (buttonState1 == HIGH) tone(speakerPin, 261,duration); //tone(pin, frequency, duration) else if (buttonState2 == HIGH) tone(speakerPin, 294,duration); else if (buttonState3 == HIGH) tone(speakerPin, 329,duration); else if (buttonState4 == HIGH) tone(speakerPin, 349,duration); else if (buttonState5 == HIGH) tone(speakerPin, 392,duration); else { noTone(speakerPin); } }
3.心得
這節課教我們用 Arduino 撥放音樂,還有搭配按鈕彈音階, 讓我想到可以做鋼琴的小遊戲。
而且他的程式碼跟原裡沒有很難,董元李後就可以延伸很多不同的曲子。
可惜這節課當時沒有借到手機或相機,沒有把接出來的執行樣子拍攝下來,
有機會再用得獎的Arduino 再接一次~
int inpin1=3; int inpin2=4; int inpin3=5; int inpin4=6; int inpin5=7; int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; void setup() { pinMode(inpin1, INPUT); pinMode(inpin2, INPUT); pinMode(inpin3, INPUT); pinMode(inpin4, INPUT); pinMode(inpin5, INPUT); } void loop() { buttonState1 = digitalRead(inpin1); buttonState2 = digitalRead(inpin2); buttonState3 = digitalRead(inpin3); buttonState4 = digitalRead(inpin4); buttonState5 = digitalRead(inpin5); if (buttonState1 == HIGH) tone(9, 524,4); else if (buttonState2 == HIGH) tone(9, 588,4); else if (buttonState3 == HIGH) tone(9, 660,4); else if (buttonState4 == HIGH) tone(9, 698,4); else if (buttonState5 == HIGH) tone(9, 784,4); else { noTone(9); } }