我要做的是从我用 Arudino 制作的转速计电路中读取连续的数据流,然后将其输入 Processing;我已经使用下面的代码成功完成了:
我不确定如何处理数据,以便每当检测到某个值时,处理中就会发生一个事件。
编辑:有人建议我的问题是调用myMovie.loop()
是阻塞调用,这意味着指令指针 invoid setup()
将保持在myMovie.loop()
. 指针将调用void Draw()
and 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 草图正在工作(我可以在串行端口上很好地读取数据),但由于某种原因,我无法通过处理来做到这一点;似乎没有数据进入。我基于 Arduino 附带的系列示例,但似乎还没有任何效果。