Need help with toggle switch

hey there I have a particular issue I have a Arduino Uno and a lcd shield connected over it on top.
Now the lcd works and with it came 6 buttons. 5 buttons are connected to the A0 pin. I believe these buttons have different resistance values which represent each button.

right key <60
up key <200
down key <400
left key <600
select key <800

the lcd shield I'm using is the popular one HiLetgo 1602 LCD Keypad Shield 1602 LCD Expansion Shield Board Blue Backlight 4.5-5.5V for Arduino.

I tried something by using the serial monitor to see what values I'm getting from the buttons and when I pressed down it reads "0" but when I lift up there is no "1" (as with other switches)

My question is this I need to make any key do multiple things going through several menu systems and I need help with a workflow.

I understand there is a counter involved but how do I represent this in the simplest form?

We need:

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

You are on the right track... a "stack exchange" solution was posted with a link to the hardware instructions, saying "[... load this sketch, open the serial monitor, and see your button values...]"

The hardware is in this link.

 /* 
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
  a minimum delay between toggles to debounce the circuit (i.e. to ignore
  noise).  

  created 28 October 2009
  by Riaan Cornelius


  */

 // constants won't change. They're used here to 
 // set pin numbers:
 const int buttonPin = 0;     // the number of the pushbutton pin
 const int ledPin =  53;      // the number of the LED pin for testing

 const int BUTTON1 = 1;
 const int BUTTON2 = 2;
 const int BUTTON3 = 3;
 const int BUTTON4 = 4;
 const int BUTTON5 = 5;

 const int BUTTON1LOW = 970;
 const int BUTTON1HIGH = 1024;
 const int BUTTON2LOW = 850;
 const int BUTTON2HIGH = 950;
 const int BUTTON3LOW = 700;
 const int BUTTON3HIGH = 800;
 const int BUTTON4LOW = 400;
 const int BUTTON4HIGH = 650;
 const int BUTTON5LOW = 250;
 const int BUTTON5HIGH = 350;

 // Variables will change:
 int ledState = HIGH;         // the current state of the output pin
 int buttonState;             // the current reading from the input pin
 int lastButtonState = LOW;   // the previous reading from the input pin

 // the following variables are long's because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in an int.
 long lastDebounceTime = 0;  // the last time the output pin was toggled
 long debounceDelay = 50;    // the debounce time; increase if the output flickers

 void setup() {
   pinMode(buttonPin, INPUT);
   pinMode(ledPin, OUTPUT);
   Serial.begin(9600);
 }

 void loop() {
   // read the state of the switch into a local variable:
   int reading = analogRead(buttonPin);
   Serial.println(reading);

   /*
   Not needed while getting the values.

   int tmpButtonState = LOW;             // the current reading from the input pin

   if(reading>BUTTON5LOW && reading<BUTTON5HIGH){
     //Read switch 5
     tmpButtonState = BUTTON5;
   }else if(reading>BUTTON4LOW && reading<BUTTON4HIGH){
     //Read switch 4
     tmpButtonState = BUTTON4;
   }else if(reading>BUTTON3LOW && reading<BUTTON3HIGH){
     //Read switch 3
     tmpButtonState = BUTTON3;
   }else if(reading>BUTTON2LOW && reading<BUTTON2HIGH){
     //Read switch 2
     tmpButtonState = BUTTON2;
   }else if(reading>BUTTON1LOW && reading<BUTTON1HIGH){
     //Read switch 1
     tmpButtonState = BUTTON1;
   }else{
     //No button is pressed;
     tmpButtonState = LOW;
   }

   // check to see if you just pressed the button 
   // (i.e. the input went from LOW to a buttonState),  and you've waited 
   // long enough since the last press to ignore any noise:  

   // If the switch changed, due to noise or pressing:
   if (tmpButtonState != lastButtonState) {
     // reset the debouncing timer
     lastDebounceTime = millis();
   } 

   if ((millis() - lastDebounceTime) > debounceDelay) {
     // whatever the reading is at, it's been there for longer
     // than the debounce delay, so take it as the actual current state:
     buttonState = tmpButtonState;
     Serial.println(buttonState);
   }

   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
   lastButtonState = tmpButtonState;

   // set the LED using the state of the button for testing:
   switch(buttonState){
     case BUTTON1:
     digitalWrite(ledPin, buttonState>0);
     break;
     case BUTTON2:
     digitalWrite(ledPin, buttonState>0);
     break;
     case BUTTON3:
     digitalWrite(ledPin, buttonState>0);
     break;
     case BUTTON4:
     digitalWrite(ledPin, buttonState>0);
     break;
     case BUTTON5:
     digitalWrite(ledPin, buttonState>0);
     break;
   }
   */
 }

