按按鈕才會發出聲音
void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);//pin8輸出(聲音)
pinMode(2,INPUT_PULLUP);//pin2按鈕,輸入聲音
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(2)==LOW)tone(8,520,1000);//讀到2:按下去為LOW 在pin8輸出音頻520,一秒鐘
else noTone(8);//不然pin8沒有音頻
}
按一個按鈕發出小蜜蜂
int Do=523;
int re=587;
int mi=659;
int fa=698;
int sol=784;
int song[13]={sol,mi,mi,fa,re,re,Do,re,mi,fa,sol,sol,sol};//宣告陣列
int now=0;//按按鍵的次數
bool up=true;
void setup() {
// put your setup code here, to run once:
pinMode(8,OUTPUT);
pinMode(2,INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
if(up&&digitalRead(2)==LOW){
tone(8,song[now],300);//發出陣列的聲音
up=false;
now=(now+1)%13;//讓數值可以重複播放
}
else {
up=true;
}
delay(100);//因為會有雜訊所以要delay
}
用單芯線接地並插pin發出聲音
void setup() {
// put your setup code here, to run once:
for(int i=2;i<=13;i++){
pinMode(i,INPUT_PULLUP);//讓所有燈亮
}
pinMode(8,OUTPUT);//發出聲音
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(2)==LOW)tone(8,523,200);
if(digitalRead(3)==LOW)tone(8,587,200);
if(digitalRead(4)==LOW)tone(8,659,200);
if(digitalRead(5)==LOW)tone(8,698,200);
if(digitalRead(6)==LOW)tone(8,784,200);
if(digitalRead(7)==LOW)tone(8,880,200);
if(digitalRead(9)==LOW)tone(8,988,200);
if(digitalRead(10)==LOW)tone(8,1047,200);
}
自己連線自己
Client端
import processing.net.*;
Client client;
void setup(){
size(200,200);
client=new Client(this,"120.125.70.33",7777);//server端的IP
}
void draw(){
}
void mousePressed(){
client.write("Irene is pretty");
}
Server端
import processing.net.*;
Server server;
void setup(){
size(300,300);
server=new Server(this,7777);
}
void draw(){
Client thisClient=server.available();
while(thisClient!=null){
String now=thisClient.readString();
if(now!=null) println("Server received:"+now);
}
}




沒有留言:
張貼留言