I have been doing a fair bit of C programming, and using the map function, somemthing that is missing from the picaxe system.
So i thought it will work in interger maths and breaking ti down into its seperate parts to correct for order of operations should allow it to work on the picaxe.
Well it does, providing you keep the intermedarey result (in this case stored in W2) below the 65535) mark you should be fine. It needs work, and knowing that there are some pretty clever folks here I suspect that the upper limit of what can be calulated can be increased.
I put here as its not really a code snipet yet, it needs work an i ma sure it can be steamlined to use less variables, but even its current form mamybe usfeul for some one
So i thought it will work in interger maths and breaking ti down into its seperate parts to correct for order of operations should allow it to work on the picaxe.
Well it does, providing you keep the intermedarey result (in this case stored in W2) below the 65535) mark you should be fine. It needs work, and knowing that there are some pretty clever folks here I suspect that the upper limit of what can be calulated can be increased.
I put here as its not really a code snipet yet, it needs work an i ma sure it can be steamlined to use less variables, but even its current form mamybe usfeul for some one
Code:
symbol val = w0
symbol in_min = 0
symbol in_max = 1023
symbol out_min = 0
symbol out_max = 180
symbol out_val = out_max-out_min 'pre calc out_val
symbol in_val = in_max-in_min 'pre cal in_val
'map (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
map:
w1 = val - in_min
w2 = w1 * out_val ',ust not exceed 65535 or overflow will happen
'in this case min out val is 0 so 65535/in_max gives 64, so val should not exceed 64
w3 = w2 / in_val + out_min 'w3 contains the output
pause 50 'pause to follow what going on
'section for incrementing and checking for overflow of val
val = val + 1
if val > 63 then
val = 0
end if
goto map