I don't have a device on the serial port, so I will simulate the data:
Code:int numberOfGraphPoints=1200;
float[] tempValsA = new float[numberOfGraphPoints]; // initialize 1st array
float[] tempValsB = new float[numberOfGraphPoints]; // initialize 2nd array
int currentPoint=0; // the marker used to travel through the array.
//Serial commPort;
int previousGrabTime=0, timeStep=10; // low timestep for fast demo
float tempC;
float tempF;
float tempB;
float tempEnvF;
void setup(){
size(800,600);
background(255); // white
currentPoint=0; // reset array marker
// commPort = new Serial(this, Serial.list()[1], 9600);
}
float prevTemp = 20; // Just for simulation
void draw()
{
// while (commPort.available() > 1)
{
// tempC = commPort.read();
// tempB = commPort.read();
tempC = prevTemp + random(-0.5, 0.5);
prevTemp = tempC;
// converts tempC to Fahrenheit
tempF = ((tempC * 9) / 5) + 32;
// converts tempB to Fahrenheit
tempEnvF = ((tempB*9)/5) + 32;
}
if (currentPoint < numberOfGraphPoints){ // don't exceed your arrays
if (millis() - previousGrabTime >= timeStep){ // if it's time to grab
// grab the values for A and B
// store them in the two float[] arrays
tempValsA[currentPoint] = tempF; // plug the values into the arrays
float x = map(currentPoint, 0, numberOfGraphPoints, 0, width);
float y = map(tempF, 0, 375, height, 0);
stroke(0);
point(x, y);
currentPoint++; // add 1 to the current point
previousGrabTime = millis(); // reset the wait time
}
}
}
You have already the previous temp, since you store them in an array (not really using it, currently, but it will come later). So the change is simple:
Code: float x = GetX(currentPoint);
float y = GetY(tempF);
float px = GetX(currentPoint - 1);
float py = GetY(tempValsA[currentPoint - 1]);
stroke(0);
line(px, py, x, y);
I have put the code to map points in functions to avoid duplication of code:
Code:float GetX(float v)
{
return map(v, 0, numberOfGraphPoints, 0, width);
}
float GetY(float v)
{
return map(v, 0, 375, height, 0);
}
And I start currentPoint at 1...
Well, actually there is a simpler method, with less computation, making px and py global and assigning them to x and y before changing the latter...