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 › How you compare latest input value to previous
Page Index Toggle Pages: 1
How you compare latest input value to previous? (Read 1341 times)
How you compare latest input value to previous?
Oct 29th, 2009, 1:22pm
 
Hi,

I have been trying to figure out how to compare the latest input value to the previous input value? I am sure it is very simple but I can't do it and apologise for that.

I have tried something along the lines of:

//Initialise an array of stored values

float[] storedVals = new float[300];
int arrayPosition = 0;

// And then in draw putting the variable into the storedVal array:

storedVals[arrayPosition] = myVariable;
 
// Then an awful attempt at trying to say if the latest variable input is //different to the previous then execute something:

       if (storedVals[1] != storedVals[0]) {
       
//Execute something
Re: How you compare latest input value to previous?
Reply #1 - Oct 29th, 2009, 2:13pm
 
 if (storedVals[arrayPosition-1] != storedVals[arrayPosition]) {
perhaps. Or just have a variable holding the latest value.
Re: How you compare latest input value to previous?
Reply #2 - Oct 29th, 2009, 3:11pm
 
Hi PhilLo,

Firstly, my apologies about all the posts over the last few week.

I have tried what you suggested and it is working potentially except that I am getting the correct input variable for one arrayPosition and 0.0 for the other when I use println();

Firstly, I wondered if there was any chance of also understanding your second suggestion which was to create a variable to store the previous value. How would I do this.

Secondly, here is the updated code that is correctly creating an input variable for [arrayPosition-1] but not [arrayPosition]  

  Code:
float myVariable = (d.getValue(1))*100; 

float[] storedVals = new float[300];
int arrayPosition = round(myVariable);

println(myVariable);

storedVals[arrayPosition] = myVariable;
arrayPosition++;

println(storedVals[arrayPosition-1]);
println(storedVals[arrayPosition]);

if (storedVals[arrayPosition] > storedVals[arrayPosition-1] ) {

// Execute something
Re: How you compare latest input value to previous?
Reply #3 - Oct 29th, 2009, 10:34pm
 
No need for apologies, you need to learn and we like to help! Smiley

I fail to understand your snippet, either it is in a bigger context (which I supposed in my first answer), or it is lacking something essential... Smiley

I supposed your array was already populated with values. For this, you need a for loop (for example) and a data source.

I don't know where your d comes from, and how arrayPosition can come from myVariable.

Perhaps you should provide a bit more context.
Re: How you compare latest input value to previous?
Reply #4 - Oct 30th, 2009, 12:50am
 
Hi PhilLo,

Thank you again for the reply. The code that I have attempted to use is probably wrong and is I imagine confusing and it would probably be easier and more clear if I just say what my intention is and provide the Pachube code that I am integrating into my overall sketch.

The Pachube (wind speed data) variable is coming into the sketch every 3.5 seconds. At the basic level I just wanted to store the previous Pachube input and then compare it to the latest variable, and then after compared it and executed something to store the latest variable as the previous variable etc. i.e. to keep comparing the latest variable to the previous variable.

I suppose ideally I would love to take it further and be able to compare a series of variables, but to be honest as it's a rolling input that could continue indefinetely, I wouldn't know how to do that (and perhaps it's the subject of a different post).

Here is the code:
Code:

import eeml.*;

DataIn dIn;

void setup(){
// e.g. every 15 seconds
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/1197.xml", "API KEY HERE", 3500);
}

void draw()
{
// do whatever needs doing in the main loop
}

// onReceiveEEML is run every time your app receives back EEML that it has requested from a Pachube feed.
void onReceiveEEML(DataIn d){
float myVariable = d.getValue(1); // get the value of the stream 1
println(myVariable);
}


Thank you again PhilLo


Re: How you compare latest input value to previous?
Reply #5 - Oct 30th, 2009, 4:29am
 
Perhaps something like:
Code:
import eeml.*;

DataIn dIn;
float[] storedVals = new float[300];
int arrayPosition = 0;

void setup(){
// e.g. every 3.5 seconds
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/1197.xml", "API KEY HERE", 3500);
}

void draw()
{
// do whatever needs doing in the main loop
}

// onReceiveEEML is run every time your app receives back EEML that it has requested from a Pachube feed.
void onReceiveEEML(DataIn d){
float myVariable = d.getValue(1); // get the value of the stream 1
println(myVariable);
storedVals[arrayPosition] = myVariable;
if (arrayPosition+1 == storedVals.length)
{
popOldestValue();
}
else
{
arrayPosition++;
}
}

void popOldestValue()
{
for (int i = 1; i < storedVals.length; i++)
{
storedVals[i - 1] = storedVals[i];
}
}
Re: How you compare latest input value to previous?
Reply #6 - Oct 31st, 2009, 5:56am
 
Hi PhilLo,

Thank you again for your help!

Cheers
Page Index Toggle Pages: 1