2020年11月30日 星期一

❀week12

 硬體軟體概念











發出Do Mi So

程式碼:

#define Do 523

#define Mi 659

#define So 784

void setup() {

  // put your setup code here, to run once:

  pinMode(8, OUTPUT);

  tone(8,Do);//不給長度,一直發音

  delay(1000);//等一秒

  tone(8,Mi);//換另一個聲音

  delay(1000);

  tone(8,So);//再換另一個聲音

  delay(1000);

  noTone(8);//靜音

}


void loop() {

  // put your main code here, to run repeatedly:

}


加入Button互動

1.setup()加pinMode(2, INPUT_PULLUP);
2.loop()加if(digitalRead(2)==LOW) tone(8,Mi,100);

程式碼:

#define Do 523

#define Mi 659

#define So 784

void setup() {

  // put your setup code here, to run once:

  pinMode(2, INPUT_PULLUP);

  pinMode(8, OUTPUT);

  tone(8,Do);//不給長度,一直發音

  delay(1000);//等一秒

  tone(8,Mi);//換另一個聲音

  delay(1000);

  tone(8,So);//再換另一個聲音

  delay(1000);

  noTone(8);//靜音

}


void loop() {

  // put your main code here, to run repeatedly:

  if(digitalRead(2)==LOW) tone(8,Mi,100);

}


用processing傳1給Arduino

程式碼:

import processing.serial.*;

Serial myPort;

void setup(){

  size(200,200);

  myPort = new Serial(this,"COM5",9600);

}

void draw(){

  

}

void mousePressed(){

  myPort.write("1");

}




















用processing傳1,3,5給Arduino

程式碼:

import processing.serial.*;

Serial myPort;

void setup(){

  size(200,200);

  myPort = new Serial(this,"COM5",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");

}

小鋼琴

程式碼:
import processing.serial.*;
Serial myPort;
void setup(){
  size(200,200);
  myPort = new Serial(this,"COM5",9600);
}
void draw(){
  
}
void keyPressed(){
  if(key=='1') myPort.write("1");
  if(key=='1') myPort.write("2");
  if(key=='1') myPort.write("3");
  if(key=='1') myPort.write("4");
  if(key=='1') myPort.write("5");
  if(key=='3') myPort.write("6");
  if(key=='5') myPort.write("7");
}
void keyReleased(){
  myPort.write("0");//noTone()
}
void mousePressed(){
  
}






































程式碼:
#define Do 523
#define Re 587
#define Mi 659
#define Fa 698
#define So 784
#define La 880
#define Ti 988


void setup() {
  Serial.begin(9600);//USB傳輸速度

  // put your setup code here, to run once:

  pinMode(2, INPUT_PULLUP);

  pinMode(8, OUTPUT);

  tone(8,Do);//不給長度,一直發音

  delay(1000);//等一秒

  tone(8,Mi);//換另一個聲音

  delay(1000);

  tone(8,So);//再換另一個聲音

  delay(1000);

  noTone(8);//靜音

}



void loop() {
  while(Serial.available()>0){
  int now = Serial.read();
  if(now=='1') tone(8,Do);
  if(now=='2') tone(8,Re);
  if(now=='3') tone(8,Mi);
  if(now=='4') tone(8,Fa);
  if(now=='5') tone(8,So);
  if(now=='6') tone(8,La);
  if(now=='7') tone(8,Ti);
  if(now=='0') noTone(8);
 }
}




沒有留言:

張貼留言