code to avoid value going below 1 doesn't work

wapo54001

Senior Member
I am working with two potentiometers and saving the position as Pot1Val and Pot2Val. I use LocalWord as variable storage. Pot2Val is the master value and Pot1Val determines how much above and below Pot2Val the Pot1Val will be set.

Most of the time it works well, but in certain cases the result drops below one and becomes 65535. I thought that using the "MIN 1" code would keep that from happening, but it does not.

IF Pot1Val = 1 and Pot2Val = 482 then
LocalWord = 482
Pot1Val = 482
Pot2Val = 1 (good result)

BUT if Pot1Val = 1 and Pot2Val = 481 then
LocalWord = 482
Pot1Val = 481
Pot2Val = 65535 (bad result, Pot2Val must remain at 1 or above)

What is wrong with my code??

Code:
SERTXD("P1&P2 ",#Pot1Val," ",#Pot2Val,"     ") 'show starting values
IF Pot1Val > 482 AND Pot1Val < 542 THEN
   Pot1Val = Pot2Val
ELSEIF Pot1Val < 483 THEN
	LocalWord = 483 - Pot1Val MIN 1
	Pot1Val = Pot2Val
	Pot2Val = Pot2Val - LocalWord MIN 1
ELSE
	LocalWord = Pot1Val - 541 MIN 1
	Pot1Val = Pot2Val - LocalWord MIN 1
ENDIF
SERTXD("LW, P1&P2 ",#LocalWord," ",#Pot1Val," ",#Pot2Val,cr,lf) 'show result of manipulation
 

Technical

Technical Support
Staff member
It's a common mistake, 65535 is indeed greater than 1, so the min 1 has no effect as 65535 is a valid value.

Use a suitable min before the subtraction, not after it.
 

inglewoodpete

Senior Member
You are probably aware that PICAXE commands (without brackets) are literally interpreted from left to right.

That means that in the command "LocalWord = 483 - Pot1Val MIN 1", the subtraction will occur before the minimum rule is applied.

I think the following code will perform the function that you require:
Code:
Pot1Val = Pot1Val MAX 482
LocalWord = 483 - Pot1Val
 
Last edited:

hippy

Technical Support
Staff member
If you can provide the result you require in a mathematical form, rather than us having to reverse engineer the result you are after, it may be easier to see an elegant solution for what you want to achieve.

As Technical notes a MIN before subtraction can prevent the underflow you are seeing -

x = a - b Min 1

With prevented underflow ...

x = a Min b - b Min 1
 

wapo54001

Senior Member
@hippy, so often I see a reply requesting to see the code when someone asks a question, so today I was careful to include my code with everything I thought would be useful information. As for mathematical form, I am hopeless at that, I can't think in those terms so you'll never see that from me.

This is the solution that eventually worked for me, thank you for the insights, gentlemen, on the use of MIN and MAX.

Pot2Val = Pot2Val MIN LocalWord - LocalWord MIN 1

Looking at the manual, I was under the impression that a) MIN and MAX always came at the end of a line and b) it required a number. It never occurred to me that it could be used within a line or that it could use a variable. If Technical deems it worthwhile, perhaps the manual could be revised to include a more robust illustration of the use. In any case, I now get it, thank you all.
 

srnet

Senior Member
Looking at the manual, I was under the impression that a) MIN and MAX always came at the end of a line and b) it required a number.
MIN appears under the section in the manual;

"The mathematical functions supported by all parts are"

And is treated the same as all the other functions, +, -, *, /, **, // etc.

Which does imply that if + 1 and + var1 is valid, then so is MIN 1 and MIN var1
 
Top