What I'm trying to do is read in a continuous stream of data from a tachometer circuit I made with Arudino, and then feed it into Processing; which I've successfully done using the code below:
What I'm not sure how to do is process the data so that whenever a certain value is detected, an event will occur in Processing.
EDIT: It was suggested by someone off SO that my problem was that the call to myMovie.loop()
is a blocking call, which would mean that the instruction pointer in void setup()
would stay on myMovie.loop()
. The pointer would be making calls to void Draw()
and movieEvent
, but would never reach the lines where the serial port is initiated
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
port.bufferUntil('\n');
The suggested solution was to move those those lines to the top of void Draw()
, and have myMovie.loop
as the last line of void setup()
. I tried this (my code below reflects this change), but I'm still reading '0.0'in as serial input in Processing, but getting the correct data in Arduino.
Below is my Processing code:
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);
}
}
And below is my Arduino code:
//// 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();
}
}
Basically, I'm using an Arduino Uno to calculate the speed of a computer fan. If the fan stays at 3600 rpm, then I want a movie to play. If it drops below that, I want the movie to stop playing. My Arduino sketch is working (I'm able to read in the data fine on the serial port), but for some reason I can't do that with Processing; no data appears to be coming in. I based this off of the serial examples included with Arduino, but nothing seems to work yet.
val
, so take it from there. Check what the value is, compare it to what you need it to be to do "more things", and continue based onif
it's correct. – Mike 'Pomax' Kamermans