How to read sensor value (analog read) from arduino to processing ide?

I was writing one code where I'm reading analog values from sensor and it is displayed in the serial monitor (the desired values are obtained) but when I opened in processing IDE the values have been decoded and it is shows as one byte (in ASCII value).

What should I do to get the exact value from Arduino IDE to processing IDE. How to reconstruct my code??.

Here's the Arduino code screenshot:

Here's the processing IDE screenshot :

what should I do in this case??…how do I exactly read the same value from the Arduino IDE to Processing IDE?

please, please never ever again post pictures of text... Not only they are not readable nor usable directly for copy&paste but they use up lots of storage and internet bandwidth which contributes to polluting the planet.

➜ do your part and do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

Lab: Serial Output from an Arduino to Processing – ITP Physical Computing (nyu.edu)

Since it is such a simple problem I'll ignore the fact you didn't post the code properly.

You are transmitting ASCII characters and a newline (LF/CR) from the Arduino with the sensor. If you are just interested in displaying the data on the "processing" Arduino you can implement the following simple loop() function:

void loop()
{
  if (myPort.available())
  {
      char inChar = myPort.read();
      Serial.print(inChar);
  }
}

This will print the ASCII characters transmitted from the sensor Arduino. However, if you want to perform processing on that data (or even if you don't) it would be better to transmit the raw analog value from the sensor Arduino using Serial.write() to transmit both bytes of the sensor reading and reassemble them into an integer on the processing Arduino.

No it didn't work. Cannot convert from char into int.

okay thank u

sorry for the informal post. I was in a urgent to complete this task...im stuck nowhere in the middle. I will correct this from next post

if the value you want to share does not fit on one byte and you don't want to loose precision by sending only one byte, then an easy way is to send the value in ASCII with an end marker of some sort.

Since you are using println() to send the value, the Arduino adds a carriage return and a line feed character after your value, ie the integer 234 is sent as "234\r\n". So far so good

On the decoding side you need to read a full line until the '\n' and then extract the integer. You can't just read one byte though.

here is a simple example

Arduino code (sending the reading of analog 0 every second at 115200 bauds

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.println(analogRead(A0));
  delay(1000);
}

and a matching Processing program to read that (you might need to adjust the port number, here I picked Serial.list()[2])

import processing.serial.*;
Serial arduino;                                           // The serial port connected to our Arduino program

void setup() {
  printArray(Serial.list());                              // List all the available serial ports
  arduino = new Serial(this, Serial.list()[2], 115200);   // Open the right port (here the #2). This will reboot a UNO or MEGA
  arduino.clear();                                        // Empty the incoming Serial buffer
  arduino.readStringUntil('\n');                          // Throw out the first reading, in case we started reading in a middle of a data send
}

void draw() {
  if (arduino.available() > 0) {                          // if something is pending
    String message = arduino.readStringUntil('\n');       // get the full line
    if (message != null) {                                // if there was actually something to read
      message = message.trim();                           // Removes whitespace characters from the beginning and end of the message 
      if (! message.isEmpty()) {
        int value = Integer.valueOf(message);             // extract an integer
        print("got \"");                                  // and print what we got
        print(message); 
        print("\" --> "); 
        println(value);
      }
    }
  }
}

Thank u so much..this helped me a lot

Great have fun

To close the thread click the solution button under the post that you feel represent the solution.

Welcome. I have posted a next issue. I'm badly stuck in my project phase plz help me.

If it’s related to this, post here otherwise

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.