2020年11月30日 星期一

Carousel~week12

Arduino

#define Do 523

#define Re 587

#define Mi 659

#define Fa 698

#define So 784

#define La 880

#define Si 988

void setup() {

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

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

 pinMode(2,INPUT_PULLUP);

 pinMode(8,OUTPUT);

 tone(8,Do);

 delay(100);

 tone(8,Mi);

 delay(100);

 tone(8,So);

 delay(100);

 noTone(8);

}

void loop() {

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

  while(Serial.available() > 0)

  {

    int now=Serial.read();

    if(now=='1')tone(8,Do,100);

    if(now=='2')tone(8,Re,100);

    if(now=='3')tone(8,Mi,100);

    if(now=='4')tone(8,Fa,100);

    if(now=='5')tone(8,So,100);  

    if(now=='6')tone(8,La,100);

    if(now=='7')tone(8,Si,100);

    if(now=='0')noTone;

  }

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

}


Processing:

import processing.serial.*;

Serial myPort;

void setup() {

  size(200,200);

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

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

}

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

  myPort.write("1");

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


}

沒有留言:

張貼留言