arduino ohm meter

Morning Guys

I am new to Arduino. I have written the following code for Arduino ohm meter. Code is simply driving a potential divider circuit measuring four resistors (R1, R2, R3, R4):

+5V..........R2..........220ohm........GND
+5V..........R3..........220ohm........GND
+5V..........R4..........220ohm........GND
+5V..........R5..........220ohm........GND

A push button is used to start the test. Green LED (gLed) and Red LED (redLed) represent PASS or FAIL.

I only want green LED to turn on when resistance test passes, and Red LED when test fails.

For some reason Red LED keeps blinking even when the test is not in progress (push button not ON)

I have made several changes in the code to rectify the problem, but all in vain.

Can you please have a look to see where problem lie. I am using Arduino UNO board btw.

int buttonInput = 0;
int buttonState1 = 1;
int buttonState2 = 1;
int selector = 1;
int selState = 0;
//int selector2 = 2;
//int selState2 = 0;
int analogPin0 = 0;
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int redLed = 12;
int gLed = 13;
int raw1 = 0;
int raw2 = 0;
int raw3 = 0;
int raw4 = 0;
int Vin = 4.950;
float Vout = 0;
float Vout1 = 0;
float Vout2 = 0;
float Vout3 = 0;
float R1 = 218;
float R2 = 0;
float R3 = 0;
float R4 = 0;
float R5 = 0;

float buffer = 0;

void setup() {
pinMode(redLed, OUTPUT);
pinMode(gLed, OUTPUT);
pinMode(buttonInput, INPUT);
// digitalWrite(buttonInput, HIGH);
pinMode(selector, INPUT);

}

void loop() {
buttonState1 = digitalRead(buttonInput);
delay(10);
buttonState2 = digitalRead(buttonInput);

if (buttonState1 == buttonState2 == HIGH){
raw1 = analogRead(analogPin0);
buffer = raw1 * Vin;
Vout = (buffer) / 1024.0;
buffer = (Vin / Vout) - 1;
R2 = R1 * buffer;

raw2 = analogRead(analogPin1);
buffer = raw2 * Vin;
Vout1 = (buffer) / 1024.0;
buffer = (Vin / Vout1) - 1;
R3 = R1 * buffer;

raw3 = analogRead(analogPin2);
buffer = raw3 * Vin;
Vout2 = (buffer) / 1024.0;
buffer = (Vin / Vout2) - 1;
R4 = R1 * buffer;

raw4 = analogRead(analogPin3);
buffer = raw4 * Vin;
Vout3 = (buffer) / 1024.0;
buffer = (Vin / Vout3) - 1;
R5 = R1 * buffer;

//delay(1000);
selState = digitalRead(selector);
if (selState == HIGH) {
if (R2 < 1 || R3 < 1 || R4 < 1 || R5 < 1) {
digitalWrite(gLed, HIGH);
delay(1000);
digitalWrite(gLed, LOW);
}

else if (selState = LOW);
//selState2 = digitalRead(selector2);
{
if (R2 < 1 || R3 < 1 || R4 < 1) {
digitalWrite(gLed, HIGH);
delay(1000);
digitalWrite(gLed, LOW);
}
else {

digitalWrite(redLed, HIGH);
delay(1000);
digitalWrite(redLed, LOW);
}
}

}

}

}

 if (selState = LOW);

Double oops.

Please use code tags when posting code.

Sorry I forgot to mention that Selector is used to test either 3 or 4 resistors.

If selector = high test 4 resistors
if not then test 3 resistors

OK, when you've fixed those two things, retest and if you've still got problems, post your revised code, but please use code tags

Sorry, I forgot to add; if a digitalRead doesn't return HIGH, there's really no need to test to see if it returned LOW.

I added that to help prevent the momentary tactile push button debounce issue. Hope that makes sense.

I have amended the code and retested it. Problem still persists (Red LED constantly blinking).

int buttonInput = 0;
int buttonState1 = 1;
int buttonState2 = 1;
int selector = 1;
int selState = 0;
int analogPin0 = 0;
int analogPin1 = 1;
int analogPin2 = 2;
int analogPin3 = 3;
int redLed = 12;
int gLed = 13;
int raw1 = 0;
int raw2 = 0;
int raw3 = 0;
int raw4 = 0;
int Vin = 4.950;
float Vout = 0;
float Vout1 = 0;
float Vout2 = 0;
float Vout3 = 0;
float R1 = 218;
float R2 = 0;
float R3 = 0;
float R4 = 0;
float R5 = 0;

float buffer = 0;

void setup() {
  pinMode(redLed, OUTPUT);
  pinMode(gLed, OUTPUT);
  pinMode(buttonInput, INPUT);
  pinMode(selector, INPUT);

}

void loop() {
          buttonState1 = digitalRead(buttonInput);
        delay(10);
          buttonState2 = digitalRead(buttonInput);
   
 if (buttonState1 == buttonState2 == HIGH){
      raw1 = analogRead(analogPin0);
      buffer = raw1 * Vin;
      Vout = (buffer) / 1024.0;
      buffer = (Vin / Vout) - 1;
      R2 = R1 * buffer;
   
      raw2 = analogRead(analogPin1);
      buffer = raw2 * Vin;
      Vout1 = (buffer) / 1024.0;
      buffer = (Vin / Vout1) - 1;
      R3 = R1 * buffer;
  
      raw3 = analogRead(analogPin2);
      buffer = raw3 * Vin;
      Vout2 = (buffer) / 1024.0;
      buffer = (Vin / Vout2) - 1;
      R4 = R1 * buffer;
 
      raw4 = analogRead(analogPin3);
      buffer = raw4 * Vin;
      Vout3 = (buffer) / 1024.0;
      buffer = (Vin / Vout3) - 1;
      R5 = R1 * buffer;


   selState = digitalRead(selector);
   if (selState == HIGH) {
      if (R2 < 1 || R3 < 1 || R4 < 1 || R5 < 1) {
        digitalWrite(gLed, HIGH);
        delay(1000);
        digitalWrite(gLed, LOW);
      }
        else {

        digitalWrite(redLed, HIGH);
        delay(1000);
        digitalWrite(redLed, LOW);
      }
    
      if (selState == LOW);
       {
        if (R2 < 1 || R3 < 1 || R4 < 1) {
        digitalWrite(gLed, HIGH);
        delay(1000);
        digitalWrite(gLed, LOW);
      }
      else {

        digitalWrite(redLed, HIGH);
        delay(1000);
        digitalWrite(redLed, LOW);
      }
       }
    
    }
  
      
    }
   
}
  if (buttonState1 == buttonState2 == HIGH){

If buttonState1 is LOW and buttonState2 is LOW . . . ?

In that case "do nothing" just wait until (buttonState1 == buttonState2 == HIGH) becomes true.

I can't figure out how to incorporate that in the code.

aabir786:
In that case "do nothing" just wait until (buttonState1 == buttonState2 == HIGH) becomes true.

I can't figure out how to incorporate that in the code.

It may surprise you but LOW == LOW == HIGH is true.
(as is HIGH==HIGH==HIGH)
Maybe you missed the && operator?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
What actually are you trying to achieve, measuring 4 resistors and getting a PASS or FAIL?

Thanks.. Tom.. :slight_smile:

Well spotted. "&&" fixed the problem. Thank you for your help.