How to only report data if matches this argument

I'm trying to send data over xbee and having an issue with noisy analog sensors.

The xbee isn't the issue, just the function itself.

I want to create a function which sets a "sendState' variable to HIGH if the sensor changes more than two increments/decrements. It will only go to LOW if it recognises that the changes aren't going over the two inc/decs.

If the variable is HIGH then I can proceed to sendData.

Can anyone suggest a solution. I've spent countless days trying to find one and my honours project deadline is looming. I hope this explained clearly.

Thanks in advance.

To make it clear, you want to set a flag when you see a change of more then 2?

Then it would be something like:

newValue = getNewValue(); Don't know where it comes from

if(abs(newValue - oldValue) > 2){
  sendState = true;
}
oldValue = newValue;

Thanks for the reply. That has solved my initial problem. It has also realised that it isn't a solution to my greater problem (this is still good news as it means I have to look elsewhere). I will elaborate further now that I'm home.

The sensor (as many sensors go) will give an imperfect value, it usually fluctuates between 2 and 3 values.

Every time the sensor value changes, a packet will be sent from the xBee. Unfortunately due to the nature of the network, this means that 8 different devices are sending to the one xBee every time a value changes on each device . This means it is being overwhelmed with data as the packets are constant.

To resolve this I'm trying to configure each device to send data only when I'm moving the sensor. Any further ideas?

Thanks again.

dean_ixd: when I'm moving the sensor.

What does that mean? Do you mean physically moving the sensor to another place? How will the Arduino know that a sensor is being moved?

What sensors are you using?

...R

Hey Robin, I'm using a magnetometer. I mean turning the sensor in order to change the reading (valid point, i should have added that!).

dean_ixd: Hey Robin, I'm using a magnetometer. I mean turning the sensor in order to change the reading (valid point, i should have added that!).

You did not explain how the Arduino would know that you have turned the sensor.

Also it would help my understanding if you explain how the sensors get turned and how often. I don't know how a magnetometer works.

...R

As far as I can tell my solution is a perfect solution for that… Now all you have to do is only sent when sendState is true (and reset it back to false). Tadaaaaaa, done