Shop OBEX P1 Docs P2 Docs Learn Events
goto be a simple way to get this fraction loop to work in BS2- I'm missing som — Parallax Forums

goto be a simple way to get this fraction loop to work in BS2- I'm missing som

rob jacksonrob jackson Posts: 23
edited 2007-11-22 17:36 in BASIC Stamp
gratio····· VAR byte
gratiofrac VAR word


DO WHILE pb5kill = 0 AND pb0estop = 1
pbagain:
SEROUT 7, 84, [noparse][[/noparse]148, "Gear Ratio " ,DEC2 gratio,".",DEC2 gratiofrac]
BUTTON pb2left, 0, 20, 1, pbcountleft, 0, rjnext1
gratiofrac = gratiofrac + 1
IF gratiofrac = 0 THEN gratio = gratio + 1
rjnext1:
BUTTON pb3right, 0, 20, 1, pbcountright, 0, rjnext2
gratiofrac = gratiofrac - 1
IF gratiofrac = 99 THEN gratio = gratio - 1
rjnext2:
LOOP

What I have is a left arrow to increase a gear ratio factor for my tachometer program.· And a right arrow to decrease it.

The issue is garatiofrac counts up to 255 cause it a word.· I want it to roll over and add a whole number to the variable gratio and reset itself to zero.· NOT continue to 255.

Can I limit the varible to only count up to 99 and reset to zero?· commands MIN and MAX don't seem right for this...

Thank You,

RobJ

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank You,


Rob Jackson
oleancomputers.com

Comments

  • FranklinFranklin Posts: 4,747
    edited 2007-11-22 02:49
    if gratiofraq = 100 then gratiofrac = 0 (and) gratio = gratio + 1

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • rob jacksonrob jackson Posts: 23
    edited 2007-11-22 12:49
    I thought it would be that simple too but I get a "binary operator" error.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thank You,


    Rob Jackson
    oleancomputers.com
  • RDL2004RDL2004 Posts: 2,554
    edited 2007-11-22 12:58
    You have gratiofrac declared as a WORD, this type of variable it can go up to 65535 not 255.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Rick
  • rob jacksonrob jackson Posts: 23
    edited 2007-11-22 13:47
    That's true but I'm only using 0-99 as a fractional roll over.

    I'm using GRATIO to be the "integer" of a decimal number and GRATIOFRAC to be the "decimal" part. When counting up in the decimal region I need to ADD one to GRATIO and reset GRATIOFRAC to 0.

    I'm thinking the problem is inherent to the BUTTON command using a different VAR "pbcountleft" to track its repeat feature??

    I'll listen to all ideas though! It has to be something so simple I'm missing and can't see!!! LOL



    tachcontrol: '*** Tachometer *********************
    COUNT 13, 250, tachcount 'tach input is pin 13
    wheelRPM = tachcount * 80
    engineRPM = (wheelRPM * gratio) + ((wheelRPM * gratiofrac)/100)
    RETURN

    DO WHILE pb5kill = 0 AND pb0estop = 1
    pbagain:
    SEROUT 7, 84, [noparse][[/noparse]148, "Gear Ratio " ,DEC2 gratio,".",DEC2 gratiofrac]
    BUTTON pb2left, 0, 20, 1, pbcountleft, 0, rjnext1
    gratiofrac = gratiofrac + 1
    IF gratiofrac = 0 THEN gratio = gratio + 1
    rjnext1:
    BUTTON pb3right, 0, 20, 1, pbcountright, 0, rjnext2
    gratiofrac = gratiofrac - 1
    IF gratiofrac = 99 THEN gratio = gratio - 1
    rjnext2:
    LOOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thank You,


    Rob Jackson
    oleancomputers.com
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-11-22 14:20
    Rob -

    If you'd like some real help with your problem, providing the entire program might be a start. At present, it can't even be compiled to see where the error is occuring due to missing variable definitions (among other things).

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • NetHogNetHog Posts: 104
    edited 2007-11-22 17:36
    Rob, try this (changes marked with **):

    gratio VAR byte
    gratiofrac VAR byte ' ** change 'word' to 'byte', as word is not required here (word is 0-65535, byte is 0-255, nib is 0-15, bit is 0-1)

    DO WHILE pb5kill = 0 AND pb0estop = 1
    pbagain:
    SEROUT 7, 84, [noparse][[/noparse]148, "Gear Ratio " ,DEC2 gratio,".",DEC2 gratiofrac]
    BUTTON pb2left, 0, 20, 1, pbcountleft, 0, rjnext1
    gratiofrac = gratiofrac + 1 ' ** in this case, gratiofrac is increasing, thus 99 will wrap to 100, and 255 will wrap to 0
    ' ** this means that the following logic is incorrect
    ' ** IF gratiofrac = 0 THEN gratio = gratio + 1
    ' ** use instead:
    IF gratiofrac >= 100 THEN ' ** condition for incrementing gratio
    gratio = gratio + 1 ' ** modify whole part
    gratiofrac -= 100 ' ** correct for this in fractional part
    ENDIF
    ' ** above I could have checked explicitly for gratiofrac = 100, and set it to 0
    ' ** however the above logic works for increments of 2, 3, 4, 5, 6, 7 etc, up to increment of 99.
    ' ** it follows same rules as when learning to add two large numbers, slightly optimized
    rjnext1:
    BUTTON pb3right, 0, 20, 1, pbcountright, 0, rjnext2
    gratiofrac = gratiofrac - 1 ' ** in this case, gratiofrac is increasing, thus 0 will wrap to 255, and 100 will wrap to 99
    ' ** again this logic is incorrect:
    ' ** IF gratiofrac = 99 THEN gratio = gratio - 1
    ' ** Solution is more challenging as STAMP doesn't do 'negative', but we know that the value should never be > 99
    ' ** We are working in 2's complement arithmatic here
    ' ** so assume any value > 99 as being a wrapped around negative number
    ' ** again, this is optimized for an decrement of 1 through 99 but no higher
    IF gratiofrac >= 100 THEN
    gratio = gratio - 1 ' ** modify whole part
    gratiofrac = gratiofrac + 100 ' ** add back the fractional part which is 100* whole part.
    ENDIF
    rjnext2:
    LOOP
Sign In or Register to comment.