Arduino&Processingのシリアル通信による、楽器作成 Part2
さて、第2弾です
今度は、「可変抵抗」を用いてチューニングし、音を出すというものにしました
さて、今回は中々苦戦しました。
というのも、シリアル通信の際、Processingで値を読み取り、切り分けるとなぜかcdsの値と可変抵抗の値が逆になってしまうのです!!
仕方ないので、理由はおいといて変数名を逆にして完成、としましたが、いかんせん納得いきません。
授業の時聞くかぁ・・・
あ、今回回路図書くのめんどかったので写真とってここに貼っときます(2枚:ブレッドボード、Arduino)
void setup() { Serial.begin(19200); } void loop() { String cdsVal = String(analogRead(0)); String tuning = String(analogRead(1)); Serial.println(cdsVal + "," + tuning); //Serial.println(tuning); delay(500); }
・Processing
import processing.serial.*; import ddf.minim.*; import ddf.minim.signals.*; int tuneEllipse[] = {500, 400, 300, 200, 100, 50}; PFont myFont; Minim minim; Serial arduino; AudioOutput out; int currentTime, prevTime; int x = width/2, y = height/2; void setup() { size(1000, 1000); arduino = new Serial(this, Serial.list()[2], 19200); arduino.bufferUntil('\n'); minim = new Minim(this); out = minim.getLineOut(Minim.STEREO); prevTime = millis(); //delay(5); currentTime = millis(); textSize(64); } void fadeToBlack() { noStroke(); fill(0, 10); rectMode(CORNER); rect(0, 0, width, height); } void draw() { for(int r = 0; r < 6; r++) { for(int c = 0; c <= 360; c+=(int)random(10)) { ellipse(tuneEllipse[r]*cos(c), tuneEllipse[r]*sin(c), 5, 5); fill(0, 0, 255, 100); } } for(int r = 0; r <= 360; r++) { ellipse(x * cos(r), y * sin(r), 5, 5); fill(255, 0, 0); } fadeToBlack(); } String indata = ""; String indata2 = ""; void serialEvent(Serial port) { try { indata = port.readStringUntil('\n'); print(indata); print(indata2); if(indata != null && indata2 != null) { indata = trim(indata); indata2 = trim(indata2); //Why these two data were reversed? int tuning = Integer.parseInt(split(indata, ",")[0]); int cdsVal = Integer.parseInt(split(indata, ",")[1]); print("cdsVal:" + cdsVal + "\n"); print("tuning:" + tuning + "\n"); x = tuning; y = tuning; String displayTuning = "tuning=" + str(tuning); text(displayTuning, width/2, height/2-100); if(tuning >= 500) { text("C3", width/2, height/2); } else if(tuning >= 400) { text("D4", width/2, height/2); } else if(tuning >= 300) { text("E4", width/2, height/2); } else if(tuning >= 200) { text("F#4", width/2, height/2); } else if(tuning >= 100) { text("G3", width/2, height/2); } else if(tuning >= 50) { text("A#", width/2, height/2);; } else if(tuning >= 0) { text("B#", width/2, height/2); } else { text("out of volume", width/2, height/2); } /* if(cdsVal >= 400) { currentTime = millis(); if(currentTime - prevTime > 1) { out.playNote("C4"); } prevTime = currentTime; } */ // many sounds if(cdsVal >= 850) { if(tuning >= 500) { out.playNote("C3"); } else if(tuning >= 400) { out.playNote("C#"); } else if(tuning >= 300) { out.playNote("D4"); } else if(tuning >= 200) { out.playNote("E4"); } else if(tuning >= 100) { out.playNote("F#"); } else if(tuning >= 50) { out.playNote("A#"); } else if(tuning >= 0) { out.playNote("B#"); } } } } catch(Exception e) { e.printStackTrace(); } }
っとと、忘れてた
設定をメモメモ...
Arduinoで選択したポート:/dev/cu.usbmodem1421
なぜかどのポート選んでも、競合するときがある(笑)