Aurduino+maker-uno
BUZZER 8 & noTone(使用BUZZER 8和靜音) :
#define C 523
#define D 659
#define E 784
void setup() {
pinMode(8,OUTPUT);
tone(8,C);
delay(1000);
tone(8,D);
delay(1000);
tone(8,E);
delay(1000);
noTone(8);靜音
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(2)==LOW)tone(8,E,100);
}
Button 2(使用button2):
#define C 523
#define D 659
#define E 784
void setup() {
pinMode(2,INPUT_PULLUP);
pinMode(8,OUTPUT);
tone(8,C);
delay(1000);
tone(8,D);
delay(1000);
tone(8,E);
delay(1000);
noTone(8);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(2)==LOW)tone(8,E,100);
}
Processing & Aurduino+Maker-uno(Processing連結Aurduino) :
- Processing
import processing.serial.*;
Serial myPort;
void setup(){
size(200,200);
myPort=new Serial(this,"COM4",9600);
}
void draw(){
}
void keyPressed(){
if(key=='1')myPort.write("1");
if(key=='3')myPort.write("3");
if(key=='5')myPort.write("5");
}
void mousePressed(){
myPort.write("1");
}
- Aurduino+Maker-uno
#define C 523
#define D 659
#define E 784
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
pinMode(8,OUTPUT);
tone(8,C);
delay(1000);
tone(8,D);
delay(1000);
tone(8,E);
delay(1000);
noTone(8);
}
void loop() {
while(Serial.available()>0){
int now =Serial.read();
if(now=='1')tone(8,C,100);
if(now=='3')tone(8,D,100);
if(now=='5')tone(8,E,100);
}
}
完整鋼琴鍵:
- Aurduino:
#define C 523 ///設置音階
#define D 587
#define E 659
#define F 689
#define G 784
#define A 880
#define B 988
void setup() {
Serial.begin(9600);
pinMode(2,INPUT_PULLUP);
pinMode(8,OUTPUT);
tone(8,C);
delay(1000);
tone(8,D);
delay(1000);
tone(8,E);
delay(1000);
noTone(8);
}
void loop() { ///按鍵設置
while(Serial.available()>0){
int now =Serial.read();
if(now=='1')tone(8,C,100);
if(now=='2')tone(8,D,100);
if(now=='3')tone(8,E,100);
if(now=='4')tone(8,F,100);
if(now=='5')tone(8,G,100);
if(now=='6')tone(8,A,100);
if(now=='7')tone(8,B,100);
if(now=='0')noTone(8);
}
}
- Processing:
import processing.serial.*;
Serial myPort;
void setup(){
size(200,200);
myPort=new Serial(this,"COM4",9600);
}
void draw(){
}
void keyPressed(){ ///設置按鍵
if(key=='1')myPort.write("1");
if(key=='2')myPort.write("2");
if(key=='3')myPort.write("3");
if(key=='4')myPort.write("4");
if(key=='5')myPort.write("5");
if(key=='6')myPort.write("6");
if(key=='7')myPort.write("7");
}
void keyReleased(){///設置靜音鍵
myPort.write("0");
}
void mousePressed(){
myPort.write("1");
}



沒有留言:
張貼留言