Drawing the data received from microcontroller.

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

Sign In or Register to comment.