Analog Percent difference

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times

Analog Percent difference

Post 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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Analog Percent difference

Post 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%.

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: Analog Percent difference

Post 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
Martin

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times

Re: Analog Percent difference

Post 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

Post Reply