how to constrain value?

if (-25<humidity_result<125){
humidity_r = humidity_result;
} else{
while ((humidity_result < -25) || (humidity_result > 125)){
delay(500);
humidity_result = humidity(temperatureC);
}
humidity_r = humidity_result;
}

Being humidity, it should be between zero and 100. I gave it some extra breathing room.

The first 2 readings I get on reboot are in the neighborhoods of 744% the first read, -6200% the second read, then third read is the correct 43%.

I applied the above code, and it didn't fix it.

Are bytes getting lost in the wireless transfer of data, or did my code just not fix anything?

You could use the constrain function

AWOL:
You could use the constrain function

Of course that would exist. What doesn't exist in C/Arduino? (sarcasm, don't answer that)

http://www.arduino.cc/en/Reference/Constrain

Ok, so how would I tell it to call the function again? the same if statement I already have, but looking for a and b? Or just the while loop?

it doesnt matter how long I wait. Whether I query the arduino twice immediately after restart, or if I wait a few minutes then query it. Still get 744 followed by -6200. So am I losing bytes, does it take 2 reads for Timer1 to initialize, or...?

Using FreqCounter library.

I don't understand what you're trying to achieve, but I very much doubt the first line of your code does what you intend:

if (-25<humidity_result<125)

I suspect you intend this to return true when humidity_result is greater than -15 and less that +125. It won't. It will be evaluated as two expressions. The first expression will be (25 < humidity_result), which will produce a boolean value. The second expression will be (boolean < 125), which will always be true since booleans are represented by int values 0 and 1 which are both less than 125.

If you want to check that the value is within a range, you need two expressions, for example:

if((humidity_result > -25) && (humidity_result < 125))

if (-25<humidity_result<125)

The if will result (nearly) allways in true: humidity is something between 0 and 100% (normally, but I can't see your whole sketch from here)

  1. -25< humidity_result will return true or false (0 or 1) which are both smaller than 125 ==> true

  2. humidity_result<125 will return in true (1) which is bigger than -25 ==> true

You could replace the block with only the while part

while ((humidity_result < -25) || (humidity_result > 125))
{
  delay(500);     // assuming this is needed for the sensor?
  humidity_result = humidity(temperatureC);  // is temperatureC constant or changing ???
}
humidity_r = humidity_result;

Thanks everyone. Didn't know that's how things were operated upon (I was indeed assuming literal). Rob's code worked right out of the box :slight_smile: