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.
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
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
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);
}
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...
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.
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.
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.
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 >=)
I am just revisiting this project. I should clarify that I am by no means qualified for this type of project as I can hardly code but this could be a valuable tool for some people at my job. Basically, what I am looking to accomplish is to have a series of known resistors (1k, 5k, 10k, etc) that I could swap into a breadboard as a my known resistance. I would then set up a voltage divider (correct me if I am wrong) to compare the total resistance of a pot and signal a red or a green LED depending on the pot value (+/- 15% of the resistors value).
Please draw and post a schematic of your experiment here.
We suggest printing the values as a check on the plausibility of your logic doing what you want. Have you literally not looked at what your analogRead statements are returning?