We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › nOOb Q: SimpleRead prints erratic serial data
Page Index Toggle Pages: 1
nOOb Q: SimpleRead prints erratic serial data (Read 1197 times)
nOOb Q: SimpleRead prints erratic serial data
Mar 31st, 2009, 1:29pm
 
Hi all,

Read through a bunch of the boards but can't get my mind around a really simple serial read in Processing.  Tested on 2 different computers with the same results...any clarity would be much appreciated.

Arduino board has a super simple program that outputs info from a potentiometer:

//start program
int analogPin =5;
int ledStrength = 0;

void setup() {
pinMode(analogPin, INPUT);
   Serial.begin(9600);
 }
 
 void loop() {
    ledStrength = analogRead(analogPin);

Serial.print("value =");
Serial.println(ledStrength);
}

//end program

When I switch on the serial monitor, I get a nice smooth range of numbers that correspond to the knob-twisting I do on the potentiometer.  All good here.  So I close Arduino and open Processing.

In Processing, I open up the Examples>Libraries>Serial>SimpleRead program (comments removed & program simplified to shorten post):

//start program

import processing.serial.*;

Serial myPort;
int val;

void setup() {
 String portName = Serial.list()[1];
 myPort = new Serial(this, portName, 9600);
}

void draw()
{
   val = myPort.read();
   println("value=" + val);
}

//end program

Processing prints an erratic (but not random) sequence of numbers that don't correspond to knob-twisting.  Yes, I'm using the right port, when I switch to another I get a steady stream of zeroes.

Can anyone share why Arduino & Processing output different numbers while receiving the same stream (but not at the same time)?

Thanks for reading.
Re: nOOb Q: SimpleRead prints erratic serial data
Reply #1 - Apr 1st, 2009, 12:12pm
 
The message coming from the Arduino is a string representing the int.

This means that 0 will come in as "0" and 42 will come in as "42".

When you call serial.read(), it is only picking up the first byte.

You could use readString() to read until the "\n". You might have to call bufferUntil(char) in Processing.

Edit: just noticed that you are already using println().
Re: nOOb Q: SimpleRead prints erratic serial data
Reply #2 - Apr 3rd, 2009, 9:09pm
 
You are working on exactly what I am trying to do. I think you have to code the serial writes on the Arduino side with a marker and then decode them on the Processing side based on that marker. Otherwise you could be looking at an endless stream of numbers not knowing where the data elements are. I'm using the following Arduino code:
Code:

void loop(){
 // read the analog input on pin 0:
 analogValue = analogRead(0);
 if (abs(analogValue-lastValue) > analogDelta) {
   //Serial.println(analogValue);
   Serial.print(analogValue);
Serial.print("A");
   lastValue = analogValue;
 }
 delay(10);
}

After trying many methods on the Processing side, smooth results were achieved only by using serialEvent(myPort) to collect the data when its available in combination with a setup myPort.bufferUntil(MARKER) and a myPort.readstringUntil(MARKER) to read the data. On the Processing side:
Code:

void serialEvent(Serial port) {
dataStr = (port.readStringUntil(MARKER));
if (dataStr != null ) {
if (dataStr.length() >=2){
dataStr = dataStr.substring(0, dataStr.length()-1); // strip off the marker char
potV = float(dataStr);
if(potV > potMax){
potMax = potV;
}
}
}
}

MARKER is 65 for character A. The serialEvent makes sure potV and potMax are current for use in drawDataArea() with in the draw() proceedure as in:
Code:

drawTitle();
drawAxisLabels();
drawVolumeLabels();
drawDataArea();
drawMaxDataLine();


Page Index Toggle Pages: 1