Line Graph jittering non-stop!
in
Programming Questions
•
1 year ago
Hi guys, I know I'm really bad at programming but here goes...
I have used some example codes from this forum and I have met up with a problem with the results...
I am trying to get serial data from my Arduino uno and display the readings in graph form. However, it seems like my graph is jittering/flickering non-stop.
here is my code:
import processing.serial.*;
Serial myPort; // The serial port
float[] values;
int numPoints;
float centerY;
void setup() {
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 115200);
myPort.bufferUntil('\n');
size(800, 600);
smooth();
strokeWeight(2.0);
stroke(0);
numPoints = width;
centerY = height / 2.0f;
values = new float[numPoints];
}
void draw() {
background(255);
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 180, 0, height);
cycleValues();
values[0] = inByte; //set the current value, im gonna base it off the last value
noFill();
beginShape();
for (int i=0; i<numPoints; i++) { //loop through the points
float value = values[i];
float x = width - (width / numPoints) * i; //we want the newest value on the right
float y = height - value; //typically y axis gets larger at top
vertex(x, y);
}
endShape();
}
}
void cycleValues() { //drop the last value, move everything else up one
for (int i = numPoints-2; i >= 0; i--) {
values[i+1] = values[i];
}
}
I got to see my Arduino's serial data, but the image is flickering, no matter what it is, it's not what I expected it to be... May I have some guidance please and thank you in advance.
1