2019年11月28日 星期四

week12 互動技術

第一步:(可以按出單音do,re,mi)

程式碼:

int Do=523;
int Re=587;
int Mi=659;
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(digitalRead(2)==LOW) tone(8,Mi,1000);
  else noTone(8);
}

第二步:(同鍵按出不同音)

程式碼:

int Do=523;
int Re=587;
int Mi=659;
int song[9]={Do,Re,Mi,Do,Do,Re,Mi,Do,Mi};
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() {
  if(UP&&digitalRead(2)==LOW){
    tone(8,song[now],100);
    UP=false;
    now=(now+1)%9;
  }
  else{
    UP=true;
  }
  delay(1000);
}


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() {
  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,689,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端連接Server端)

Client端程式碼:

import processing.net.*;
Client client;
void serup(){
   size(200,200);
   client=new Client(this,"120.125.70.41",7777);
}
void mousePressed(){
   client.write("Hello");
}

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);  
 }
}

沒有留言:

張貼留言

alanhc 互動技術-week17 [final]

回顧這學期的作品:  期中作業:LANDING:PLANET 賣點&特點: 炫麗的特效 物理(星球重力及降落)及粒子系統(噴射) 世界地圖可根據視角縮放 困難點: 重寫3次最終改寫成物件導向的CLASS寫法...