Shop OBEX P1 Docs P2 Docs Learn Events
Easy way to get 2 bytes from vb to Stamp — Parallax Forums

Easy way to get 2 bytes from vb to Stamp

ArchiverArchiver Posts: 46,084
edited 2003-02-18 21:02 in General Discussion
Didn't I say I would share the success when I asked for ideas?
Here's the (veeery simple) vb code I came up with that sends a 16 bit value
to the stamp.
It works with a floating point number as well.
It looks a lot like Jon's modulus snippet from his N&V article, with one
difference..
The sample code only worked for SOME numbers input, not all.
This will work with any value - even if the binary expression has the msb
set.
Without the conditional statement it will not always work...

Dim Distance as Single 'starting value - single precision floating point
Dim HDim As Long 'High Byte result
Dim LDim As Long 'Low Byte result

HDist = Distance / 256
LDist = Distance Mod 256
If LDist > 127 Then
HDist = HDist - 1

Then just * the high byte by 256 & add to the low byte in Stamp Word space.
Voila, a clean 16 bit value - every time.

I plan on writing a book with all the little tricks that got my projects
past the sticky zones. Perhaps this will be one of them....

Chris
Sign In or Register to comment.