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 › need to calculate average temp change
Page Index Toggle Pages: 1
need to calculate average temp change (Read 779 times)
need to calculate average temp change
Dec 18th, 2009, 12:17pm
 
hello, back again.
I have successfully created a sketch, fnally, that graphs temperature during a coffee roast.  I'm trying to make some upgrades to the displayed information to make it more useful.  
The array is set for 20 minutes, and graphs each second until the roast is complete.
What I'm trying to do is display the average change/increase in temp over the last 15 seconds.  Basically take the current temperature and subtract the value of the temp 15 seconds prior, then average the change/second.  

Here is my array code:

Code:
if ((currentPoint < numberOfGraphPoints)
   && (millis() - previousGrabTime >= timeStep)
   && (commPort.available() > 2)
   && (commPort.read() == 255)){
   // if it's time to grab, the array is not full, AND the commport is available...
   
   tempC = commPort.read();
   tempB = commPort.read();
   tempF = ((tempC * 9) / 5) + 32;
   // converts tempC to Fahrenheit
   tempEnvF = ((tempB*9)/5) + 32;
   // converts tempB to Fahrenheit
   //refresh the background to clear old data
   // grab the values for A and B
   // store them in the two float[] arrays
   float x = map(currentPoint,0,graphPoints,30,530);
   float y = map(tempF,minTemp,maxTemp,520,145);
   float xa = map(currentPoint,0,graphPoints,30,530);
   float ya = map(tempEnvF,minTemp,maxTemp,520,145);
   tempValsA[currentPoint]=tempF; // plug the values into the arrays
   tempValsB[currentPoint]=tempEnvF;

   currentPoint++; // add 1 to the current point

   previousGrabTime =millis(); // reset the wait time
   println(currentPoint);
   
   if (tempF >= minTemp && tempF <= maxTemp){
  strokeWeight(3);
  stroke(beanColor);
  line (px,py,x,y);
   px = x;
   py = y;
   strokeWeight(0);
   }
   if(tempEnvF >= minTemp && tempEnvF <= maxTemp){
   strokeWeight (3);
   stroke(envColor);
   line (pxa,pya,xa,ya);
   strokeWeight(0);
   
   pxa=xa;
   pya = ya;
 
 
   }
   
   
 
 }


the temp I want to average is tempF
starting at currentPoint >= 15 I need to subtract tempF at currentPoint 1 from the current tempF then divide it by fifteen.  After each second get the currentPoint temp from 15 seconds previous to do the same calculation.  Any ideas?  Once I can get that value I can have it displayed on the sketch.

Thanks in advance guys.
Re: need to calculate average temp change
Reply #1 - Dec 18th, 2009, 1:15pm
 
Uhm, just take the temperature 15 seconds ago subtract from the current temperature and divide by 15... that will pretty much give you the average change/second
Re: need to calculate average temp change
Reply #2 - Dec 18th, 2009, 1:48pm
 
JR wrote on Dec 18th, 2009, 1:15pm:
Uhm, just take the temperature 15 seconds ago subtract from the current temperature and divide by 15... that will pretty much give you the average change/second


Yeah I know, but I want it to display on the sketch.  However I am trying to figure out how to write the code to get that value.  Basically, each second after 15 seconds I need to grab the temp value from 15 seconds ago.

if (currentPoint >= 15){
?
}
I'm having a hard time coding it.  Obviously I am still a novice.
Thanks again.
Re: need to calculate average temp change
Reply #3 - Dec 18th, 2009, 3:15pm
 
ok, somehow my brain jumped into gear and I figured it out.  Probably not the best coding, but it works!

where tempAvg = 1 , so after 15 seconds it starts at the first temp reading, then so on.

Code:
 if (currentPoint >= 15){
   
     temp15 = tempValsA[tempAvg];
     tempChange = tempF-temp15;
     tempSlope = tempChange/15;
     tempAvg++; // adds 1 to tempval current point
Page Index Toggle Pages: 1