How to read Serial String from arduino as Int on processing?

Hello, I'm really new to processing and I'm trying to read the values from a potenciometer as int so I can use each value for different things. My code is as it is (obtained from here)

import processing.serial.*;

Serial myPort;  // Create object from Serial class
String val;     // Data received from the serial port
void setup()
{
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}

void draw()
{
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n');         // read it and store it in val
  } 

println(val); //print it out in the console
}

I really appreciate any help, I'm really at a blank here as I don't really know where to start searching. Thanks

Answers

  • Do you need help with your arduino or processing data? That Processing data looks ok.

    Kf

  • with processing, I guess I misunderstood the forum organization, I will ask in the processing code section

  • No need to create another post. Your post is in the right section. I don't understand from your post what is missing. I mean, the processing code seems to be ok.

    Maybe this line: int y = Integer.parseInt(val); Remember, Processing is just Java.

    Kf

  • Yes, sorry, I'll try to clarify myself. What I have is processing receiving the nalogRead() numbers from arduino and printing them to the console to verify that it is indeed receiving them. All good here, but what I want is to use those numbers to modify an Image through pixel sorting, so for example arduin is sending 4.3 as a value i need that value to arrange the pixels in a certain way, what I'm missing is the Int, as I have read.

    I tried Integer.parseInt() but it send me this: NumberFormatException: Null

    Again sorry if something doesn't make sense, I'm new to even java

  • If you are sending 4.3 then you should be working with float values instead. The sorting will happen only when you receive a new value? It should work like this then:

    import processing.serial.*;
    
    Serial myPort;  // Create object from Serial class
    String val;     // Data received from the serial port
    boolean triggerSorting;
    
    void setup()
    {
      // On Windows machines, this generally opens COM1.
      // Open whatever port is the one you're using.
      String portName = Serial.list()[0];
      myPort = new Serial(this, portName, 9600);
      triggerSorting=false;
    }
    
    void draw()
    {
      if ( myPort.available() > 0)
      {  // If data is available,
        val = myPort.readStringUntil('\n');         // read it and store it in val
        int intVal = Integer.parseInt(val.trim());
    
        //For example, if potentiometer value is larger than 10, trigger sorting
        //The value of 10 is arbitrary and chosen for demonstration purposes.
        if(intVal>10){
          triggerSorting=true;
        }
      }
    
      //NEXT performs sorting only if it has received proper data
      if(triggerSorting==true){
    
        //
        //.....DO YOUR SORTING OPERATION HERE
        //
    
        triggerSorting=false; //RESET for next read operation
      }
    
      println(val); //print it out in the console
    }
    

    Kf

  • yes you are right, it should be float, not int, so I guess the int intVal = Integer.parseInt(val.trim()); should change for a float converter? (while runing it sends error NumberFormatException: for input string "1.21")

  • Answer ✓

    This should solve it: float f = Float.parseFloat("25");

    Kf

  • Thank you!! it's working perfectly now

  • Actually, if you're using the Processing IDE, you can just use int("25") and float("25") instead of Integer.parseInt("25") and Float.parseFloat("25")

Sign In or Register to comment.