Proce55ingからArduinoを使う
準備
Arduino Playground - Processingの通りで出来ました。
同ページのExampleも変更することなく、動きました。
Processingのlibraries > arduino > firmware > Standard_Firmata > Standard_Firmata.pde
をArduinoの開発環境で実行し、Upload to I/O Board します。
これで、Processingで書いたコードを実行することが出来るようになりますが、
Arduino開発環境で別のプログラムをI/Oボードにアップロードすると、再び上記作業が必要となります。
テスト
Built with Processing [改訂版]の力を溜めるサンプル(p146-147)を実行してみました。
当然、そのままでは動きませんが少し手を入れればおkです(マウスイベント部分 -> 光センサの状態判定)。
光センサに手をかざしている間、力が溜まります(小さな多数の円がグルグル回りながら大きくなっていく)。
どけると力の輪がしぼんでいきます。
コードは以下のようになります。
光センサ部分は前回同様です。state_low, state_highが回路の組み方を間違えたか逆になっていますが、気にしません(ぇ
import processing.serial.*; import cc.arduino.*; Arduino arduino; int inPin = 0; int threshold = 120; int state_low = 0; int state_high = 1; int last_state = state_low; int Length = 20; float[] X = new float[Length]; float[] Y = new float[Length]; float[] Ang = new float[Length]; float[] Rot = new float[Length]; float[] C = new float[Length]; float[] Radius = new float[Length]; float Power = 0; float MaxPower = 200; void setup() { size(400, 400); colorMode(HSB, 100); background(99); frameRate(30); arduino = new Arduino(this, Arduino.list()[0], 57600); arduino.pinMode(inPin, Arduino.INPUT); } void draw() { fadeToWhite(); int current_state = last_state; if (Power > 0) { for ( int i = 0; i < Length; i++) { Ang[i] += Rot[i] * ( Power / 10); X[i] = width / 2 + ( Power * sin(radians(Ang[i])) ); Y[i] = height / 2 + ( Power * cos(radians(Ang[i])) ); noStroke(); fill(C[i], 80, 99, min(Power, 80)); ellipse(X[i], Y[i], Radius[i], Radius[i]); } } if (arduino.analogRead(inPin) < threshold) { current_state = state_low; } else { current_state = state_high; } if (current_state == state_low) { if (current_state != last_state) { Power = 0; for(int i = 0; i < Length; i++) { Radius[i] = random(10, 30); C[i] = random(100); Rot[i] = random(-5, 5); } } else { if (Power < MaxPower) Power += 1; } } else if (current_state == state_high) { if (Power > 1) Power = Power * 0.98; else Power = 0; } last_state = current_state; } void fadeToWhite() { noStroke(); rectMode(CORNER); fill(99, 30); rect(0, 0, width, height); }
loop()がdraw()と書けるとなんか感動ですねw
コーディングスタイルがキャメルスタイルとアンダーバーで繋げるスタイルが混在していますが、やっぱり、気にしません(ぇ
ちゃんとしないといけないなぁと思いつつ。
Built with Processing [改訂版]
posted with amazlet at 08.08.05
前川 峻志 田中 孝太郎
ビー・エヌ・エヌ新社
売り上げランキング: 38937
ビー・エヌ・エヌ新社
売り上げランキング: 38937
おすすめ度の平均:
さっそく改訂版を買いました