Trouble with data into/out of object
in
Programming Questions
•
7 months ago
I am new to using objects - the object in the code below came from a simpler sketch and was a function that worked correctly. The data comes in as strings from an Arduino. I want to move up to more complex data handling and so want to move up from a function to an object.
In the function sketch, the data is displayed correctly, but in the object sketch, the data is just zeros.
It appears that I am not correctly passing the data into the object or not calling calling for it to be displayed. Any observations?
code:
import processing.serial.*;
Serial port;
//import controlP5.*;
//ControlP5 control;
Serial port;
//import controlP5.*;
//ControlP5 control;
DataDisplay xDataDisplay, yDataDisplay, zDataDisplay;
String coord_x = "";
String coord_y = "";
String coord_z = "";
String coord_y = "";
String coord_z = "";
void setup()
{
size (640, 480);
port = new Serial(this, "COM3", 9600);
port.bufferUntil(';');
{
size (640, 480);
port = new Serial(this, "COM3", 9600);
port.bufferUntil(';');
xDataDisplay = new DataDisplay(25, 25, coord_x);
yDataDisplay = new DataDisplay(25, 125, coord_y);
zDataDisplay = new DataDisplay(25, 225, coord_z);
}
void serialEvent(Serial port)
{
//the serialEvent parses the string sent by Arduino
//declare the serialEvent variables
String data = "";
int index_x = 0;
int index_y = 0;
int index_z = 0;
//read the data up to the semi-colon
data = port.readStringUntil(';');
//trim off semi-colon
data = data.substring(0, data.length() - 1);
//look for the x
index_x = data.indexOf("X");
//fetch the x coordinate
coord_x = data.substring(0, index_x);
//look for the y
index_y = data.indexOf("Y");
//fetch the y coordinate
coord_y = data.substring(index_x+1, index_y);
//look for the z
index_z = data.indexOf("Z");
//fetch the z coordinate
coord_z = data.substring(index_y + 1, index_z);
}
yDataDisplay = new DataDisplay(25, 125, coord_y);
zDataDisplay = new DataDisplay(25, 225, coord_z);
}
void serialEvent(Serial port)
{
//the serialEvent parses the string sent by Arduino
//declare the serialEvent variables
String data = "";
int index_x = 0;
int index_y = 0;
int index_z = 0;
//read the data up to the semi-colon
data = port.readStringUntil(';');
//trim off semi-colon
data = data.substring(0, data.length() - 1);
//look for the x
index_x = data.indexOf("X");
//fetch the x coordinate
coord_x = data.substring(0, index_x);
//look for the y
index_y = data.indexOf("Y");
//fetch the y coordinate
coord_y = data.substring(index_x+1, index_y);
//look for the z
index_z = data.indexOf("Z");
//fetch the z coordinate
coord_z = data.substring(index_y + 1, index_z);
}
void draw()
{
background(0);
xDataDisplay.update();
yDataDisplay.update();
zDataDisplay.update();
xDataDisplay.display();
yDataDisplay.display();
zDataDisplay.display();
}
class DataDisplay
{
int xpos;
int ypos;
String coord;
float inch_coord;
DataDisplay(int ixpos, int iypos, String icoord)
{
xpos = ixpos;
ypos = iypos;
coord = icoord;
}
void update()
{
float flt_coord = float(coord);//convert string coord to float
float inch_coord = flt_coord/2650;//convert raw to inches
}
void display()
{
pushMatrix();
translate(xpos, ypos);
fill (0, 255, 0);
rect(0, 0, 340, 50);
fill(255,0,0);
textSize(24);
textAlign(CENTER, CENTER);
text(inch_coord * 1, 170, 25);
popMatrix();
}
{
background(0);
xDataDisplay.update();
yDataDisplay.update();
zDataDisplay.update();
xDataDisplay.display();
yDataDisplay.display();
zDataDisplay.display();
}
class DataDisplay
{
int xpos;
int ypos;
String coord;
float inch_coord;
DataDisplay(int ixpos, int iypos, String icoord)
{
xpos = ixpos;
ypos = iypos;
coord = icoord;
}
void update()
{
float flt_coord = float(coord);//convert string coord to float
float inch_coord = flt_coord/2650;//convert raw to inches
}
void display()
{
pushMatrix();
translate(xpos, ypos);
fill (0, 255, 0);
rect(0, 0, 340, 50);
fill(255,0,0);
textSize(24);
textAlign(CENTER, CENTER);
text(inch_coord * 1, 170, 25);
popMatrix();
}
}
1