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??.
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).
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:
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.
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
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);
}
}
}
}