Arduino Ohm meter issue

Hello,

I'm working on an Arduino based Ohm meter for a little instrument panel. I followed a tutorial some time back that gave a design like this:

This is the code:

//the LCD display
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);

//for timing purposes to avoid delay()
unsigned long timer;


//resistor meter
int resistorPin = A0;
float R1 = 0;
float R2 = 0;
int v = 0;
float vin = 5; //it actually is!
float vout = 0;
float buffer = 0;
int voltOn = 2;

void setup(){  
  Serial.begin(9600);
  pinMode(12, INPUT);
  pinMode(11, INPUT);
  pinMode(10, INPUT);
  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);
  pinMode(voltOn, OUTPUT);
  
  pinMode(resistorPin, INPUT);

  //Use external voltage reference
  //analogReference(EXTERNAL); //Didn't work as well as NOT using it

  lcd.init(); // initialize the lcd 
  lcd.backlight();

  
  
}

void loop(){

  if (millis() - timer >= 1000UL){
    timer = millis();

    digitalWrite(voltOn, HIGH);
    //start searching with 470000
  
    //470000 (this worked better that 1M)
    R1 = 470000;
    pinMode(12, INPUT);
    pinMode(11, INPUT);
    pinMode(10, INPUT);
    pinMode(9, OUTPUT);
    digitalWrite(9, LOW);
    v = analogRead(resistorPin);
    buffer = v * vin;
    vout = (buffer)/1023;
    R2 = (vout*R1)/(vin-vout);      
    
    if (R2 > 100000 && R2 < 2000000){
      if (R2 > 1000000){
        R2 = R2/1000000;
        clearLine(1);
        lcd.print(R2);
        lcd.print("M");
        lcd.print(char(244));
      }
      else{
        R2 = R2/1000;
        clearLine(1);
        lcd.print(R2);
        lcd.print("K");
        lcd.print(char(244));
      }
      return;
    }
    else{
      //100000
      R1 = 100000;
      pinMode(12, INPUT);
      pinMode(11, INPUT);
      pinMode(10, OUTPUT);
      pinMode(9, INPUT);
      digitalWrite(10, LOW);
      v = analogRead(resistorPin);
      buffer = v * vin;
      vout = (buffer)/1023;
      R2 = (vout*R1)/(vin-vout);      
      
      if (R2 > 10000 && R2 < 100000){
        R2 = R2/1000;
        clearLine(1);
        lcd.print(R2);
        lcd.print("K");
        lcd.print(char(244));
        return;
      }
      else{
        //10000
        R1 = 10000;
        pinMode(12, INPUT);
        pinMode(11, OUTPUT);
        pinMode(10, INPUT);
        pinMode(9, INPUT);
        digitalWrite(11, LOW);
        v = analogRead(resistorPin);
        buffer = v * vin;
        vout = (buffer)/1023;
        R2 = (vout*R1)/(vin-vout);      
        
        if (R2 > 1000 && R2 < 10000){
          R2 = R2/1000;
          clearLine(1);
          lcd.print(R2);
          lcd.print("K");
          lcd.print(char(244));
          return;
        }
        else{
          //1000
          R1 = 1000;
          pinMode(12, OUTPUT);
          pinMode(11, INPUT);
          pinMode(10, INPUT);
          pinMode(9, INPUT);
          digitalWrite(12, LOW);
          v = analogRead(resistorPin);
          buffer = v * vin;
          vout = (buffer)/1023;
          R2 = (vout*R1)/(vin-vout);      
          
          if (R2 > 0 && R2 < 1000){
            if (R2 > 1000){
              R2 = R2/1000;
              clearLine(1);
              lcd.print(R2);
              lcd.print("K");
              lcd.print(char(244));
            }
            else{
              clearLine(1);
              lcd.print(R2);
              lcd.print(char(244));
              return;
            }
          }
          else{
            clearLine(1);
            lcd.print("Insert resistor");
            return;
          }
        }
      }
    }
  }
}

void clearLine(int line){               
  lcd.setCursor(0,line);
  lcd.print("                    ");
  lcd.setCursor(0,line);
}

The only way this circuit seems to work is by alternately setting the correct known resistors to HIGH (and the unused to INPUT) and replacing D2 with ground. I can't replace D2 with ground as The terminal D2 is wired to will be using other connections later that are planned but not yet implemented. I figured I could turn off D2 by setting it to INPUT. Unfortunately The way it stands above it isn't even close to accurate. As I said, replacing D2 with a ground connecting during testing does give extremely close results but I was hoping I wouldn't have to do that. Any thoughts? Is there any substance to this design?

Why? Why can't you use another pin? Why not just connect it to ground?

As mentioned above, that would keep that terminal from being used by any other digital pin and I want the terminal to be used for different functions.

In short, I guess my question is can two pins share the same column on a breadboard or the same terminal jack? Is there a way to "turn off" one pin so the column can be used by the other pin?

Yes, you make one of them an input. If two pins are connected together and they are both outputs, then if both are high or both are low, then there will be no damage, but if one is high and the other is low, there will be a damaging short circuit.

Now, will you please answer my questions?

What repetitive code.

To test a resistor you need to
set one line as an output, and HIGH to provide a voltage source
set all others as INPUT (ie open circuit)
read the voltage on A0.

You can easily do that using the I/O registers
PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

DDRB - The Port B Data Direction Register 

if the bit for pin 8 has the 0 value that means the pin 8 is an input pin,
if it has 1 value is an output pin.
PORTB - The Port B Data Register - read/write
PINB - The Port B Input Pins Register - read only

So for example PORTB = PORTB || 0B00001111 sets pins 8,9,10,11 HIGH and
DDRB = DDRB && 0B11110001 sets pin 8 as output and 9,10,11 as inputs

Hi,

What is the restriction, D2 is going to have more than one use, why not use another input(s) for the other uses?

Tom... :grinning: :+1: :coffee: :australia: