私がやろうとしているのは、Arudino で作成したタコメーター回路からデータの連続ストリームを読み取り、それを Processing にフィードすることです。以下のコードを使用して成功しました:
どのようにすればよいかわからないのは、データを処理して、特定の値が検出されるたびに Processing でイベントが発生するようにすることです。
編集:私の問題は、への呼び出しmyMovie.loop()
がブロッキング呼び出しであるということでした。これは、の命令ポインターがvoid setup()
on のままであることを意味しmyMovie.loop()
ます。ポインターは と を呼び出しますvoid Draw()
がmovieEvent
、シリアル ポートが開始された回線には決して到達しません。
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
port.bufferUntil('\n');
提案された解決策は、これらの行を の先頭に移動void Draw()
しmyMovie.loop
、 の最後の行にすることでしvoid setup()
た。私はこれを試しました(以下のコードはこの変更を反映しています)が、処理中のシリアル入力として「0.0」をまだ読んでいますが、Arduinoで正しいデータを取得しています。
以下は私の処理コードです:
import processing.video.*;
import processing.serial.*;
Serial port;
Movie myMovie;
//try as a float?
double val = 0;
void setup()
{
//create screen
size(320, 240);
background(0);
//load movie
myMovie = new Movie(this, "countdown.mov");
// print a list of all available ports
println(Serial.list());
// choose the port to which the Arduino is connected
// on the PC this is usually COM1, on the Macintosh
// this is usually tty.usbserial-XXX
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
///(1) if this line used, no information is read
// port.bufferUntil('\n');
myMovie.loop();
}
void draw() {
if (0 < port.available()) {
///(2) If this line is used, '0.0' is read once and displayed in serial output
String strData = port.readStringUntil('\n'); // string representation of value
//TEST
print(val);
val = Double.parseDouble(strData); // double from string data
}
image(myMovie, 0, 0);
}
void movieEvent(Movie m) {
m.read();
if (val >= 3600) {
myMovie.speed(1);
}
else {
myMovie.speed(0);
}
}
以下は私のArduinoコードです:
//// This example shows one way of creating an optoswitch
//// using an IR LED as emitter and an IR LED receiver as
//// light sensor.
////
//// + GROUND +GROUND
//// | |
//// < <
//// > 220 ohm resistor > 220 ohm resistor
//// < <
//// | |
//// | |
//// ----- -----
//// / \ >>IR LED emitter >>>>>>>>>>>>>>>> / \ IR LED receiver
//// ----- -----
//// | |
//// | |
//// + +5VCD + ANALOG INPUT 0
////
////
////
////<a href="http://playground.arduino.cc/Learning/Tachometer" target="_blank" rel="nofollow">http://playground.arduino.cc/Learning/Tachometer</a>
int val;
long last=0;
int currentStatus=LOW;
int previousStatus=LOW;
int count=0;
int sens=85; // this value indicates the limit reading between dark and light,
// it has to be tested as it may change acording on the
// distance the leds are placed.
int nSpokes=7; // the number of blades of the wheel
int milliseconds=500; // the time it takes each reading
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
val=analogRead(0);
if(val<sens)
currentStatus=LOW;
else
currentStatus=HIGH;
digitalWrite(13,currentStatus); //as iR light is invisible for us, the led on pin 13
//indicate the state of the circuit.
if(previousStatus!=currentStatus){ //counts when the state changes from (dark to light) or
//from (light to dark), remmember that IR light is invisible for us.
count++;
previousStatus=currentStatus;
}
if(millis()-last>=milliseconds){
double rps=((double)count/nSpokes)/2.0*1000.0/milliseconds;
double rpm=((double)count/nSpokes)/2.0*60000.0/(milliseconds);
// Serial.print((count/2.0));Serial.print(" RPS ");Serial.print(rps);
// Serial.print(" RPM");
// Serial.print(rpm);
// Serial.print(" VAL ");Serial.println(val);
Serial.println(rpm);
count=0;
last=millis();
}
}
基本的に、コンピューターのファンの速度を計算するために Arduino Uno を使用しています。ファンが 3600 rpm にとどまっている場合は、映画を再生したいと考えています。それを下回ったら、映画の再生を止めてほしい。私のArduinoスケッチは機能しています(シリアルポートでデータをうまく読み取ることができます)が、何らかの理由でProcessingでそれを行うことができません。データが入ってきていないようです。これは Arduino に含まれている一連のサンプルに基づいていますが、まだ何も機能していないようです。