Hello drewiepooh

Consider

#define ProjectName "Buttons_for_1602_LCD_Keypad_Shield"
// make names
enum ButtonNames {Right, Up, Down, Left, Select};
// make variables
constexpr uint16_t UpperRight {59};
constexpr uint16_t BelowRight {0};
constexpr uint16_t UpperUp {199};
constexpr uint16_t BelowUp {60};
constexpr uint16_t UpperDown {399};
constexpr uint16_t BelowDown {200};
constexpr uint16_t UpperLeft {599};
constexpr uint16_t BelowLeft{400};
constexpr uint16_t UpperSelect {1023};
constexpr uint16_t BelowSelect{600};
constexpr uint16_t AnalogButton{A0};
uint8_t knopNew;
uint8_t knopOld;
// tools
void heartBeat(int LedPin, uint32_t currentMillis)
{
  static bool setUp = false;
  if (!setUp) pinMode (LedPin, OUTPUT), setUp = !setUp;
  digitalWrite(LedPin, (currentMillis / 500) % 2);
}
void setup()
{
  Serial.begin(115200);
  Serial.println(ProjectName);
  Serial.println(__FILE__);
  Serial.println(__DATE__);
  Serial.println(__TIME__);
}
void loop()
{
  uint32_t currentMillis = millis();
  heartBeat(LED_BUILTIN, currentMillis);
  switch (analogRead(AnalogButton))
  {
    case BelowRight  ... UpperRight: knopNew = Right; break;
    case BelowUp     ... UpperUp: knopNew = Up; break;
    case BelowDown   ... UpperDown: knopNew = Down; break;
    case BelowLeft   ... UpperLeft: knopNew = Left; break;
    case BelowSelect ... UpperSelect: knopNew = Select; break;
  }
  if (knopOld != knopNew)
  {
    knopOld = knopNew;
    switch (knopNew)
    {
      case Right: Serial.println("Right"); break;
      case Up: Serial.println("Up"); break;
      case Down: Serial.println("Down"); break;
      case Left: Serial.println("Left"); break;
      case Select: Serial.println("Select"); break;
    }
  }
}
//------------------------------------------------------------

Have a nice day and enjoy coding in C++.

To achieve your goal of using the buttons to navigate through multiple menu systems, you can use a finite state machine (FSM) approach. This allows you to define different states for each menu and transition between them based on button presses.

Here's an example of a simple FSM implementation using a switch-case structure:

  1. Define your states as constants or enums.

cppCopy code

enum State {
  MAIN_MENU,
  OPTION_1,
  OPTION_2
};
  1. Define a variable to store the current state.

cppCopy code

State currentState = MAIN_MENU;
  1. Read the button presses and store the value in a variable.

cppCopy code

int buttonValue = analogRead(A0);
  1. Use a function to determine which button was pressed.

cppCopy code

int getButton(int value) {
  if (value < 60) return 1;
  if (value < 200) return 2;
  if (value < 400) return 3;
  if (value < 600) return 4;
  if (value < 800) return 5;
  return 0;
}
  1. Use a switch-case structure to handle the different states and button presses.

cppCopy code

void loop() {
  int button = getButton(analogRead(A0));

  switch (currentState) {
    case MAIN_MENU:
      if (button == 2) { // Up button
        currentState = OPTION_1;
      } else if (button == 3) { // Down button
        currentState = OPTION_2;
      }
      break;

    case OPTION_1:
      if (button == 4) { // Left button
        currentState = MAIN_MENU;
      }
      // Add more actions for OPTION_1
      break;

    case OPTION_2:
      if (button == 4) { // Left button
        currentState = MAIN_MENU;
      }
      // Add more actions for OPTION_2
      break;
  }

  // Update the LCD based on the current state
  updateLCD();
}
  1. Finally, create a function to update the LCD based on the current state.

cppCopy code

void updateLCD() {
  switch (currentState) {
    case MAIN_MENU:
      // Display main menu on the LCD
      break;
    case OPTION_1:
      // Display option 1 on the LCD
      break;
    case OPTION_2:
      // Display option 2 on the LCD
      break;
  }
}

This example demonstrates a simple implementation of an FSM that can be easily extended to include more menu options and button actions. You can adjust the state transitions and button actions based on your requirements.

cheers!