Basic Stamp 2 comparison problem
seraph
Posts: 2
Just trying to learn my way around PBASIC. I wrote the following to adjust a digital pot. When I enter a value to set the pot level, it is subtracted from the old value to get the number of pulses to send to adjust to the new level. If the new value is less than the old, then the pulses number is negative. But when the If then checks to see if pulses is negative or positive, it always evaluates to positive and skips the code that lowers the pot value. Any help on what is happening here would be appreciated.
' {$STAMP BS2}
' {$PBASIC 2.5}
idx VAR Word
digits VAR Word
udpin CON 5
clkpin CON 6
oldnum VAR Word
pulses VAR Word
'turn pot all the way off
LOW udpin
FOR idx=0 TO 127
PULSOUT clkpin,1
PAUSE 1
NEXT
'first pass
DEBUG "Enter potentiometer setting and press Enter (0-127)", CR
DEBUGIN SNUM digits
oldnum=digits
HIGH udpin
FOR idx = 0 TO digits
PULSOUT clkpin,1
PAUSE 1
NEXT
DO
DEBUG "Enter potentiometer setting and press Enter (0-127)", CR
DEBUGIN DEC digits
pulses = digits - oldnum
DEBUG SDEC ? pulses 'this correctly shows a negative number for pulses if the new entry is less than the previous entry
IF pulses < 0 THEN 'pulses never evaluates to less than zero so this part never executes
DEBUG "Pulses < 0", CR
pulses = -pulses 'change pulses to positive number
DEBUG "New pulses value ", DEC pulses
LOW udpin
FOR idx = 0 TO pulses
PULSOUT clkpin,1
PAUSE 1
NEXT
ELSEIF pulses > 0 THEN
DEBUG "Pulses > 0", CR
DEBUG SDEC ? pulses
HIGH udpin
FOR idx = 0 TO pulses
PULSOUT clkpin,1
PAUSE 1
NEXT
ELSE
DEBUG CR, "No change in value"
ENDIF
oldnum=digits 'move digits value to oldnum for next cycle
LOOP
' {$STAMP BS2}
' {$PBASIC 2.5}
idx VAR Word
digits VAR Word
udpin CON 5
clkpin CON 6
oldnum VAR Word
pulses VAR Word
'turn pot all the way off
LOW udpin
FOR idx=0 TO 127
PULSOUT clkpin,1
PAUSE 1
NEXT
'first pass
DEBUG "Enter potentiometer setting and press Enter (0-127)", CR
DEBUGIN SNUM digits
oldnum=digits
HIGH udpin
FOR idx = 0 TO digits
PULSOUT clkpin,1
PAUSE 1
NEXT
DO
DEBUG "Enter potentiometer setting and press Enter (0-127)", CR
DEBUGIN DEC digits
pulses = digits - oldnum
DEBUG SDEC ? pulses 'this correctly shows a negative number for pulses if the new entry is less than the previous entry
IF pulses < 0 THEN 'pulses never evaluates to less than zero so this part never executes
DEBUG "Pulses < 0", CR
pulses = -pulses 'change pulses to positive number
DEBUG "New pulses value ", DEC pulses
LOW udpin
FOR idx = 0 TO pulses
PULSOUT clkpin,1
PAUSE 1
NEXT
ELSEIF pulses > 0 THEN
DEBUG "Pulses > 0", CR
DEBUG SDEC ? pulses
HIGH udpin
FOR idx = 0 TO pulses
PULSOUT clkpin,1
PAUSE 1
NEXT
ELSE
DEBUG CR, "No change in value"
ENDIF
oldnum=digits 'move digits value to oldnum for next cycle
LOOP
Comments
There are several workarounds but none look very elegant. Tracy Allen suggests adding an offset to keep the number positive. There are other examples in the "Similar Threads" below.
To get an idea of how numbers are stored in the Stamp (two-compliment) you can use the calculators in Windows in "Programmers" mode. Enter -127 to see how it looks in binary. Try changing the number of bits to "Word" and "Byte" to see what this does to the number.
IMO, you might be better off using a number between 0 and 255 instead of trying to work with negative numbers.
IF pulses < 0 THEN ...
Try
IF pulses.BIT15 = 1 THEN ...
If a number is "negative" then it's most significant bit will be a 1 and will let you compare properly. The only hitch is the subtraction will possibly be so big as to lead to a positive number. In other words, this will only work if the result of the subtraction will certainly be >= than -32768 and < 32767
God bless!