2020年11月30日 星期一

Sherry: arduino音樂按鍵

TRY_1:

#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:

} 


TRY_2:

pinMode(2,INPUT_PULLUP);

 pinMode(8,OUTPUT);

void loop() {

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

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

}

TRY_3:

(processing)

import processing.serial.*;

Serial myPort;

void setup(){

  size(200,200);

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

}

void draw(){

}

void mousePressed(){

  myPort.write("1");

}

(arduino)

#define Do 523

#define Mi 659

#define So 784


void setup() {

  Serial.begin(9600);

  // 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() {

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

  while(Serial.available()>0){

    Serial.read();

    tone(8,So,100);

  }

}

TRY_4:

(processing)

import processing.serial.*;

Serial myPort;

void setup(){

  size(200,200);

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

}

(arduino)
 

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

  // 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() {

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

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

  }

}


參考網站:

https://github.com/CytronTechnologies/MakerUno_Examples/blob/master/MakerUno_DefaultSketch/MakerUno_DefaultSketch.ino



下次上課帶:

剪線鉗 杜邦線 麵包板 makeruno (joystick)

沒有留言:

張貼留言