PDA

View Full Version : Finding the change and direction of a signed word value (sensor output) ?



mr.sneezy
- 17th May 2010, 12:24
I'm being bogged down by the math involved in using a I2C pressure sensor to calculate true pressure, so I'm looking for a simpler course to just detect a positive or negative changes in it's output rather calculate than it's absolute value.

At this point I only need to know if pressure is rising, falling, or steady. The sensor outputs a signed 16bit value.

Has anybody seen or used some PBP code to compare a new Word value against a previous Word value to see if the difference is positive or negative, and by how much ?

I'm short on bright ideas right now, so anything may help me get going the right way.

mackrackit
- 17th May 2010, 12:37
Maybe something like this?

If reading1 > reading2 then
diff = reading1 - reading2
else
diff = reading2 - reading1
endif

Then maybe set a flag to know which calc was used?

Acetronics2
- 17th May 2010, 12:46
Hi, Martin

Assuming :confused: pressure will be read as i.e 1013.3 mb ( displayed number < 65536) ...

the difference between two measures will be < 32768 ... ( or you will feel really really really bad !!! )

sooo ...

the MSB of the difference will be 0 for positive difference and 1 for neg difference... add to that PBP offers the direct print of the value and sign : " SDEC Difference "

Alain

mr.sneezy
- 17th May 2010, 13:31
Hi,

Assuming :confused: pressure will be read as i.e 1013.3 mb ( displayed number < 65536) ...

the difference between two measures will be < 32768 ... ( or you will feel really really really bad !!! )

sooo ...

the MSB of the difference will be 0 for positive difference and 1 for neg difference... add to that PBP offers the direct print of the value and sign : " SDEC Difference "

AlainThe sensor is a Bosch BMP085. The actual raw un-compensated pressure value at about 260m (where I live) is -24200 in SDEC. I'm not sure what the range of values is going to be yet, I need to work that out yet (from sea level to say 1000m).

I'll try out the SDEC Difference to see what it does too.

mr.sneezy
- 17th May 2010, 13:35
Maybe something like this?

If reading1 > reading2 then
diff = reading1 - reading2
else
diff = reading2 - reading1
endif

Then maybe set a flag to know which calc was used?Thanks, that looks simple enough for a start tonight.

I'd like to take the difference value and average it too, before deciding if the change is big enough to use and not just sensor noise. To average the raw output of the sensor needs 32bit math I think, so this has got to be easier for me.

mackrackit
- 17th May 2010, 16:05
The difference should be smaller than a WORD so if you do the average of a couple you should not need 32 bit math. If it does over flow the WORD then I think that would be telling you that the difference is larger enough to... make a difference :)

mr.sneezy
- 18th May 2010, 06:34
The difference should be smaller than a WORD so if you do the average of a couple you should not need 32 bit math. If it does over flow the WORD then I think that would be telling you that the difference is larger enough to... make a difference :)OK, yes I see that.
Is there any drawback to averaging the difference between the numbers rather than the total of the numbers ?

mackrackit
- 18th May 2010, 16:40
OK, yes I see that.
Is there any drawback to averaging the difference between the numbers rather than the total of the numbers ?
It sounds like you are after a relative measurement and not concerned withnthe actual so averaging the difference should be fine.

Are you thinking the Difference values will be small enough to use more samples in the average? Should be on target then.