Operation of Min Max


Senior Member
Have the following code which does not behave as expected.
PICAXE 14m Firmware 9.B

'Test Mode
'Monitor ADC values during setup - Anderson K107 LCD Chip
If pin4 = 1 then sertxd(" X",#b6," Y",#b7," Z",#b8,"?n") ' Pin4 is high only on testing
 ReadADC10 1, w0 'Dimension Engineering DE-ACCM3D Accelerometer +- 3G in X,Y & Z Axes
 ReadADC10 2, w1 '0g = 1.33V with 0.333v per g, reads 1g in Z axis when static
 ReadADC10 3, w2 'May need calibration to local Magnetic Field / position
 b6=w0-205 Min 0 Max 255 'centre output on 127, limit within byte range 
 b7=w1-208 Min 0 Max 255 '1G = 68 units of ADC10 @ 5.0v Ref
 b8=w2-271 Min 0 Max 255 'Correct Z Axis to centre point at 1G
 pause 500
 Goto Test 'Loop for continuous display
The idea here is that the resolution of readadc10 is desired but as the range of values is small enough to fit in a byte value, the range is shifted (the -205/208 and 271). However the range of values can extend past the 256 that a byte can hold so the idea of the min 0 and Max 255 is to stop overflows.

In practice what is happening is that as the Z axis value reduces to the zero end it jumps to 255 suggesting it is in fact overflowing at the -271 calculation prior to the application of the Min and Max limiters. This is not what I expected from the explanation in the documentation.
(I know the issue can be resolved as the Z axis reads 1G normally instead of 0 so the 'centre' of it's normal range is actually higher than the X and Y axes but this does not explain the behaviour of Min Max)

Any assistance would be appreciated.


Senior Member
As the PICAXE only deals with positive numbers, "MIN 0" does not work because any negative number is also a large positive number and therefore is greater than zero.

You'll need to rewrite your -

-- b6=w0-205 Min 0 Max 255

as -

-- b6=w0 Min 205 - 205 Max 255


Senior Member
Thanks very much, code now works as expected.

I expected the internal logic of the Min function to take care of that issue but I guess 'strictly left to right' really means what it says!