reading voltage and if statements

hummm.... compiles okay, but get a 'bad reading' and no antenna ports selected as i change the input voltages.

the bottom LCD prints out fine. The "DO NOT tramit' works when pin 4 is low and my voltage test works when pin 4 is low

but thats about it

I've been thinking in an analog world.

what if?

Is is possible to do away with reading voltages and using a rotary encoder to simply select the antenna I want, then write to a pin and let a optocoupler and relay do the heavy lifting?

I dont have a clue if that is even possible let alone know how to do it

mike

let me fire up a board and get some serial prints going to see whats wrong its probably a mistake I have made.

don't see a problem. Run this for me and tell me what it says. It should say bad then the numbers that are being used

theres some code in there that by passes the switch and other junk just ignore that for now

/*/remote switch display
  vers 3.2  26 August 2017
  Voltage read on A1 from antenna remote
  Fault alarm pin 4
  Test switch on pin 9
*/
// Voltage read on A1 from antenna remote
//
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(8,9,10,11,12,13); DONT NEED THIS NOW WITH I2C LCD


#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#define I2C_ADDR 0x27 // Define I2C Address for controller
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define BACKLIGHT 3
#define fault 4       //THIS IS THE PIN THAT LOOKS FOR THE fault LED from switch controller. H = ok L = fault
#define test 9       // switch to turn on voltage readings for setting display

LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

int input2;
int input ;
int antenna;
int radios;
int testPin = 9;     //digital pin 9 for switch to show volts on display
int antennaNumber = 0;

void setup() {
  lcd.setBacklightPin(3, POSITIVE);
  lcd.begin(16, 2);
  // Serial.begin(9600);
  lcd.clear();                        //clears any junk on the screen
  // delay(2000);                        //wait 2 seconds
  lcd.print(" Antenna Switch");       //a quick splash
  // delay(4000);                      //take a look for four seconds


  pinMode(4, INPUT_PULLUP); // Fault input  on digital pin 4
  pinMode(testPin, INPUT_PULLUP);  //select for voltage readings on LCD

}

void loop() {
  //input = analogRead(A1);              //this is the input for the antenna relays
  input2 = analogRead(A2);             //input for the transceivers
  //antenna = (input / 129) + 1;
  radios = (input2 / 129) + 1;

  show();
}

//****Functions****

void show() {
  static byte prevtestButton;//done this so i can see a state change to clear lcd
  static byte prevfaultButton;//done this so i can see a state change to clear lcd
  byte testButton = digitalRead(testPin);//done this so i can see a state change to clear lcd
  byte faultButton = digitalRead(fault);//done this so i can see a state change to clear lcd

  if (prevtestButton != testButton || prevfaultButton != faultButton) {
    lcd.clear();//got to work out how to deal with this to stop screen flicker
  }
  prevtestButton = testButton;
  prevfaultButton = faultButton;
  byte testing = 1;
  //if (testButton == LOW) { //when pin is low, the Display will show AD voltage on LCD
  if (testing == 1) {
    lcd.setCursor(0, 0);                   //returns cursor to first line
    lcd.print("#");                           //only way I know of to get both the voltage and antenna port on the LCD
    input = analogRead(A1);   //wake pin
    // int serialtest= 995;
    // antennaNumber = map(serialtest, 0, 1024, 1, 9);//read pin
    // Serial.print("antennaNumber ");
    //  Serial.println(antennaNumber);
    antennaNumber = map(analogRead(A1), 0, 1024, 1, 9);//read pin
    lcd.print(antennaNumber); //was to put these IF statements here or the line blinked.
    printAntenna(antennaNumber);
    lcd.setCursor(0, 1);
    lcd.print(input / 204.6);
    lcd.setCursor(5, 1);
    lcd.print(" volts input");
    //  delay(500);                               // reduce flicker
  } else if (faultButton  == LOW) { //Fault and test both active?

    //this tests to see if the antenna switch has a fault
    lcd.setCursor(0, 0);                   //sets cursor to second line
    lcd.print("SWITCH FAULT! ");           //prints to lcd that something is wrong
    lcd.setCursor(0, 1);
    lcd.print("Do NOT transmit!");
  } else {//test pin and fault pin are high to get here
    //this selects the antenna and displays the results on the LCD

    lcd.setCursor(0, 0);                  //returns cursor to first line
    lcd.print("#");
    lcd.print(antennaNumber);                   //prints out the number of the switch
    printAntenna(antennaNumber);
    //Let's select a radio by reading the voltage on analog pin 2

    lcd.setCursor(0, 1);                  //places this info on the second line

    // lets show what radios are connected to the other switch

    lcd.print("#");
    lcd.print(radios);
    if (radios == 1)lcd.print(F("TenTec OMNI 7  "));//made same lenght to over write
    if (radios == 2)lcd.print(F(" Yaesu FT-450D "));
    if (radios == 3)lcd.print(F(" TenTec Jupiter"));
    if (radios == 4)lcd.print(F(" TenTec Omni V "));
    if (radios == 5)lcd.print(F(" TenTec Omni C "));
    if (radios == 6)lcd.print(F(" Drake TR4CW   "));
    if (radios == 7)lcd.print(F(" Drake TR-5    "));
    if (radios == 8)lcd.print(F(" Yaesu FT-1200 "));
    //   delay(200);//Reduce flicker
  }

}


void printAntenna(int show) {

  switch (show) {
    case 1:
      lcd.print(F(" Vertical    "));//made same lenght to over write
      break;
    case 2:
      lcd.print(F(" 40 Meter DP "));
      break;
    case 3:
      lcd.print(F(" (Yagi)      "));
      break;
    case 4:
      lcd.print(F(" (Loop)      "));
      break;
    case 5:
      lcd.print(F(" (6 meter DP)"));
      break;
    case 6:
      lcd.print(F(" (Parabolic) "));
      break;
    case 7:
      lcd.print(F(" (Beverage)  "));
      //   Serial.println("ok");
      break;
    case 8:
      lcd.print(F(" (Dummy load)"));
      break;
    default:
      lcd.print(F("bad "));
      lcd.print(input);
      lcd.print(" ");
      lcd.print(show);
      break;
  }
}

