We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi!
First of all I only recently discovered Processing and it has already helped me tremendously in my work. Since I am fairly new to this software, I am a little confused.
I am trying to "draw" the points that I am receiving from my dsPIC microcontroller. I know that I am receiving the correct data since println
shows that I do receive them.
A little summary: I am reading a txt file that contains G-code commands for something. I send those commands line by line to my dsPIC, which crunches the data. I then return the coordinates I already obtain from the txt file (through Processing) so that I can hopefully draw them on the work area. I do this instead of extracting directly from the file because I wanna make sure that my microcontroller process the data correctly.
And here comes the rub: my drawing code does not work! I want to draw and connect the points as I receive them so that I can visualize whats happening on the machine in my monitor.
The code seems fairly logical to me, but I guess there are nuances to Processing that I do not know yet. It also does not help that I really am not very familiar with Java coding(only C).
Finally, here is my code:
` import processing.serial.*;
PVector[] vecs;
Serial myPort;
String val;
String[] command;
int command_num = 0;
int counter = 0;
int letter = 0;
String dummy;
String dummy2;
String[] coordinates;
int vectorcounter = 0;
int i = 0;
boolean ender = true;//if false, then endShape to stop drawing
boolean drawing; //if true, start drawing
boolean firstContact = false;
void setup(){
size(800,800);
//some Serial and file reading setup
vecs = new PVector[command.length];//irrelevant, doesnt work
}
void draw(){
beginShape();
while(ender){
if(drawing){
vertex(float(coordinates[0]),float(coordinates[1]));
drawing = false;//toggle to avoid infinite loop
}
}
endShape(CLOSE);
}
void serialEvent(Serial myPort){
//put incoming data into a string
///\n delimiter indicating end of packet
val = myPort.readStringUntil('\n');
if((val != null)){ //if null, do nothing
println("[dspic]"+val);
if(firstContact == false){
//code to establish contact with dspic
}
else{
if(val.equals("Next!\n")){
dummy = command[command_num];
if(command_num == command.length - 1){
ender = false;
println("Reached end of file. Please re-run.\n");
while(true){}
}
while(counter<dummy.length()){
//some code to send the commands to dspic
}
if(val.charAt(0) == 'X'){///HERES WHERE I RECEIVE THE DATA
dummy2 = val.substring(2);
coordinates = split(dummy2,",");
println(coordinates[0]+coordinates[1]);///***
drawing = true;
}
}
}
}`
Please pardon some seemingly random variables and ommitted because I have been experimenting to no avail. My current logic is that in serialEvent I set drawing
to true
when I receive the data, so that the draw()
function knows when to draw. I also toggle the boolean there.
I already know that I am receiving the correct coordinates at println(coordinates[0]+coordinates[1]);///***
, but I just cant draw it at the draw()
routine, even though global variables are involved. I am under the impression that the whole code at serialEvent
only executes when there is serial data available, so I plan to use the rest of the time to draw in the draw
function. But that doesnt work apparently.
I would really appreciate any help. Thanks! I might not reply quick though because I lost a night's sleep due to this problem. :))
Answers
You have to split your program. Any data sent should be done in a controlled manner in draw. In a controlled manner, I mean you have to make sure you are sending it only once.
SerialEvent() function should be used only to receive data.
Last, you should avoid using any while loop in the draw() because draw itself is a while loop. It is running at a 60fps. If you set a while loop inside draw, you are blocking the draw function and your program could become unresponsive.
Consider checking the following examples:
https://forum.processing.org/two/discussion/comment/88115/#Comment_88115
https://forum.processing.org/two/discussion/21447/two-sensors-trigger-same-part-of-code#latest
https://forum.processing.org/two/discussion/comment/88886#Comment_88886 [Here focus in the last postings on Feb 27th]
Kf
Okay okay, now that 60 fps thing is a revelation to me. Okay, I will try something else, will reply if it works.
Thanks for your insight!
@mouser -- good luck. For more background re:60fps default, see:
I have the code working now. I basically just kept in mind that SerialEvent is only called until the the character in readStringUntil, and that
draw()
is called at the framerate.The code inside
draw
is therefore almost the same except I removed the while loop while theif(drawing)
statement is kept intact.Thanks!