 # Percent of constant changing total value

Hello!

I have been working on a code for some time, and everything seems to be working, ecxept the part where my LED's should turn on.

I have 3 sensors that each has an LED.

What i want is having a LED turn on, when a specific sensor is 10% or over, the total value.

For example

Sensor1 = 10 Sensor2= 3 Sensor3= 30

Now total value is 43.

If Sensor1 gets over 20 of total value, LED1 should turn on.

Sensor1 is 23%.

Sensor1 = 10 Sensor2= 3 Sensor3= 40

Now Sensor1 is 19%. and its LED should turn off..

I have tried some simple

sensor1/(sensor1+Sensor2+senor3)*100 > 20, digitalWrite (led1, high.)

but what it does is turning on when sensor1 is above 20 reading. not 20% What am i doing wrong?

Holse: I have tried some simple

sensor1/(sensor1+Sensor2+senor3)*100 > 20, digitalWrite (led1, high.)

Well, that isn't code, so you should post the actual code you're trying.

Integer arithmetic in C is not arithmetic, its integer arithmetic. 1/2 = 0 (ie division truncates to an integer result.

Perhaps you should try sensor1 * 100 / (sensor1+sensor2+sensor3) > 20

First of all I would do the calculations in stages as that makes debugging much easier

sensorTotal = sensor1 + sensor2 + sensor3

Then it is probably easier, with integer maths to invert the calculation - divide the big number by the smaller number

sensor1Part = sensorTotal / sensor1

Then you can do a test such as

if (sensor1Part <= 10) { // (anything over 10% will give a result less than 10)

…R

Thank you Robin2 and MarkT

I did as you for the easier debugging Robin2, and the calculations from MarkT, and now i am getting the results i want.

Thank you very much! ++