Shop OBEX P1 Docs P2 Docs Learn Events
weird subtraction of accelerometer readings — Parallax Forums

weird subtraction of accelerometer readings

PAX-OPAX-O Posts: 5
edited 2007-08-06 02:28 in BASIC Stamp
Hello everybody,
The attached picture contains the output data from an accelerometer. Basically I am doing 2 readings from the accelerometer and then I am subtracting the values and display the result with D (Dmin and Dmax stand for the min and max deltas calculated from subtracting the two readings).

Now the weird part: When I do the subtraction of the two readings that are for example 2520 and 2519 or vice versa sometimes i get an answer of 5535 instead of just 1... I also get the 5535 delta when the accelerometer is just sitting on the board of education. Can someone explain to me why I can't just get a 1 or a -1 or other numbers that are reasonable and make sense and instead I get those extremes of 0 and 5535?
182 x 238 - 9K

Comments

  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-08-06 00:14
    You haven't posted any code so I assume you're using the MAX operator which· is intended for unsinged integers only. Look up the MAX operator in the Stamp Editor's help file. Wiki or Google "two's complement" to get an understanding on the subject.
    http://en.wikipedia.org/wiki/Two%27s_complement

    Also look into the ABS operator and HIGHBIT.
    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 8/6/2007 12:19:13 AM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-08-06 00:18
    5535 is the last four digits of 65535, which is $FFFF or -1. If you print 65535 using SDEC, you'lll see your -1. But TechnoRobo is right: MIN and MAX work with unsigned integers only.

    -Phil
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-08-06 01:02
    Good catch Phil. PAX-O must be using using DEC4 for formatting, therefore he should be using SDEC4 (if 4095 is his max reading) if he wants to keep the same format. Though the negative sign may throw the columns off.

    Here's one way to keep them inline:

    DEBUG· "Dmin", CRSRX,(10-Dmin.HIGHBIT),SDEC4 Dmin,CR
    DEBUG· "Dmax", CRSRX,(10-Dmax.HIGHBIT),SDEC4 Dmax,CR

    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 8/6/2007 1:21:29 AM GMT
  • PAX-OPAX-O Posts: 5
    edited 2007-08-06 02:17
    thanks for the quick responses! 4095 is the max reading because of the accelerometer itself. The truth is that i don't care that much about formatting the output but what I care about is how the memory looks like and how I can use it. I will read the wiki and find the usage of ABS and MIN and MAX and I'll get back to you.
  • PAX-OPAX-O Posts: 5
    edited 2007-08-06 02:23
    UPDATE... 2 minutes later the ABS function fixed my problem[noparse]:)[/noparse] thanks!
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-08-06 02:28
    At some point you had to be using DEC4 to get leading zeros such as "0000" in your screen cap. Your post is still using DEC and not SDEClike Phil suggested so you will still get 65535 for -1. Try phil's suggestion or my
    suggestion but change mine to :

    DEBUG CRSRXY, (7-axCount.HIGHBIT), (6),SDEC4 axCount
    DEBUG CRSRXY, (7-lowestCount.HIGHBIT), (11),SDEC4 lowestCount
    DEBUG CRSRXY, (7-highestCount.HIGHBIT), (12),SDEC4 highestCount
    DEBUG CRSRXY, (7-secondRead.HIGHBIT), (7),SDEC4 secondRead
    DEBUG CRSRXY, (7-D.HIGHBIT), (8), SDEC4 D
    DEBUG CRSRXY, (7-Dmin.HIGHBIT), (9), SDEC4 Dmin
    DEBUG CRSRXY, (7-Dmax.HIGHBIT), (10), SDEC4 Dmax


    and you'll get nice clean columns - like your ScreenCap

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 8/6/2007 2:37:50 AM GMT
Sign In or Register to comment.