Comparing analog signals

I am trying to build a device that reads an input from a known resistor and compares it to an unknown potentiometer. If the pot's resistance is within 15% +- that of the resistor a green LED will illuminate, but if it is outside that tolerance a red one will illuminate. I've been going back and forth, but I can't get my code to work and I'm unsure why.

const int analogInPin1 = A4;  
const int analogInPin2 = A5;
int potValue = 0;
int resistorValue = 0;
int redLed = 8;
int greenLed = 12;

void setup(){
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(analogInPin1, INPUT);
  pinMode(analogInPin2, INPUT);
}

void loop(){
  potValue= analogRead(A4);
  resistorValue = analogRead(A5);
  

  if(potValue <= resistorValue * 0.9 || potValue >= resistorValue * 1.1) 
    digitalWrite(redLed, HIGH);
  else digitalWrite(redLed, LOW);
  
  if(potValue >= resistorValue * 0.9 || potValue <= resistorValue * 1.1) 
    digitalWrite(greenLed, HIGH);
  else digitalWrite(greenLed, LOW);
  
  }

What doesn't work? I see no attempt to serial.print() the values being read so you know what you are dealing with. Help your self by printing values and results of your compares.
Paul

if(potValue >= resistorValue * 0.9 || potValue <= resistorValue * 1.1)

Think this condition needs to use and rather than or
You can write the and or && for the logical and condition.

if(potValue >= resistorValue * 0.9 and  potValue <= resistorValue * 1.1)

if(potValue >= resistorValue * 0.9 && potValue <= resistorValue * 1.1)

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

I'm not trying to actively monitor the value as I don't need to see the exact value. I just need to know whether the pot is within +-15% of the resistor and if so, illuminate the green led

Updated the code to use 'and' rather than 'or'. No matter the value of the pot the green led illuminates.

const int analogInPin1 = A4;  
const int analogInPin2 = A5;
int potValue = 0;
int resistorValue = 0;
int redLed = 8;
int greenLed = 12;

void setup(){
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(analogInPin1, INPUT);
  pinMode(analogInPin2, INPUT);
}

void loop(){
  potValue= analogRead(A4);
  resistorValue = analogRead(A5);
  

  if(potValue <= resistorValue * 0.9 && potValue >= resistorValue * 1.1) 
    digitalWrite(redLed, HIGH);
  else digitalWrite(redLed, LOW);
  
  if(potValue >= resistorValue * 0.9 && potValue <= resistorValue * 1.1) 
    digitalWrite(greenLed, HIGH);
  else digitalWrite(greenLed, LOW);
  
  }

@aidansturn are you deliberately ignoring the advice to use code tags when posting code ?

I didn't see that reply yet, is this what you mean?

//If the pot is out of tolerance, illuminate red led
  if(potValue <= resistorValue * 0.9 && potValue >= resistorValue * 1.1) 
    digitalWrite(redLed, HIGH);
  else digitalWrite(redLed, LOW);
  
//If the pot is within tolerance, illuminate green led  
  if(potValue >= resistorValue * 0.9 && potValue <= resistorValue * 1.1) 
    digitalWrite(greenLed, HIGH);
  else digitalWrite(greenLed, LOW);
  
  }

No. I mean use code tags when posting code as described in the topic I linked to, like this

const int analogInPin1 = A4;
const int analogInPin2 = A5;
int potValue = 0;
int resistorValue = 0;
int redLed = 8;
int greenLed = 12;

void setup()
{
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(analogInPin1, INPUT);
  pinMode(analogInPin2, INPUT);
}

void loop()
{
  potValue = analogRead(A4);
  resistorValue = analogRead(A5);
  if (potValue <= resistorValue * 0.9 && potValue >= resistorValue * 1.1)
    digitalWrite(redLed, HIGH);
  else digitalWrite(redLed, LOW);
  if (potValue >= resistorValue * 0.9 && potValue <= resistorValue * 1.1)
    digitalWrite(greenLed, HIGH);
  else digitalWrite(greenLed, LOW);
}

Note that I have also Auto formatted the code as well as posting it inside code tags. See how much easier it is to read and copy for examination

Oh I had no idea that was a feature, I'm pretty new to this

Click the icon that looks like </>.

const int analogInPin1 = A4;
const int analogInPin2 = A5;
int potValue = 0;
int resistorValue = 0;
int redLed = 8;
int greenLed = 12;

A couple of hints - Use the Serial Monitor to "print" the analog values (like the Analog Read Serial example).

And maybe show us your schematic. The Arduino can't directly read a resistor, it can only read voltage. With a pot connected like the Analog Read Serial example, the pot becomes a (variable)
Voltage Divider. You'll need an additional known resistor in series with your unknown resistor to make another voltage divider.

P.S.
Since a voltage divider gives you a voltage proportional to the resistance ratio, the percentage voltage change isn't the same as the percentage resistance change... You can work that out from your voltage divider calculations...

"resistance of the pot" - but on which terminals? The fixed resistance, or wiper resistance?

Fixed resistance

What range of resistances do you need to cover?

You only need to use the "and" condition for the in spec comparison when both things need to be true. The "or" is correct for the out of range when only one thing needs to be true.

if(potValue <= resistorValue * 0.9  ||  potValue >= resistorValue * 1.1) 
    digitalWrite(redLed, HIGH);
  else digitalWrite(redLed, LOW);
  
  if(potValue >= resistorValue * 0.9 && potValue <= resistorValue * 1.1) 
    digitalWrite(greenLed, HIGH);
  else digitalWrite(greenLed, LOW);

All the other comments about printing the analogRead() values and working with the resistor network are very relevant. Having the correct comparison code will only work if you correct values.

(to OP) Yes, reading the reference resistor is a suspicious bit of code. It suggests that your circuit is not designed properly.

I can't understand why noone done the obvious thing and eliminated one of the if's - they are the same condition, with opposite sense, so you only need to calculate one of them.

  bool different = potValue <= resistorValue * 0.9  ||  potValue >= resistorValue * 1.1 ;
  digitalWrite (redLed, different) ;
  digitalWrite (greenLed, ! different) ;

Simple, no chance of introducing a bug by getting the two conditions not-quite opposite (for instance cattledog's answer still has a bug since <= is not the opposite of >=)

1 Like