Must be a shorter way.

the old fart

Senior Member
Hi Guys,

Is there a better / easier way to program this.

Its to show water level, sensed by sfr05. lower the distance, higher the level.


Only showing relative parts.


Code:
#picaxe 40x2

symbol oled=A.7
symbol baud=n2400

;bargraph setup CGram
serout oled,baud, (254,64,0,0,0,0,0,0,0,21)     	;0
serout oled,baud, (254,72,16,16,16,16,16,16,16,21)    ;1
serout oled,baud, (254,80,20,20,20,20,20,20,20,21)    ;2
serout oled,baud, (254,88,21,21,21,21,21,21,21,21)    ;3

WLevel = 165


main:

;simulates reading from sensor
inc WLevel
if WLevel>200 then let Wlevel=130:endif


;each bar = 2mm
aux1=WLevel-145/2 	;145 = highest level

if Wlevel<145 then let aux1=0:endif

serout oled,baud,(254,206)
if aux1=0 then serout oled,baud,(0,"HIGH",0):endif
if aux1=1 then serout oled,baud,(3,3,3,3,3,3):endif
if aux1=2 then serout oled,baud,(3,3,3,3,3,2):endif
if aux1=3 then serout oled,baud,(3,3,3,3,3,1):endif
if aux1=4 then serout oled,baud,(3,3,3,3,3,0):endif
if aux1=5 then serout oled,baud,(3,3,3,3,2,0):endif
if aux1=6 then serout oled,baud,(3,3,3,3,1,0):endif
if aux1=7 then serout oled,baud,(3,3,3,3,0,0):endif
if aux1=8 then serout oled,baud,(3,3,3,2,0,0):endif
if aux1=9 then serout oled,baud,(3,3,3,1,0,0):endif
if aux1=10 then serout oled,baud,(3,3,3,0,0,0):endif
if aux1=11 then serout oled,baud,(3,3,2,0,0,0):endif
if aux1=12 then serout oled,baud,(3,3,1,0,0,0):endif
if aux1=13 then serout oled,baud,(3,3,0,0,0,0):endif
if aux1=14 then serout oled,baud,(3,2,0,0,0,0):endif
if aux1=15 then serout oled,baud,(3,1,0,0,0,0):endif
if aux1=16 then serout oled,baud,(3,0,0,0,0,0):endif
if aux1=17 then serout oled,baud,(2,0,0,0,0,0):endif
if aux1=18 then serout oled,baud,(1,0,0,0,0,0):endif
if aux1>18 then serout oled,baud,(0,"LOW",0,0):endif


goto main


Thank you.

TOF
 

hippy

Technical Support
Staff member
It draws a bar graph on the OLED screen I presume.

You could try a SELECT-CASE which would mean less source code and more visually pleasing to some but it would be much the same as you have under the cover, with even more memory used, so no real gain but would give an increase in speed..

You can output variables in each character position so there's only one actual SEROUT, then set the variables as required.

Setting all to 3 then only changing those which aren't would reduce the number of assignments. You could have multiple IF-ENDIF to alter just one or two variables, which then fall into the next IF-ENDIF to reduce the number of assignments.

You could however store the output sequences in EEPROM or TABLE then index into those, copy those sequences into variables then the SEROUT. That could be quite efficient using TABLECOPY but does not exist on X2 devices.

Unless there's a compelling need to change what you have I would simply go the SELECT-CASE route.
 

westaust55

Moderator
In pseudo code:
Code:
Aux1= 18 - aux1 
Aux2= aux1 / 3 ==> no of 3's to send
If aux2 < 6 then
Aux3= aux1 // 3==> last digit to send
ENDIF
Avoiding typing long code via iPhone while on transport :)

Edit:
Also need to consider trailing zeros
If aux2<6 then
Aux4 = 5 - aux2 ==> no trailing zeros to follow
 
Last edited:

the old fart

Senior Member
I've ended up with this.

2 push buttons to move wlevel up and down.



Code:
#picaxe 40x2

symbol oled=A.7
symbol baud=n2400

;bargraph setup CGram
serout oled,baud, (254,64,0,0,0,0,0,0,0,21)     	        ;0 bars
serout oled,baud, (254,72,16,16,16,16,16,16,16,21)    ;1 bar
serout oled,baud, (254,80,20,20,20,20,20,20,20,21)    ;2 bars
serout oled,baud, (254,88,21,21,21,21,21,21,21,21)    ;3 bars

WLevel = 165
setwater=165  

main:

if pinb.1=1 then inc wlevel:endif
if pinb.0=1 then dec wlevel:endif



if WLevel>200 then let Wlevel=200:endif
if wlevel<130 then let wlevel=130:endif


aux1=setwater-27   ; high limit
aux2=setwater+27   ; low limit

if wlevel<aux1 then let aux3=0:goto limit:endif ;below low limit
if wlevel>aux2 then let aux3=18:goto limit:endif ;above high limit

aux3=wlevel-aux1 / 3

aux4=18-aux3/3    ; number of '3'
aux5=18-aux3//3   ; number remaining
aux6=6		; number of chr$ to display 


serout oled,baud,(254,206)    ;set start position

disp3:				;display 3 bars
if aux4>0 then 
	serout oled,baud,(3)
	dec aux6
	dec aux4
endif
if aux4>0 then goto disp3
if aux6=0 then goto bgfin     

serout oled,baud,(aux5) 	;display remaining bars
dec aux6
if aux6=0 then goto bgfin

bg0:
serout oled,baud,(0)    	;display no bars
dec aux6
if aux6>0 then goto bg0

bgfin:

goto main
 
Top