Struggling with basic maths!

John O

Senior Member
Hi Guys,

Maths isn't my strong point so I'd be grateful if someone could talk me through this.

I'm reading a readadc10 and trying to convert the result into 0 to 359

Code:
do
 readadc10  C.1, w0
 w1 = 360 / 32 * w0 / 32   ; saw that trick somewhere :)
 debug
 pause 100
loop
Although w0 reaches 1023, w1 never gets above 351. Doing the maths on an integer-only calcualtor is the same. I'm hoping that, as 1023 represents over 2.8 "units" per degree (the 360), I'd get better resolution than that.


John.
 

hippy

Ex-Staff (retired)
degrees = adc10 / K

so for 359 = 1023 / K

K = 1023/359

K= 2.8495821727019498607242339832869

Now you need to make the integer divisor as big as possible to reduce rounding errors, eg multiply by N, but you also need to multiply adc10 by N so the result is the same ...

degree = ( adc10 * N ) / (K * N )

Having N as big as possible reduces error but N cannot be bigger than 64 as (1023*65) is an overflow of 16-bit ( greater than 65535 ) so let's use N = 64, K*N = 182, so ...

degree = adc10 * 64 / 182

That's at least a good first pass. Someone better at maths may be able to work out a more accurate away of approaching it !
 
Last edited:

hippy

Ex-Staff (retired)
The other way is ...

degrees = adc10 / K

degrees = adc10 / ( 1023 / 359 )

degrees = adc10 * 359 / 1023

Then choose some N which makes adc10*(359/N) not overflow ...

degrees = adc10 * (359/N) / (1023/N)

N = 359 / (65535/1023) = 5.6039826047150377660791943236439

degrees = adc10 * (359/5.603) / (1023/5.603)

degrees = adc10 * 64 / 182
 

John O

Senior Member
Brilliant... many thanks :)

I understood your first method easiest so it's now duly bookmarked and printed out & put safe in my folder!

John.
 

Mike7081

New Member
Hippy you're quick at fixing your errors. I was working on a post showing the correct equation and just before I posted it I saw you had already edited your post. lol good job! You got the correct equation but the first part of your post should be:

degrees = adc10 / K

so for 360 = 1024 / K

K = 1024 / 360
K = 2.84444444444...
 

hippy

Ex-Staff (retired)
Welcome to the forum.

For "360 = 1024 / K" versus "359 = 1023 / K" ... That's an interesting point and I'm sure it will provoke some debate. Not sure my theoretical maths is up to that though ! In practice it's probably the same answer using PICAXE maths but there would be times where it does affect things for similar sitituations.
 

ckoehn

Member
Another way that decimal math has been used also works.

K= 2.8495821727019498607242339832869

Reduce to 2.85, no point going over to 10,000th

w1=w0*2
w1=w0*8/10+w1
w1=w0*5/100+w1



Clint
 
Top