We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey all, I am quite new at that world, but I would like to know why does it happen to me. The thing is that I created a variable on Arduino which receives a random number between 0 and 100; however, I wanted to print in on Processing but it is not recognized because instead of that it writes COM3, the port where Arduino is connected. By the way, here is the Arduino code:
void setup() { //initialize serial communications at a 9600 baud rate
Serial.begin(9600); }
void loop() {
int num = random(0, 100);
Serial.println(num);
delay(100);
}
And here the Processing one:
import vsync.*;
import processing.serial.*;
Serial myPort;
void setup() {
myPort = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
println(Serial.list());
delay(1000); }
I don't know if I am missing some libraries but I am kinda stocked for hours.
Thank you.
Answers
new Serial(this, Serial.list()[0], 9600).bufferUntil(ENTER);
http://forum.Processing.org/two/discussion/12348/arduino-serialread-makes-my-processing-animation-stop
I am really thankful man! Everything worked fine at the end. For the ones with similar problems you could check this example as well: https://processing.org/reference/libraries/serial/serialEvent_.html
Glad it worked! For next code posts, better learn how to format them for the forum though: *-:)
http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text
Today I have been working a bit more on that, and I realised that I am not able to work with the variables separately. Here there is the example:
ARDUINO:
PROCESSING:
What I would like to obtain somehow is the variables on their own. I mean, in Arduino I work with "num" and "num2" even though in Processing due to the readstring I can only use one, called "final", which turns into "variable".
Thank you for all the help!
Since you intend to sending out 2
int
values, the 1st serialEvent() is gonna be num1.And the following 2nd trigger is thus num2.
Just need an extra
boolean
flag variable to track down which 1 of those is currently being sent: :DYou saved my life, thanks! By the way, what does
^=
mean? :D^=
is 1 of the many compound assignments. Like+=
,*=
,%=
, etc:https://Processing.org/reference/addassign.html
^
is the bitwise operator XOR. When both bits are equal, we get0
. Otherwise1
.if (isNum2 ^= true)
is equivalent toif (isNum2 = !isNum2)
:https://Processing.org/reference/logicalNOT.html
true
/false
states each time that assignment happens.true
, the 1stisNum2 ^= true
switches it tofalse
.else num1 = n;
happens 1st. And next time,num2 = n; redraw = true;
. And so on... :bzSo now I am trying to do the same with 3 variables, and I get something like this in the serialEvent:
It seems to work fine but the numbers I receive are mixed. I mean, num1 should stay between 0 and 10, num2 between 10 and 20 and num3 between 20 and 30 but practicaly some print lines are like this: (frame count) 22 10 23.
That
boolean
approach was custom-made to deal w/ 2 diff. input values only.Since a
boolean
primitive data-type got 2 states only:false
&true
:https://Processing.org/reference/boolean.html
We could "upgrade" it to the object data-type Boolean, so we got an extra 3rd state:
null
:http://docs.Oracle.com/javase/8/docs/api/java/lang/Boolean.html https://Processing.org/reference/null.html
Logically it's not a definitive long-term solution b/c you may wish for more than 3 input values later...
In order to deal w/ multiple input values, we can store them all in some array.
Then have an extra variable to track down current input index being dealt for each serialEvent().
And in order to reset index back to
0
, we use the modulo/remaining%
operator:https://Processing.org/reference/modulo.html
And check out when it becomes
0
again and invoke redraw(), so we display the full array capture:https://Processing.org/reference/redraw_.html
Thank you another time! I am going to check it and see if that fits my project :)
It worked really fine, but in order to draw graphics and all of this stuff is quite hard without getting the variables as if it was the Arduino. I have tried the vSync library but without success. Is it just me or that works? It would be perfect!
https://Processing.org/reference/arrayaccess.html
http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Finally I can solve all the issues I had, thank you for everything!! :)