We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm streaming data from a rotary encoder connected to and Arduino through the serial port into Processing - lets call this number 'rotaryNum'
rotaryNum will only ever be between 0-15. Every time this value changes, I want to start a 3 second timer. When the timer finishes, I want to compare the difference between rotaryNum before the timer started, and when the timer finished.
What is the best way to do this?
I've added some code where I've tried to do this, but I don't really know how to do it. Would anyone have any ideas on how best to achieve what I'm attempting to do?
import processing.serial.*;
Serial myPort;
float value;
float currentValue;
int savedTime;
int totalTime = 3000;
void setup() {
size(400,400);
frameRate(25);
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
}
void draw() {
background(0);
String pulses_raw = myPort.readStringUntil('\n'); //read until the new line, like print ln
int passedTime = millis() - savedTime;
if (pulses_raw != null) {
pulses_raw = trim(pulses_raw);
float rotaryNum = float(pulses_raw);
println(rotaryNum);
//timer = millis();
currentValue = rotaryNum;
if(passedTime > totalTime){
println("currentValue is = " + currentValue);
if( (rotaryNum >= (currentValue + 2)) && (rotaryNum <= (currentValue + 2)) ){
println("pulses is more than currentValue + or - 2");
background(random(255));
savedTime = millis();
}
}
}
}
Answers
I've got some examples that designates a function to run continuously apart from draw() by using thread(""):
Timer Example I:
Timer Example II:
Timed XML Fetcher:
Hi,
Just getting a change to test this now. Well, the timer works great, so many thanks for that :) I used Example II.
However, I'm having a metal block in trying to save the number in a variable before the timer starts, and then comparing it to the current value of the number when the timer finished. I'm baffled. Any ideas on how to do this?
A function called by thread("") can't receive passed arguments! So it's pretty much dependent on "global" variables. :-S
@ Example II, it calls target function logger() after each delay().
Issue is, since timer() itself is void of passed parameters, there's no exclusive parameters it can invoke logger() with! :-?
On the other hand @ Example I, timer() assigns
true
to aboolean
in order to flag the INTERVAL time has been reached.Then draw() keeps checking on that "global"
boolean
. Once it turns outtrue
, it sets it again tofalse
and invokeslogger(), passing more up-to-date parameters!
Therefore, I advise you to try the more flexible Example I out. You can also make the check within draw() to set a "start" value too.
Hi,
Thanks for the advice about the timers - I never had much of an understanding of them.
In the end I managed to hack together a solution using an array, so I didn't utilize those timers. To give you an explination:
I'm reading serial data from Arduino, and putting them into the array called 'churnArray'. I put them in at the start, and after I've put one in I shorten it. I then compare the first character in the array to the last, and if they're different I know that there has been a change. It probalby isn't the best way to do it, especially seeing as I have a frameRate(1); which means I cant do much with graphics on screen, but otherwise works!
Thanks for your help!