First Project - Display name associated with Analog Input

I just created my first ever Arduino project and - GASP! - it appears to work! This is a ham radio widget, and I’d like to write up the project for the local ham club newsletter. So just because it works doesn’t mean it is really well coded or thought-out; maybe I just had beginners luck? Anyway, I’d appreciate feedback on the following before I write up and submit to our club. Thanks in advance for all suggestions!

Project is a LCD display that shows which radio I’ve selected to connect to the station antenna. I’m using a commercial 6-port switch box that is remotely switched by bringing a control line to ground (one control line for each port; non-selected ports are floating). I initially planned to use a single-pole, six-throw rotary switch to control the switch box. The Arduino is for a display only, so I just need the sketch to determine which control line is grounded, and display a text message (Port number or Radio Model) associated with the grounded control line. The switch box has six ports; the Arduino has six Analog I/Os… seems like a happy coincidence to me!

My first thought was to connect each of the analog I/O pins to a position on the rotary switch, so when I ground the control line, I also ground the analog line. Whoops, that didn’t work, because the analog I/O’s don’t like to be left floating. Change of plans: I decided to switch the 5V output from the Arduino to the selected Analog I/O, with a 10K resistor from each Analog I/O to ground so that the I/O’s are not floating.

I have a double-pole, six-throw switch that will work fine for this project; I use one pole to ground the selected control line, and one pole to put 5 V to the desired Analog I/O. I was surprised to find that I only need the 10K resistor on the Analog0 I/O - all the other Analog I/O’s are floating, but aren’t causing problems. So my first question is: Should I still put a resistor to ground on all the Analog I/O’s?

For the sketch, I used nested If/Else statements. The 250 msec delays are to minimize flickering when I’m rotating the switch through the six positions. So, here’s the code; any suggested improvements? Is there a smarter way to do this?

/*
 Antenna Switch Display

 The circuit:
* A0 = Port 1
* A1 = Port 2
* A2 = Port 3
* A3 = Port 4
* A4 = Port 5
* A5 = Port 6
*/

// include the library code:
#include <LiquidCrystal.h>

// initialize the library by associating  LCD interface pins
// with the appropriate arduino pins

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int analogPin1 = 0;    // Port 1
int analogPin2 = 1;    // Port 2
int analogPin3 = 2;    // Port 3
int analogPin4 = 3;    // Port 4
int analogPin5 = 4;    // Port 5
int analogPin6 = 5;    // Port 6

int val = 0;

void setup() {
 // set up the LCD's number of columns and rows:
 lcd.begin(16, 2);
  lcd.print("Radio Selected");
}

void loop() 
{
 lcd.setCursor(0, 1);                   // set the cursor to column 0, line 1
 
val = analogRead(analogPin1);     // read the input pin 1

if (val > 1000)                            // 5 V = 1023
{
 lcd.print ("Port 1");
 delay (250);
}
 else 
 {
 val = analogRead(analogPin2);     // read the input pin 2
 if (val > 1000)
 {
 lcd.print ("Port 2");
 delay (250);
 }
   else
   {
   val = analogRead(analogPin3);     // read the input pin 3
   if (val > 1000)
   {
   lcd.print ("Port 3");
   delay (250);
   }
     else
     {val = analogRead(analogPin4);     // read the input pin 4
     if (val > 1000)
     {
     lcd.print ("Port 4");
     delay (250);
     }
       else
       {val = analogRead(analogPin5);     // read the input pin 5
       if (val > 1000)
       {
       lcd.print ("Port 5");
       delay (250);
       }
         else
         {val = analogRead(analogPin6);     // read the input pin 6
         if (val > 1000)
         {
         lcd.print ("Port 6");
         delay (250);
         }
           else
           {
           lcd.print ("       ");  // Blanks the screen when switch is moving to a new position
           delay (250);
           }
         }
       }
     } 
    }
   }
}

Thanks again for all suggestions and comments.
David

Moderator added code tags

So your signal is either 5V or 0V. That's a digital signal. Why are you using analogRead to read it? You can use digitalRead on those same pins and then you only have to deal with LOW or HIGH.

You can greatly simplify this program by using an array instead of six values.

Also better to use byte for values <256 instead of int, it saves memory.

Connect the mid point of your rotary dial to GND, then enable the internal pull-ups and no need for external resistors. You should never let a pin float - it may seem to work but it’s not going to be stable.

To prevent flicker and have fast reactions, only refresh the screen upon a change of the input, rather than every time. Also you can overwrite the original text instead of first printing spaces and then the new value. That also saves on flicker.

For publication, also make sure your formatting looks good. For starters, the indentation.

So basically your program reduces to:

#include <LiquidCrystal.h>

// initialize the library by associating  LCD interface pins
// with the appropriate arduino pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte lastState = 0;
byte inputPin[] = {A0, A1, A2, A3, A4, A5};

void setup() {
  lcd.begin(16, 2);
  lcd.print("Radio Selected");  
  for (byte i = 0; i < 6; i++) {
    pinMode(inputPin[i], INPUT_PULLUP);
  }
}

void loop() {
  for (byte i = 0; i < 6; i++) {
    if (digitalRead(inputPin[i]) == LOW) {
      if (i != lastState) {
        lastState = i;
        lcd.setCursor(0,1);
        lcd.print(F("Port "));
        lcd.print(i+1);
      }
    }
  }
}

Note: there may be typos in there. Didn’t test it.

Delta_G and wvmarle - Thank you for your suggestions. I set up the analog I/O as digital ports and used the internal pull-up resistors. Simplified the project, and it works perfectly. Your suggestions helped!