Arduino&Processing楽器作成3 書き換えてみた
まとめも兼ねて、主にProcessingのコードを書き換えました。
コメントも付け加え、少しわかりやすくなったかなと思います。
・Arduino近辺の写真
・Arduino(変更なし)
void setup() { Serial.begin(19200); } void loop() { String cdsVal = String(analogRead(0)); String tuning = String(analogRead(1)); Serial.println(cdsVal + "," + tuning); delay(500); }
・Processing (変更あり)
import processing.serial.*; import ddf.minim.*; import ddf.minim.signals.*; PFont myFont; Minim minim; Serial arduino; AudioOutput out; //setup function void setup() { size(1000, 1000); //display-size arduino = new Serial(this, Serial.list()[2], 19200); arduino.bufferUntil('\n'); minim = new Minim(this); out = minim.getLineOut(Minim.STEREO); prevTime = millis(); //preview Time currentTime = millis(); //current Time textSize(64); //set text-size } //fadeout function void fadeToBlack() { noStroke(); fill(0, 10); rectMode(CORNER); rect(0, 0, width, height); } int tuneEllipse[] = {500, 400, 300, 200, 100, 50}; int x = width/2, y = height/2; //draw function void draw() { //draw division for(int r = 0; r < 6; r++) { for(int c = 0; c <= 360; c+=(int)random(5)) { ellipse(tuneEllipse[r]*cos(c), tuneEllipse[r]*sin(c), 5, 5); fill(0, 0, 255, 100); } } //draw ellipse for(int r = 0; r <= 360; r++) { ellipse(x * cos(r), y * sin(r), 5, 5); fill(255, 0, 0); } fadeToBlack(); //fadeout } String indata = ""; //tuning String indata2 = "";//cdsVal int currentTime, prevTime; void serialEvent(Serial port) { try { indata = port.readStringUntil('\n'); print(indata); print(indata2); if(indata != null && indata2 != null) { indata = trim(indata); indata2 = trim(indata2); //triming 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; //set (x,y) //display tuning value String displayTuning = "tuning=" + str(tuning); text(displayTuning, width/2, height/2-100); //Displaying text which express tone. if(tuning >= 500) { text("do", width/2, height/2); //do } else if(tuning >= 400) { text("re", width/2, height/2); //re } else if(tuning >= 300) { text("mi", width/2, height/2); //mi } else if(tuning >= 200) { text("fa", width/2, height/2); //fa } else if(tuning >= 100) { text("so", width/2, height/2); //so } else if(tuning >= 50) { text("ra", width/2, height/2);; //la } else if(tuning >= 0) { text("si", width/2, height/2); //si } else { text("out of volume", width/2, height/2); } // many sounds //do-re-mi-fa-so-la-si if(cdsVal >= 850) { //outer if statement if(tuning >= 500) { //inner if statement out.playNote(261.6); //do } else if(tuning >= 400) { out.playNote(293.7); //re } else if(tuning >= 300) { out.playNote(329.6);//mi } else if(tuning >= 200) { out.playNote(349.2);//fa } else if(tuning >= 100) { out.playNote(392.1);//so } else if(tuning >= 50) { out.playNote(440);//la } else if(tuning >= 0) { out.playNote(494.1);//si } //end of inner if statement } //end of outer if statement } } catch(Exception e) { //catch Exception e.printStackTrace(); } }
ポート番号対応付け表
/etc/servicesを覗くと、以下の具合にポート番号の対応付けを見ることができます
(*'-') < cat /etc/services | head -1000 # # Network services, Internet style # # ・ ・ ・ # # WELL KNOWN PORT NUMBERS # ここからポート番号情報
あ、当然ですが俺のは載っけませんよ?怖いんで
gitコマンド まとめ
ちょこちょこ使うんですが、なかなか忘れてしまうのでメモ
・リポジトリをとってくる
git clone https://github.com/[ユーザ名]/[リポジトリ名]
・リポジトリを追加
git add [ファイル名]
・リポジトリを削除
git rm [ファイル名]
・リポジトリの状況を確認
git status
・コミット詳細を確認
git show
・コミットログを確認
git log
・変更差分を確認
git diff
・コミット
git commit -m 'メッセージ'
・プッシュする
git push origin master
・単一のコミットを元に戻す
git revert [コミット]
curlコマンド
またまた、まとめ
・ページ丸ごと取得
curl http://www.yahoo.co.jp
・それを丸ごと保存(名前指定)
curl -o sample.txt http://www.yahoo.co.jp
・それを丸ごと保存(デフォルト名)
curl -O sample.txt http://www.yahoo.co.jp
・POST送信
curl -d name=admin -d pass=aokfw34543 http://nanika.co.jp/index.php
・サーバのHTTPヘッダ取得
ヘッダの閲覧が主な目的。
curl -I yahoo.co.jp
・証明書エラースキップ
curl -k https://secure-socket-layer.co.jp
・サーバとのやりとり取得
curl -v yahoo.co.jp
・File Transfer Protocolを用いてファイルアップロード
curl -T sample.jpg -u [username]:[password] ftp://ftp.example.com/static/imgs/
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
なぜかどのポート選んでも、競合するときがある(笑)