1. 先複習上週的進度: Blink 及你們做的 Knight Rider 燈
1.1. Step 1: Prepare Hardware and Software
http://arduino.cc/en/Guide/Windows
1.2. Step 2: Try the Example Blink
http://arduino.cc/en/Guide/Windows
1.3. Step 3: Write Your Code
2. 我們來試試用 按鈕 Button 吧!
2.1. Arduino: File - Examples - 2.Digital - Button
http://www.arduino.cc/en/Tutorial/Button
3. 我們來試試用 蜂鳴器 來發出聲音吧!
3.1. Arduino: File - Examples - 2.Digital - toneMelody
http://arduino.cc/en/Tutorial/Tone
4.
http://www.arduino.cc/en/Tutorial/PlayMelody
http://www.arduino.cc/en/Tutorial/InputPullupSerial
介紹 Piano的 440Hz frequency
http://en.wikipedia.org/wiki/Piano_key_frequencies
http://arduino.cc/en/reference/tone
http://blog.xuite.net/meifang5603/97307/67703417-%E5%B0%8F%E8%9C%9C%E8%9C%82%E7%B0%A1%E8%AD%9C
▏5 3 3 - ▏4 2 2 - ▏1 2 3 4 ▏5 5 5 - ▏
嗡嗡嗡 嗡嗡嗡 大家一起 勤作工
▏5 3 3 - ▏4 2 2 - ▏1 3 5 5 ▏3 - - - ▏
嗡嗡嗡 嗡嗡嗡 做工趣味 濃
▏2 2 2 2 ▏2 3 4 - ▏3 3 3 3 ▏3 4 5 –▏
天暖花好 不做工 將來哪裡 好過冬
▏5 3 3 - ▏4 2 2 - ▏1 3 5 5 ▏1 - - - ▏
嗡嗡嗡 嗡嗡嗡 別學懶惰 蟲
練習用 tone() 發聲音
void setup(){
tone(8, 440*2, 1000);
}
void loop(){
}
將 myButton 與 tone() 做結合
void setup(){
pinMode(2, INPUT_PULLUP);//no resistor
pinMode(13, OUTPUT);
}
void loop(){
if(digitalRead(2)==HIGH){
digitalWrite(13, HIGH);
tone(8, 440*2, 300);
}else{
digitalWrite(13, LOW);
noTone(8);
}
}
將 myButton 與 PC 用 Serial 做連結 (參考 Processing-Examples-Libraries-Serial-SimpleRead)
void setup(){
pinMode(2, INPUT_PULLUP);//no resistor
pinMode(13, OUTPUT);
Serial.begin(9600);/////Serial
}
void loop(){
if(digitalRead(2)==HIGH){
digitalWrite(13, HIGH);
tone(8, 440*2, 300);
}else{
digitalWrite(13, LOW);
Serial.write('L'); //////Serial
noTone(8);
}
}
改裝 Melody 變成可以因為按鍵而中斷
#include "pitches.h"
int melody[] = {NOTE_G4, NOTE_E4,NOTE_E4, 0, NOTE_F4, NOTE_D4,NOTE_D4, 0,
NOTE_C4, NOTE_D4,NOTE_E4,NOTE_F4,NOTE_G4,NOTE_G4,NOTE_G4,0,
NOTE_G4, NOTE_E4,NOTE_E4, 0, NOTE_F4, NOTE_D4,NOTE_D4, 0,
NOTE_C4, NOTE_E4,NOTE_G4,NOTE_G4,NOTE_E4,0
};
int noteDurations[] = { 4,4,2,16, 4,4,2,16, 4,4,4,4, 4,4,4,4, 4,4,2,8, 4,4,2,8, 4,4,4,4, 2,4};
void setup(){
pinMode(2, INPUT_PULLUP);
}
void loop(){
for (int thisNote = 0; thisNote < 30; thisNote++) {
if(digitalRead(2)==LOW){
noTone(8); break;
}
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
}
}

沒有留言:
張貼留言