Search code examples
serial-portprocessingarduino-uno

int(string) returns 0 on processing when string comes from the serial monitor


I am trying to turn a string value from the Serial monitor into an int so I can use potentiometer reading in my processing sketch. When i use int() to parse the string into int, the output is 0, while the string has a different value varying according to the potentiometer reading.

This is my processing sketch. I have checked that the arduino code is sending the correct values so the issue must be here.

import processing.serial.*;

Serial myPort;
String sVal;

float w=1023;
float h=100;
void setup() {
  size(1023, 100);
  background(255);
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);
}

void draw() {
  if (myPort.available() > 0 ) {
    sVal = myPort.readStringUntil('\n');
    int val= int(sVal);
    println("i am int "+ val);
    println("i am string "+ sVal);
    line(val, 0, val, h);
  }
  myPort.clear();
}

Some of my outputs with the previous code are:

i am int 0 
i am string 494

i am int 0 
i am string 94

Solution

  • You are likely trying to parse null strings and therefore these return zero. I would also suggest that you use serialEvent() to read the strings instead of using draw(). The following source code should filter out the null strings and parse only when there is an actual string. If you unREM the first println() before the data is filtered and trimmed you should see all the null returns. trim() will also help remove any extraneous characters which contaminate the data. Serial data is not transmitted in nice packets like we would hope; strings are often broken at random locations. They then have to be reconstructed on the receiving end and put back together. The delimiter on each end of the string can help with this process; just be sure that you use the same delimiter for data when sending and receiving. Your example appears to use a line feed character to bracket the data which is automatically added by using println() on the Arduino end. Other characters could be alternatively used, eg commas, asterisks, etc. I have also added a simple Arduino demo which creates sawtooth data that you can use for testing.

    Processing code:

    import processing.serial.*;
    
    Serial port;
    
    void setup() {
      printArray(Serial.list());
      // Use correct port number for your system
      port = new Serial(this, Serial.list()[2], 9600);
    }
    
    void draw() {
    }
    
    void serialEvent (Serial port) {
      String inStr = port.readStringUntil('\n');
    //  println(inStr);
      if (inStr != null) {
        String trimStr = trim(inStr);
        int val = int(trimStr);
        println("str = " + trimStr +  " : int = " + val);
      }
    }
    
    

    Arduino code for testing:

    byte outputValue = 0;
    
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      outputValue += 8;
      Serial.println(outputValue);
    }