# If between function

Hi,

I have two potentiometers that have their values displayed as a percentage through an LCD screen.

Further in my project one potentiometer will display position of a linear actuator and the other potentiometer will be used to move the arm.

so I create a function saying that if the value of the actuator is less than that of the other pot then the arms needs to move forward and vice versa if the value of the arm is greater than that of pot.

At the moment I am trying to make a set of rules saying that if there is a difference of 3% in the values, then a red LED needs to flash and if less than 3% flash a green LED. This will be used to show if there is a calibration issue between the pot and the linear actuator.

to do this I have create a selection of if rules saying the following:

if ((PercentageActRound - PercentageInRound) > 3.01){
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
}

if ((PercentageActRound - PercentageInRound) < -3.01){
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
}

if ((PercentageInRound - PercentageActRound) > -3.01) {
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
}

if ((PercentageInRound - PercentageActRound) < -3.01); {
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);

However this is not giving the results I would like.

Is there a function that I can use to say if the two values are different by a certain amount to turn the red LED on and if they are between that value to turn the green LED on?

I hope this makes sense & I hope somebody can help

Last if has a stray ; after the conditional. That means the semicolon (ie, nothing) is done when the condition is true, then the following block is executed regardless of whether the condition is true.

You can combine multiple conditions using boolean operators. See && - Arduino Reference

Ex (note - this doesn't do what you want, just concept example)

if (((PercentageInRound - PercentageActRound) > 3 ) && (PercentageInRound - PercentageActRound) < 4) {
doStuffHere();
}

Be sure to know the difference between && and ||, the boolean operators and & and |, the bitwise ones.

Try:

``````if (abs(PercentageActRound - PercentageInRound) > 3.01){
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
}
else{
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, HIGH);
}
``````

https://www.arduino.cc/en/Reference/Abs

Further in my project one potentiometer will display position of a linear actuator

How are you going to display the position of a linear actuator on a potentiometer?

outsider:
Try:

``````if (abs(PercentageActRound - PercentageInRound) > 3.01){
``````

}

``````

https://www.arduino.cc/en/Reference/Abs
``````

Or `fabs()`, which I belive is a library function rather than a macro.

Not sure why the values have to be rounded if the op is working in floating point.

All if statements turn on the re

lparry92:
Hi,
...
to do this I have create a selection of if rules saying the following:
...
However this is not giving the results I would like.

If the result is that you always get the red led, it is because all your statements write RedLED HIGH and GreenLED LOW.

Using 3.01 looks wrong, but of course we don't know as we don't have the full code. If you divide two values and the result is <1.03 and >0.97, they are within 3 percent.

if (quotient < 1.03 && quotient > 0.97){
digitalWrite(RedLED, LOW);
digitalWrite(GreenLED, HIGH);
}else{
digitalWrite(RedLED, HIGH);
digitalWrite(GreenLED, LOW);
}

To avoid float calculation I think you can use

quotient = 100* readValue / requestValue;
if (quotient < 103 && quotient > 97){
...

There are true experts here that can correct my last assumption if I am wrong.

Thanks guys.

The ABS function suggested by OUTSIDER worked perfect!!!

Just out of curiosity. How would I put in a function to say that if the values are greater than 3.01% for more than 3 secs, to then have the red LED turn on?

I tried IF and then FOR but I couldn't get that to work

How would I put in a function to say that if the values are greater than 3.01% for more than 3 secs, to then have the red LED turn on?

If what values?

On any given pass through loop(), you need to determine if some value has exceeded a threshold. If it has, record the time.

On any given pass through loop(), you need to determine if that value has dropped below the threshold. If it has, clear the time variable.

On any given pass through loop(), you need to see if the recorded time is more than three seconds old, if the recorded time is not 0. It is is, turn the LED on.

Under what condition do you then want to turn the red LED off?

Hi,

I want the red LED to turn on if the difference of two pot values have a difference in number greater than 3.01 after 3 seconds.

When the values are similar (within a range of 3) then i want a green LED to come on.

Later in my project one pot will be an input for a linear actuator to move forwards and backwards and another pot will be a feedback of the true position of the arm.

I want to use the red LED to show that that there is a problem with the system, however as the arm will be moving to adjust to the input value there needs to be a time delay to allow the values to be different while the arm is moving.

I hope this helps.

I hope this helps.

Doesn't help me. I already told you what you need to do. YOU need to actually write the code.

I found what you were saying about pass through loops quite confusing and also you asked questions so I just tried to answer them an be appreciative of your help.

I found what you were saying about pass through loops quite confusing and also you asked questions so I just tried to answer them an be appreciative of your help.

loop() gets called over and over. Each time it gets called is a "pass through the loop() function".

void loop()
{

// Compare it(them) to the previous value(s)

// If the value (or difference) exceeded some threshold, record when

// If a change happened (there is a recorded when), and now minus then is
// greater than 3 seconds, do whatever needs doing
}

Each comment needs to be replaced with some code, or, better yet, a function call. In the function, do what the comment says.

Each comment/block of code is at the same level, so each happens independently and every time through loop.