# A Maths Question

#### neiltechspec

##### Senior Member
Yet again I seem to be struggling with maths.

Built a Wideband AFR guage that displays 0v to 5v as 10 to 20 AFR.
This used,
Code:
``````    readadc sens,adc
afr = adc*20/51+100 max 199     'max  value limited to 19.9
bintoascii afr,b10,b11,b12``````
It worked well enough.

Now I have changed the Lambda Controller.
This gives from 0v to 5v as 7.35 to 22.39 AFR, supplier says to convert Volts to AFR use - V*15.04/5+7.35.
So I have tried, afr = adc * 10 / 51 * 15 / 5 + 73 max 199
But this seems to give large jumps between readings.

Any suggestions on the maths.

Have subsequently come up with this below, untested on H/W yet though

Code:
``````    readadc10 sens,adc
afr = adc / 2 * 15 / 5 + 735 max 1999
bintoascii afr,b10,b11,b12,b13,b14

Last edited:

#### hippy

##### Technical Support
Staff member
This may help ...

afr = adc * 1504 / 255 + 735
afr = adc * 5.898039215686275 + 735
afr = (adc * 5) + (adc * 0.898039215686275 * 65536 / 65536) + 735
afr = (adc * 5) + (adc * 58854 / 65536) + 735

Code:
``````Symbol adc = b0
Symbol afr = w1

adc =   0 : Gosub CalcAfr
adc =   1 : Gosub CalcAfr
adc = 254 : Gosub CalcAfr
adc = 255 : Gosub CalcAfr
End

CalcAfr:
afr = adc ** 58854 + afr + 735

ShowAfr:
b13 = afr / 100
b12 = afr / 10  // 10
b11 = afr       // 10
SerTxd(#adc, TAB, #afr, TAB, #b13, ".", #b12, #b11, CR, LF)
Return``````
Code:
``````0       735     7.35
1       740     7.40
254     2233    22.33
255     2239    22.39``````

#### neiltechspec

##### Senior Member
Thanks hippy, look interesting.

I'll try it on the H/W tomorrow.

for info, entire code is here.

Code:
``````#rem
7 Seg Com Anode display direct drive AFR Meter
0-5v, 7.3 - 22.4 AFR. (AFR = Volts *15.04/5+7.35)
Display limited from min 10.0 to max 19.9

Using PICAXE20M2, V2, Jun 2022

Connections:

tenths
c.0 ..... 330R to Segment  a
c.1 ..... 330R to Segment  b
c.2 ..... 330R to Segment  c
c.3 ..... 330R to Segment  d
c.4 ..... 330R to Segment  e
c.5 ..... 330R to Segment  f
c.7 ..... 330R to Segment  g

units
b.1 ..... 330R to Segment  a
b.2 ..... 330R to Segment  b
b.3 ..... 330R to Segment  c
b.4 ..... 330R to Segment  d
b.5 ..... 330R to Segment  e
b.6 ..... 330R to Segment  f
b.7 ..... 330R to Segment  g

470R to -ve for DP
470R's from A.0 to Tens segments B & C
#endrem

#picaxe 20m2
#no_data

symbol sens  = b.0    'has 47uF across it & 10k in series with I/P to damp readings
symbol afr   = w2
symbol volts = w3
symbol delay = 100

init:
pullup 16384
high A.0
dirsB = %11111110
dirsC = %10111111
pinsB = %11111110 'blank tenths
pinsC = %10111111 'blank units
pause 1000
gosub test
pause 1000
setfreq m16

afratio:    '7.35 to 22.39 afr, limited to min 10.0, max 19.9
do
afr = adc ** 30815 + adc + 735 min 1000 max 1999
bintoascii afr,b10,b11,b12,b13,b14

select case b13
case 48 let pinsB = %10000000    '0
case 49 let pinsB = %11110010    '1
case 50 let pinsB = %01001000    '2
case 51 let pinsB = %01100000    '3
case 52 let pinsB = %00110010    '4
case 53 let pinsB = %00100100    '5
case 54 let pinsB = %00000110    '6
case 55 let pinsB = %11110000    '7
case 56 let pinsB = %00000000    '8
case 57 let pinsB = %00110000    '9
endselect

select case b12
case 48 let pinsC = %10000000    '0
case 49 let pinsC = %10111001    '1
case 50 let pinsC = %00100100    '2
case 51 let pinsC = %00110000    '3
case 52 let pinsC = %00011001    '4
case 53 let pinsC = %00010010    '5
case 54 let pinsC = %00000011    '6
case 55 let pinsC = %10111000    '7
case 56 let pinsC = %00000000    '8
case 57 let pinsC = %00011000    '9
endselect

if b11 = 48 then high A.0
else low A.0
endif

pause 400
loop

test:
pinsB = %10000000    'display "0"
pinsC = %10000000    'display "0"
pause delay
pinsB = %11110010    'display "1"
pinsC = %10111001    'display "1"
pause delay
pinsB = %01001000    'display "2"
pinsC = %00100100    'display "2"
pause delay
pinsB = %01100000    'display "3"
pinsC = %00110000    'display "3"
pause delay
pinsB = %00110010    'display "4"
pinsC = %00011001    'display "4"
pause delay
pinsB = %00100100    'display "5"
pinsC = %00010010    'display "5"
pause delay
pinsB = %00000110    'display "6"
pinsC = %00000011    'display "6"
pause delay
pinsB = %11110000    'display "7"
pinsC = %10111000    'display "7"
pause delay
pinsB = %00000000    'display "8"
pinsC = %00000000    'display "8"
pause delay
pinsB = %00110000    'display "9"
pinsC = %00011000    'display "9"
pause delay
low A.0
pause delay
return``````

Last edited:

#### neiltechspec

##### Senior Member
Updated the code above.

Works well.

#### hippy

##### Technical Support
Staff member
[
Works well.
Excellent news, glad I could help.

I am not sure if the "N * T/B" => "N * X.Y" => "N ** K" trick is properly documented anywhere. I'll take a look and rectify that if not.