how to maintain position in OLED when number of digits in number varies

wapo54001

Senior Member
I would like to display a variable value in the AXE133Y display at the same position regardless of the number of digits in the number. I can do it like this, but there must be a better way?

Code:
IF W7 < 10 THEN
	serout c.7,N2400_16, (254,195,#w7)
ELSEIF W7 < 100 THEN
	serout c.7,N2400_16, (254,194,#w7)
ELSE
	serout c.7,N2400_16, (254,193,#w7)
ENDIF
pause 10
 

hippy

Technical Support
Staff member
It probably depends on what your definition of "better" is. You could calculate where it should be placed -

Code:
Select Case w7
  Case >= 100 : position = 193
  Case >= 10  : position = 194
  Else        : position = 195
End Select
SerOut C.7, N2400_16, ( 254, position, #w7 )
Or you could use BINTOASCII to get the component digits, turning leading zeroes into spaces.
 

Goeytex

Senior Member
Hippy's example is what I was going to suggest, but even then you still have to deal with residual characters when the number changes from 3 digits to 2 digits or 2 digits to 1 digit.
 

wapo54001

Senior Member
I tested a few options and eventually settled on this. It causes no flashing of the display (which was annoying when I was clearing the whole display when changing numbers) and keeps the code fairly short and it's fast enough for my purposes. If anyone can further optimize what I've got here I'd love to see it:

Code:
IF Pot2Val < 10 THEN
	serout c.7,N2400_16, (254,204,"  ",#Pot2Val)
ELSEIF Pot2Val < 100 THEN
	serout c.7,N2400_16, (254,204," ",#Pot2Val)
ELSE
	serout c.7,N2400_16, (254,204,#Pot2Val)
ENDIF
pause 10
RETURN
 

inglewoodpete

Senior Member
I wrote the following subroutine to handle formatting of decimal numbers a few years ago

Code:
' ----- ShowDec: Send Binary value to PE Terminal as formatted number -----------------------
'
'    Send numbers to PE terminal so they can appear is neat columns (Leading Zero Blanking)
'            
'    Entry:   wSendData      (word) data to be transmitted
'             wColumnDet     (word) Number of digit spaces to occupy
'                                               (10, 100, 1000, 10000)
'                                   wColumnDet could optionally be set within this subroutine
'    Exit:    wSendData      is unchanged
'
'
ShowDec: Do While wColumnDet > 1
            If wSendData < wColumnDet Then 'Eg if data < 10 then insert a " " (LZB)
               SerTxd(" ")                 'Pack display out by 1 space per unused digit
            EndIf
            wColumnDet = wColumnDet / 10
         Loop
         SerTxd(#wSendData)
         Return
 

oracacle

Senior Member
I have written several pieces but have not found a better way than this as yet

Code:
[color=Blue]if [/color][color=Black]adc0 [/color][color=DarkCyan]< [/color][color=Navy]10 [/color][color=Blue]then
                        serout [/color][color=Black]screen, baud, [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],[/color][color=Navy]202[/color][color=Black],[/color][color=Red]"Err"[/color][color=Blue])
                  else if [/color][color=Black]adc0 [/color][color=DarkCyan]>[/color][color=Navy]9 [/color][color=DarkCyan]and [/color][color=Black]adc0 [/color][color=DarkCyan]< [/color][color=Navy]100 [/color][color=Blue]then
                        serout [/color][color=Black]screen, baud, [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],[/color][color=Navy]202[/color][color=Black],[/color][color=Navy]32[/color][color=Black],#adc0[/color][color=Blue])
                  else if [/color][color=Black]adc0 [/color][color=DarkCyan]> [/color][color=Navy]100 [/color][color=Blue]then
                        serout [/color][color=Black]screen, baud, [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],[/color][color=Navy]202[/color][color=Black],#adc0[/color][color=Blue])
                  end if[/color]
this does print an error massage below a value of 10, but should give the idea

wrote this for the macro rail, should deal with numbers up to 9999

Code:
[color=Blue]let [/color][color=Black]curs [/color][color=DarkCyan]= [/color][color=Navy]135                                              [/color][color=Green]'set courser position
      [/color][color=Blue]if [/color][color=Black]imgcount [/color][color=DarkCyan]< [/color][color=Navy]10 [/color][color=Blue]then                                       [/color][color=Green]'check for less than 10 in varaible
            [/color][color=Blue]serout [/color][color=Black]screen,[/color][color=Blue]n2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],curs,[/color][color=Navy]32[/color][color=Black],[/color][color=Navy]32[/color][color=Black],[/color][color=Navy]32[/color][color=Black],#imgcount[/color][color=Blue])    [/color][color=Green]'insert 3 blanks to remove other digits from number greater that 9
      [/color][color=Blue]elseif [/color][color=Black]imgcount [/color][color=DarkCyan]> [/color][color=Navy]9 [/color][color=DarkCyan]and [/color][color=Black]imgcount [/color][color=DarkCyan]< [/color][color=Navy]100 [/color][color=Blue]then                 [/color][color=Green]'check for number greater than 9, but less that 100
            [/color][color=Blue]serout [/color][color=Black]screen,[/color][color=Blue]n2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],curs,[/color][color=Navy]32[/color][color=Black],[/color][color=Navy]32[/color][color=Black],#imgcount[/color][color=Blue])       [/color][color=Green]'insert 2 blanks to remove other digits from number greater than 100
      [/color][color=Blue]elseif [/color][color=Black]imgcount [/color][color=DarkCyan]> [/color][color=Navy]100 [/color][color=DarkCyan]and [/color][color=Black]imgcount [/color][color=DarkCyan]< [/color][color=Navy]1000 [/color][color=Blue]then              [/color][color=Green]'check for number greater than 100 but less than 1000
            [/color][color=Blue]serout [/color][color=Black]screen,[/color][color=Blue]n2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],curs,[/color][color=Navy]32[/color][color=Black],#imgcount[/color][color=Blue])          [/color][color=Green]'insert 1 blank to remove digits from number great than 999
      [/color][color=Blue]else if [/color][color=Black]imgcount [/color][color=DarkCyan]> [/color][color=Navy]1000 [/color][color=Blue]then                                [/color][color=Green]'check for number great than 999
            [/color][color=Blue]serout [/color][color=Black]screen,[/color][color=Blue]n2400[/color][color=Black], [/color][color=Blue]([/color][color=Navy]254[/color][color=Black],curs,#imgcount[/color][color=Blue])             [/color][color=Green]'print number
      [/color][color=Blue]end if[/color]
I found that both these work flawlessly for keeping the text were it should be
 
Top