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 & HelpSyntax Questions › building a line graph for 2 temps - real time
Page Index Toggle Pages: 1
building a line graph for 2 temps - real time (Read 2453 times)
building a line graph for 2 temps - real time
Aug 16th, 2009, 1:25am
 
I'm trying to build a graph within one of my sketches that is already able to read the 2 thermocouples real time.
Can any one point me in the right direction to get started.
Basically, the temp would be along the y axis from 125 F to 500 F.  And the time along the x axis, from 0 to 20 minutes.  
I want the line to travel from left to right rising as the temp gets higher.  Basically the end of the line would be the current temp.

Re: building a line graph for 2 temps - real time
Reply #1 - Aug 16th, 2009, 5:44am
 
In Actionscript I'd use an array and push() and pop(), but I'm not sure there are equivalent array methods in Processing/Java...

One option might be to create a simple class to hold the temp data (must admit I'm not 100% sure this step is necessary).  Anyway you could then use an ArrayList in which to store your data.  You can then simply use add to append data to the ArrayList as it comes in.  Once your array grows to the full width of the graph (just a simple test against the length of the ArrayList) you would then just use remove to remove the first element, before appending the next bit of data to the end.

Once you have that structure in place it's just a matter of iterating through the ArrayList, retrieving the temp data and plotting it to the graph...

It may of course be that there's a simpler option, but I reckon the logic is sound.  A similar approach is used to smooth data with an arduino, and it may be that their example code might offer another solution.
Re: building a line graph for 2 temps - real time
Reply #2 - Aug 28th, 2009, 3:53pm
 
ok  still working on this one.
With a lot of help from Ben I was able to successfully, or at least as good as I could, set up 2 arrays.  Currently I'm only going to use one array to graph, until I figure out the correct method.  Then I'll add the second.

As of now, it graphs points.  What I want to do is make it a line that "snakes" across the graph as the temp changes.  I will need to put in a line() which needs 4 points.  My problem is, trying to figure out the code that will save the current temps as previous temps, so it can draw a line from the previous point to current point.
Does that make sense?  After each temp is recorded, I need that to turn into the previous temp, and so on.
Here is the code I have thus far.
Ben has been more than patient with me via PM, so I figured I'd spread my neediness around a bit. Any help is much appreciated.

Code:
import processing.serial.*;

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);
}

void draw()
{
while (commPort.available() > 1)
{
tempC = commPort.read();
tempB = commPort.read();

tempF = ((tempC * 9) / 5) + 32;
// converts tempC to Fahrenheit
tempEnvF = ((tempB*9)/5) + 32;
// converts tempB to Fahrenheit
}



 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
     
   
    float x = map(currentPoint,0,numberOfGraphPoints,0,width);
    float y = map(tempF,0,375,height,0);
   
   
   
   
     
     tempValsA[currentPoint]=tempF; // plug the values into the arrays
     
     
     
     currentPoint++; // add 1 to the current point
     
     previousGrabTime = millis(); // reset the wait time
     
     stroke(0);
     
     
    point(x,y);
   

   }
 
 }
}



Re: building a line graph for 2 temps - real time
Reply #3 - Aug 29th, 2009, 12:27am
 
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... Smiley
Re: building a line graph for 2 temps - real time
Reply #4 - Aug 29th, 2009, 8:39am
 
oh, that might be why my suggestions haven't been working!  Yes: px and py have to be global.  Should have mentioned.

float x, y, px=0, py=0;

void draw(){
 x = [get your x];
 y = [get your y];
 line (px,py,x,y);
 px = x;
 py = y;
}

--Ben
Re: building a line graph for 2 temps - real time
Reply #5 - Aug 29th, 2009, 5:07pm
 
well that was easy.  Thank you both.  However, I have a red vertical line, spanning the height of the screen.  Just at the very beginning.

Is there a way to get a thicker line? // nevermind just added strokeWeight(3);  

Code:
import processing.serial.*;

int numberOfGraphPoints=1200; // match width for simplicity in demo
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.
int currentPointA=0;
int maxTemp = 375;
int minTemp = 0;

Serial commPort;
int previousGrabTime=0, timeStep=1000; // low timestep for fast demo
float tempC;
float tempF;
float tempB;
float tempEnvF;
float x,y,px=0,py=0;
float xa,ya,pxa=0,pya=0;



void setup(){
 size(800,600);
 background(255); // white
 currentPoint=0; // reset array marker
 commPort = new Serial(this, Serial.list()[1], 9600);
}

void draw()
{
while (commPort.available() > 1)
{
tempC = commPort.read();
tempB = commPort.read();
// removed draw graph and placed in new procedure drawgraph()
// draw graph sets up graph area but doesn't plot data
tempF = ((tempC * 9) / 5) + 32;
// converts tempC to Fahrenheit
tempEnvF = ((tempB*9)/5) + 32;
// converts tempB to Fahrenheit
}



 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

   
    float x = map(currentPoint,0,numberOfGraphPoints,0,width);
    float y = map(tempF,minTemp,maxTemp,height,0);
    float xa = map(currentPointA,0,numberOfGraphPoints,0,width);
    float ya = map(tempEnvF,minTemp,maxTemp,height,0);
   

tempValsA[currentPoint]=tempF; // plug the values into the arrays
   

currentPoint++; // add 1 to the current point
currentPointA++;
previousGrabTime = millis(); // reset the wait time

stroke(0);
    line (px,py,x,y);
  px = x;
  py = y;
  stroke(255,0,0);
    line (pxa,pya,xa,ya);
    pxa=xa;
    pya = ya;
   }
 
 }
}
Re: building a line graph for 2 temps - real time
Reply #6 - Aug 30th, 2009, 1:30pm
 
I thought it was just the red line, but after removing the red line, I noticed the the black line is doing the same thing.  
Can any see, on my sketch above, why this is happening?

Also, if I want to make the graph into a smaller box in the sketch.  I'll map it for that area and draw the graph. Unfortunately if I draw the rectangle in draw, it continues to refresh over the lines.  Is there any way to avoid this?
Re: building a line graph for 2 temps - real time
Reply #7 - Aug 30th, 2009, 2:58pm
 
OK.  I guess I'm keeping a dialogue with myself.  Which is good.  But I was able to fix the problem.  I just drew boxes over the text, so they dont flash, which also covered the line from 0,0 to starting temp.  It works great now.  
Next step is to make the graph start to plot when the timer is started.  And then freeze when timer is stopped.


Re: building a line graph for 2 temps - real time
Reply #8 - Aug 30th, 2009, 6:45pm
 
Looks like the dialogue paid off.  I was able to figure it out.  working great so far.  Just need to fix the stopwatch problem.  And thats another thread. Thanks again.
Page Index Toggle Pages: 1