We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Problem:
The processing code is not receiving the data from the Arduino correctly (or the Arduino isn't sending the correct data). There seems to be a buffer issue as well. When Arduino is disconnected, Processing still prints 2 digit values, but no "-1" values. What am I doing wrong and is there a more efficient way to handle the graphing? Thank you in advance for any help.
Set up:
Arduino generates a random decimal value every 100 mills and prints to the serial port. The serial monitor displays the correct value. The processing code connects and receives data and prints the data for debugging purposes, but there are several "-1" values and no decimal values. When processing is NOT running Arduino serial monitor displays values correctly. When Processing sketch is running, 5-10 loop cycles display correctly in Serial Monitor, then strange numbers show up, then another 5-10 cycles display correctly. BTW... running on a Mac.
Code:
/*
______________
Arduino Code
______________
*/
void setup() {
Serial.begin(115200);
}
void loop() {
int x = int(random(100));
int y = int(random(9));
int z = int(random(9));
String X = String(x);
String Z = String(z);
String Y = String(y);
String val = X + "." + Y + Z;
Serial.println (val);
// int x = random(6);
// Serial.println(x);
//
delay(100);
}
/*
_________________
Processing Code
_________________
*/
//Barrett Anderies
//Jan. 21, 2013
import processing.serial.*;
Serial myPort;
int width = 800;
int height = 400;
int[] data1 = new int[width];
int newDataPoint1 = 0;
void setup()
{
size(width,height);
frameRate(100);
smooth();
println(Serial.list());
myPort = new Serial(this, Serial.list()[5], 115200);
}
void draw()
{
background(255,255,255);
stroke(0,0,0);
strokeWeight(2);
line(0,height/2,width,height/2);
line(width/4,0,width/4,height);
int inByte = myPort.read();
println(inByte);
newDataPoint1 = inByte;
for(int i = 0; i < width-1; i++)
{
data1[i] = data1[i+1];
}
data1[width-1] = newDataPoint1;
strokeWeight(2);
for(int i = width-1; i > 0; i--)
{
stroke(255,0,0);
line(i,data1[i-1], i+1, data1[i]);
}
delay(20);
}
Answers
When posting code always press Ctrl+K so we can read it.
I don't get why you use
Serial.list()[5]
often you just useSerial.list()[0]
but u might have a reason for that, anyway just check its right port u use.the .read(); returns -1 if an empty byte has been sent to prevent this you use
and im not sure about this, but i belive read() only reads a Byte (a value 0-255) so when you send somthing with "." in it it might give unexpected results. I think you are supposed to use the .readuntill function, however i havent tried it, i dont own a arduino, so hopefully som 1 else can answer what ur supposed to use. or u figure out ur self.