Reducing number of input pins with lcd keypad shield

Hey all, I am having a bit of a hard time wrapping my head around replacing code for individual input_pullup switches with switches being used via ADC

I'm using a 1602 LCD keypad shield with an Arduino Mega2560

previously I had written the code for the inputs as



  pinMode(Mode1Pin, INPUT_PULLUP);
  pinMode(Mode2Pin, INPUT_PULLUP);
  pinMode(Mode3Pin, INPUT_PULLUP);
  pinMode(Mode4Pin, INPUT_PULLUP);
  pinMode(Mode5Pin, INPUT_PULLUP);

const int  Mode1Pin = 9;                    // Menu next   
const int  Mode2Pin = 10;                   // Menu Prev      
const int  Mode3Pin = 11;                   // Menu select
const int  Mode4Pin = 12;                   // Retard button
const int  Mode5Pin = 13;                   // Boost button

void   checkrunmode(){
  int m1;
  int m2;
  int m3;
  int m4;
  int m5;
    
  m1 = digitalRead(Mode1Pin);
  m2 = digitalRead(Mode2Pin);
  m3 = digitalRead(Mode3Pin);
  m4 = digitalRead(Mode4Pin);
  m5 = digitalRead(Mode5Pin);

  // Reset ModeChangeDelay status if all swicthes are off (debounce switch)
  if ((m1 != LOW) && (m2 != LOW) && (m3 != LOW)) {
     ModeChangeDelay = false;
  }

  if (!ModeChangeDelay) {
    // switch 1 pushed - Next menu
    if ((m1 == LOW)) {
      if (MenuChangeDelay >= millis()) {
        MenuSelect = MenuSelect + 1;
        CharDelay = millis() + CharDelayTime;
        if(MenuSelect > MaxMenuSelect) {
          MenuSelect = 1;
        }
      }else{
        MenuCount = 0;       
      }
      MenuCount += 1;
      MenuChangeDelay = millis() + MenuChangeDelayTime;
      ModeChangeDelay = true;
      if (MenuCount > 1) {                              // a bit of code to avoid sending unneeded chars to LCD 
        MenuChar = 6;
      }else{
        MenuChar = 0;
      }
      MenuCharOld = 0;
    }
    
    // switch 2 pushed - Prev menu
    if ((m2 == LOW)) {
      if (MenuChangeDelay >= millis()) {
        MenuSelect = MenuSelect - 1;
        CharDelay = millis() + CharDelayTime;
        if(MenuSelect < 1) {
          MenuSelect = MaxMenuSelect;
        }
      }else{
        MenuCount = 0;       
      }
      MenuCount += 1;
      MenuChangeDelay = millis() + MenuChangeDelayTime;
      ModeChangeDelay = true;
      if (MenuCount > 1) {
        MenuChar = 6;
      }else{
        MenuChar = 0;
      }
      MenuCharOld = 0;
    }
    
    // switch 3 pushed - Select
    if ((m3 == LOW)) {
      if (MenuChangeDelay >= millis()) {
        DoMenuSelect = true;
        MenuChangeDelay = 0;
        ModeChangeDelay = true;
      }else{
        MenuChangeDelay = millis() + MenuChangeDelayTime;
        ModeChangeDelay = true;
        MenuCount = 0;       
      }
      MenuChar = 0;
      MenuCharOld = 0;
    }
    
    // switch 4 pushed - Boost
    if ((m4 == LOW)) {
      ModeChangeDelay = true;
      MenuChangeDelay = 0;
      UseRetard = true;
      DelayRdn = ! DelayRdn; // toggle delay 
    }
    
    // switch 5 pushed - Idle
    if ((m5 == LOW)) {
      ModeChangeDelay = true;
      MenuChangeDelay = 0;
      UseBoost = true;
   }
  }
}

I have read example code showing how to read the values from the ADC but it's just not clicking on how to adjust my code for the analog switches

Eg.

int x;
 x = analogRead (0);
 lcd.setCursor(10,1);
 if (x < 60) {
   lcd.print ("Right ");
 }
 else if (x < 200) {
   lcd.print ("Up    ");
 }
 else if (x < 400){
   lcd.print ("Down  ");
 }
 else if (x < 600){
   lcd.print ("Left  ");
 }
 else if (x < 800){
   LCD.print ("Select");

Something like

// reset my variables
m1 = my = m3 = m4 = m5 = HIGH;


int x;
 x = analogRead (0);
 if (x < 60) {
   m1 = LOW;
 }
 else if (x < 200) {
   m2 = LOW;
 }
 else if (x < 400){
   m3 = LOW;
 }
 else if (x < 600){
   m4 = LOW;
 }
 else if (x < 800){
   m5 = LOW;
  }

Now your code can keep on working with the m variables, just remove the associated digitalReads.

1 Like

Getting the error "x" does not name a type for the line

 x = analogRead (0);

I'm a bit confused since the line above declares x as an integer

Is that with the code in the opening post or with a different / updated code? In the latter case, please post the new code.

Your code specifically

analogRead puts out an integer (on e.g. a Nano or Uno a value of 0-1023; i.e. 10 bits resolution). So x needs to be an int. @sterretje's code then evaluates that integer and sets any of your m-variables to LOW as would have happened in your original code.

Now I'm also confused: why were m1, m2...etc integers?

I get that analogRead outputs an integer, but the compiler states: "x" does not name a type
even though the code establishes x as an integer

I previously had 5 input switches that I wanted to change over to push buttons through the 1602 LCD Keypad shield which uses an ADC

Ok, well, hard to tell because I don't see a version of the complete code anywhere as you're trying to compile it. It would help to post that.

Yeah I know.

Sorry, just trying to be clear with my explanation

I guess I don't understand what the other code would have to do with the error that there is not a type declared for x when it's done the line right before?

Like I said, show the code as you try to compile it.

As I asked before, is that with the code in the opening post or with a different / updated code (e.g. integrating the suggestion in post #2)? In the latter case, please post the new code.

FYI, there might be another error that eventually results in the error that the variable does not exist.