Re: How to prevent variable going below zero?
Originally Posted by
CuriousOne
Code:
reader:
adcin 0,Z
pause 100
IF Z>100 AND Z<130 THEN I=I+5
IF Z<100 THEN I=I-5
if i>255 then i=255
if i<5 then i=0
HPWM 2,I,10000
XY=I
gosub decoder
goto reader
Here is the simple code, the part if i>255 then i=255 works fine, but the part if i<5 then i=0 does not, because when i value goes below 0, it does not go below zero, it goes to 65535 and so on, completely messing whole code. Any ideas?
Using MAX and MIN operators provided by the compiler ????
4 times IF-THEN in a row, ... Time to have a look to SELECT CASE ...
BTW ... if you want your number staying between 0 and 255 ... use a BYTE, instead of a WORD ...
I'm just curious about what you try to do ( in the real world, of course ) with such a piece of code !!!
Alain
************************************************** ***********************
Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
************************************************** ***********************
IF there is the word "Problem" in your question ...
certainly the answer is " RTFM " or " RTFDataSheet " !!!
*****************************************
Bookmarks