Problem parsing arduino data in processing

edited April 2014 in Arduino

Having this Arduino code sends out a string with 3 random numbers. I want to use the variables in Processing, but I cannot get it to work.

void loop() {

a=random(5); b=random(8); c=random(9);

Serial.print(a); Serial.print(" "); Serial.print(b); Serial.print(" "); Serial.println(c); delay(750); } The terminal on my PC sees the datasteam and this part works fine. (portsetup is fine), so this part looks to be fine..

My Processing code comes here. import processing.serial.*;

int a, b, r, g, c; // laver a og b som globbae vaiabler Serial myPort; // The serial port

void setup() { size(256, 256); // Stage size String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); }

void draw() { }

void serialEvent(Serial myPort) { String numbers = myPort.readString(); int[] nums = int(split(numbers, ' ')); a=int(nums[0]); b=int(nums[1]); c=int(nums[2]);

println(a); }

I would expect thee 3 integers in my stream to get into a, b and c. But i cannot get it to work. Any sugestions? I have used the last 3 hours fiddeling with it but with no luck

Best

Peter

Answers

  • edited April 2014
        void loop() {
    
        a=random(5); b=random(8); c=random(9);
    
        Serial.print(a); Serial.print(" "); Serial.print(b); Serial.print(" "); Serial.println(c); delay(750); } 
    
          import processing.serial.*;
    
        int a, b, r, g, c; // laver a og b som globbae vaiabler Serial myPort; // The serial port
    
        void setup() { size(256, 256); // Stage size String portName = Serial.list()[0]; myPort = new Serial(this, portName, 9600); }
    
        void draw() { }
    
        void serialEvent(Serial myPort) { String numbers = myPort.readString(); int[] nums = int(split(numbers, ' ')); a=int(nums[0]); b=int(nums[1]); c=int(nums[2]);
    
        println(a); }
    

    try this:

     void serialEvent(Serial myPort) { 
     String numbers = myPort.readStringUntil('\n'); 
      numbers = trim(numbers);
     int[] nums = int(split(numbers," "));
     a=int(nums[0]);
     b=int(nums[1]);
     c=int(nums[2]);
     println(a); 
     }
    
Sign In or Register to comment.