Can someone help me with the maths, Please!

JPU

Senior Member
Hi, thank you looking.

Im struggling with what should be simple but I cant get my head around it and I am just going around in circles. Its even harder as we cant deal in fractions!

I have an ADC input that ranges from 340 - 1000

Id like to take the input and use it to set the speed of a motor that is controlled using the pwmduty command and the range is 0-1024.

How do I take my 340 -1000 input and turn that into a value for the Pwmduty of 0 - 1024

I have come up with this bit of code but its clunky and the motor speed up has obvious incremental steps and Id like it to be more linear?

Code:
pwmout b.4,255,0

main:
Readadc10 b.5,adcvalu
adcvalu=adcvalu-340
adcvalu=adcvalu*15/10
PWMDUTY B.4,adcvalu

etc
etc

goto main
Thanks for helping as I have about given up and I am going around in circles.

JPU
 

hippy

Senior Member
It should be working without any incremental speed-ups. You could check it does work smoothly with -
Code:
Do 
  For w0 = 340 To 1024
    adcvalu=w0
    adcvalu=adcvalu-340
    adcvalu=adcvalu*15/10
    PWMDUTY B.4,adcvalu
    Pause 10
  Next
  For w0 = 1023 To 341 Step -1
    adcvalu=w0
    adcvalu=adcvalu-340
    adcvalu=adcvalu*15/10
    PWMDUTY B.4,adcvalu
    Pause 10
  Next
Loop
It would be worth using "adcvalu=adcvalu Min 340 - 340" just in case it comes in under 340.

Another alternative for your *15/10, an approximation for *1024/(1000-340) is -

adcvalu = adcvalu ** 36143 + adcvalu
 
Top