ok I found what I had screwed up

carn't read a pin with in a "if" then expect a different "if" to read the same pin

sry I was playing in your code so I might have made a mess but this should show that the idea works

/*/remote switch display
  vers 3.2  26 August 2017
  Voltage read on A1 from antenna remote
  Fault alarm pin 4
  Test switch on pin 9
*/
// Voltage read on A1 from antenna remote
//
//#include <LiquidCrystal.h>
//LiquidCrystal lcd(8,9,10,11,12,13); DONT NEED THIS NOW WITH I2C LCD


#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#define I2C_ADDR 0x27 // Define I2C Address for controller
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define BACKLIGHT 3
#define fault 4       //THIS IS THE PIN THAT LOOKS FOR THE fault LED from switch controller. H = ok L = fault
#define test 9       // switch to turn on voltage readings for setting display

LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

int input2;
int input ;
int antenna;
int radios;
int testPin = 9;     //digital pin 9 for switch to show volts on display
int antennaNumber = 0;

void setup() {
  lcd.setBacklightPin(3, POSITIVE);
  lcd.begin(16, 2);
  lcd.clear();                        //clears any junk on the screen
  delay(2000);                        //wait 2 seconds
  lcd.print(" Antenna Switch");       //a quick splash
  delay(4000);                      //take a look for four seconds


  pinMode(4, INPUT_PULLUP); // Fault input  on digital pin 4
  pinMode(testPin, INPUT_PULLUP);  //select for voltage readings on LCD

}

void loop() {
  //input = analogRead(A1);              //this is the input for the antenna relays
  input2 = analogRead(A2);             //input for the transceivers
  //antenna = (input / 129) + 1;
  radios = (input2 / 129) + 1;

  show();
}

//****Functions****

void show() {
  static byte prevtestButton;//done this so i can see a state change to clear lcd
  static byte prevfaultButton;//done this so i can see a state change to clear lcd
  byte testButton = digitalRead(testPin);//done this so i can see a state change to clear lcd
  byte faultButton = digitalRead(fault);//done this so i can see a state change to clear lcd
  input = analogRead(A1);   //wake pin
  antennaNumber = map(analogRead(A1), 0, 1024, 1, 9);//read pin

  if (prevtestButton != testButton || prevfaultButton != faultButton) {
    lcd.clear();//got to work out how to deal with this to stop screen flicker
  }
  prevtestButton = testButton;
  prevfaultButton = faultButton;

  if (testButton == LOW) { //when pin is low, the Display will show AD voltage on LCD
    lcd.setCursor(0, 0);                   //returns cursor to first line
    lcd.print("#");                           //only way I know of to get both the voltage and antenna port on the LCD
    lcd.print(antennaNumber); //was to put these IF statements here or the line blinked.
    printAntenna(antennaNumber);
    lcd.setCursor(0, 1);
    lcd.print(input / 204.6);
    lcd.setCursor(5, 1);
    lcd.print(" volts input");
    // reduce flicker
  } else if (faultButton  == LOW) { //Fault and test both active?

    //this tests to see if the antenna switch has a fault
    lcd.setCursor(0, 0);                   //sets cursor to second line
    lcd.print("SWITCH FAULT! ");           //prints to lcd that something is wrong
    lcd.setCursor(0, 1);
    lcd.print("Do NOT transmit!");
  } else {//test pin and fault pin are high to get here
    //this selects the antenna and displays the results on the LCD

    lcd.setCursor(0, 0);                  //returns cursor to first line
    lcd.print("#");
    lcd.print(antennaNumber);                   //prints out the number of the switch
    printAntenna(antennaNumber);
    //Let's select a radio by reading the voltage on analog pin 2

    lcd.setCursor(0, 1);                  //places this info on the second line

    // lets show what radios are connected to the other switch

    lcd.print("#");
    lcd.print(radios);
    if (radios == 1)lcd.print(F("TenTec OMNI 7  "));//made same lenght to over write
    if (radios == 2)lcd.print(F(" Yaesu FT-450D "));
    if (radios == 3)lcd.print(F(" TenTec Jupiter"));
    if (radios == 4)lcd.print(F(" TenTec Omni V "));
    if (radios == 5)lcd.print(F(" TenTec Omni C "));
    if (radios == 6)lcd.print(F(" Drake TR4CW   "));
    if (radios == 7)lcd.print(F(" Drake TR-5    "));
    if (radios == 8)lcd.print(F(" Yaesu FT-1200 "));
    //   delay(200);//Reduce flicker
  }

}

void printAntenna(int antDisplay) {

  switch (antDisplay) {
    case 1:
      lcd.print(F(" Vertical    "));//made same lenght to over write
      break;
    case 2:
      lcd.print(F(" 40 Meter DP "));
      break;
    case 3:
      lcd.print(F(" (Yagi)      "));
      break;
    case 4:
      lcd.print(F(" (Loop)      "));
      break;
    case 5:
      lcd.print(F(" (6 meter DP)"));
      break;
    case 6:
      lcd.print(F(" (Parabolic) "));
      break;
    case 7:
      lcd.print(F(" (Beverage)  "));
      break;
    case 8:
      lcd.print(F(" (Dummy load)"));
      break;
    default:
      lcd.print(F("bad "));
      lcd.print(input);
      lcd.print(" ");
      lcd.print(antDisplay);
      break;
  }
}