Spin If - Then Question
ajward
Posts: 1,130
Hi All...
I'm working with an accelerometer on my Activity Board and am running to a bit of a roadblock. Can the IF statement resolve a condition with fractional numbers? That is will: If "value a" < 0.019 Then "do something" work?
I'm converting the ± values from the accelerometer to an absolute value with the || operator and trying to branch to a subroutine using the IF statement. If I twiddle the code to pass a whole number the code works as expected. If I pass the fractional number, the subroutine isn't accessed.
I can't see anything in the Prop. manual to suggest a limitation, but maybe I missed something. I can post the whole program, but being somewhat lazy, I thought I'd ask about the IF statement first.
Thanks for any wisdom!!!!!!!
Amanda
I'm working with an accelerometer on my Activity Board and am running to a bit of a roadblock. Can the IF statement resolve a condition with fractional numbers? That is will: If "value a" < 0.019 Then "do something" work?
I'm converting the ± values from the accelerometer to an absolute value with the || operator and trying to branch to a subroutine using the IF statement. If I twiddle the code to pass a whole number the code works as expected. If I pass the fractional number, the subroutine isn't accessed.
I can't see anything in the Prop. manual to suggest a limitation, but maybe I missed something. I can post the whole program, but being somewhat lazy, I thought I'd ask about the IF statement first.
Thanks for any wisdom!!!!!!!
Amanda
Comments
Is this in Spin or C?
In Spin, you can't compare against a floating point number like that. You might be able to in C.
You cannot use the || operator on a floating point number. It only works on fixed point numbers. However, you can get the absolute value of a floating point number by zeroing out the MSB. You can do this by ANDing with $7FFF_FFFF.
Are you sure that you're getting a floating point number from the accelerometer driver? If so, you're code should look something like this: If you're getting a fixed point value from the driver you will need to determine the fixed point value that is equivalent to 0.019, and use that in your comparison instead. You can also use the || operator with fixed point values.
Amanda
Edit: Followup... The accelerometer outputs a whole number that's manipulated to represent the the G-forces acting on the module. I can use the IF statement on that value to branch to the appropriate subroutine. It works pretty well, tho' some tweaking is needed.
I built the thing to detect unusual vibrations. I get an occasional Z-axis alert when some nearby resident in the building slams a door or goes tromping by in the hallway. :-)
I'm off to Hawaii.
Amanda, I'm not sure if your familiar with using floating point numbers on the Prop. Depending on your application, you may not need floating point numbers, you can just used scaled integers. If you do need floating point, then your math all needs to be done with a floating point object like F32. F32 and similar objects have a "Cmp" (IIRC) method which will let you compare floats.
You can see in the above comments, the FCmp method will let you now if the pair of floats are equal or which one is larger.