Page 1 of 1

Analog Percent difference

Posted: Thu Oct 06, 2011 12:21 pm
by jgu1
Hi everybody! :D

Can anyone help. I started making a project for a brake test equipment for cars. I measure the power consumption of the two engines that converts AC to DC. The hardware works, 0 - 2V analog in. and FC works with 2 bar graph in the display, that shows what they should. But I would have liked to read out the difference in percentages and I can not get it to work.

I want difference (example) calculated as a percentage: Ana0 = 150 and Ana1 = 140 and nomally you calculate this by:

150-140
_______ = 0.0714 * 100 = 14.7% Difference

140


If I make the calculations in FC calculationbox Percent = 150-140/140 * 100 I get some wrong calculations

Percent, An0 and An1 is defined as Int

Is there anyone who can help with the calculation, without any decimals after the comma.

Thank´s in advance :idea:

Best regard

Jorgen

Re: Analog Percent difference

Posted: Thu Oct 06, 2011 1:19 pm
by Benj
Hello Jorgen,

On a microcontroller mathematics is done using integer maths unless you are using float variables.

for example

150-140 = 10
10 / 140 = 0
0 * 100 = 0

You could use floating point maths to calculate the percentage but this is probably overkill and will add a lot to the program memory.

An alternative is to use an INT type variable to hold the results of the calculation and instead do it like this.

150 * 100 = 15000
140 * 100 = 14000
15000 - 14000 = 1000
1000 / 140 = 7% difference

I assume your example percentage result should have been 7.14% and not 14.7%.

Re: Analog Percent difference

Posted: Thu Oct 06, 2011 1:33 pm
by medelec35
Hi
I’m the first to admit my maths is poor, so I can’t offer an elegant solution.
If it was me I would do this.
((Int1*10-int2*10)*10)/int2


There will be rounding errors e.g. 6 could be displayed instead of 7


Martin

Re: Analog Percent difference

Posted: Thu Oct 06, 2011 7:10 pm
by jgu1
Both Hello!

Imagine having FC and furthermore, such a team. Thanks to both. :D :D
I will test both examples, and correct Benj 7.14% and not 14.7%

Thank´s

Best Regards

Jorgen