2020年11月30日 星期一

Σ(゚д゚) weeeeeeeeeek 12

MAKER UNO


由於上星期請假

所以這星期第一次接觸到這個器材

跟大一上arduino的課時接觸到的器材很像

接到電腦上後透過程式可以做出很多不同的變化

像今天的第一個範例


上傳之後機器會發出 Do Mi So 的聲音


而第二個範例 

是在第一個的基礎上

加上了按鍵控制

當按下按鍵時就會發出聲音 

pinMode(2, INPUT_PULLUP); ------->按下按鍵


今天第三個範例

這邊開始結合了Processing的程式

在Processing程式運行畫面點擊時發出聲音


範例四


這邊加上了鍵盤控制

數字鍵盤1 3 5分別對應上了Do Mi So三個音階


範例五


加上更多音階使鋼琴更加完整

並且將發音的長度刪掉

這邊記得加一行程式

void keyReleased(){

   myPort.write("0"); 

}

當放開按鍵時靜音 

否則會響個不停Σ(゚д゚)


今日份程式碼

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(100);
  tone(8, Mi);
  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, Ti, 100);
    if(now == '0')noTone(8);
  }
}


Processing

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 == '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"); 
}

沒有留言:

張貼留言