Getting Buttons' values for LCD1602 - Strategy

Hi, I have been trying to write sketches for Arduino Uno r 3 and lately I managed to more or less make it work.
But I would like someone to point to me to where I can get FULL details on the strategy in the sketch to get the values that "cold turkey" have been given to me in other sketches when you read the value I believe is A0 and then for the return comparing with the values that I have seen in other sketches I can determine which KEY was pressed and consequently use that info to feed a switch loop to produce a menu and so on.
Is there any book or website or info available so I can write myself the whole sketch knowing exactly what I am doing and why?

At this my intuition tells me that probably each key read (as it is pushed) brings a voltage between 0 and 5 volts. Then some hidden function similar Arduino function map(), maps those values to integer values from let say from Zero to 850.
When I print the value a integer of the "read()" functions I get the integers: 1; 2; 3; 4; and 5.
How come those 1,2,3,4,5 foreach button on the LCD Shield is compared with values like {0,30, 150, 369, 535 and invalid if above 850?
Very much appreciated if you can give me a lead on where to find all this details.
Thanks you

Probably does not translate internationally.

Programming is procedural, where one step follows the previous. Your (above) monologue shows "try anything and hope" (and trying to use words you do not yet understand) rather than "start at the beginning with step one and keep it simple."

There are no hidden functions. You determine exactly how your code proceeds.

So... it seems...

  1. you have some buttons and an LCD.
  2. you have some buttons connected through a resistor ladder (of increasing resistance) to the analog input A0.
  3. you want to determine which button was pressed by reading the value returned from the resistor/button array in #2.
  4. you wish to map the analog value to a digit 1, 2, 3, 4, or 5.
  5. you want to display the digit on the LCD

Draw your project, pen and paper will work, post it here.

Things to read and try:

  1. Resistor ladder: https://electronics.stackexchange.com/questions/99417/what-resistors-to-use-to-read-several-buttons-with-a-single-analog-pin
  2. The analogRead(); function: https://docs.arduino.cc/language-reference/en/functions/analog-io/analogRead/
  3. The map(); function: https://docs.arduino.cc/language-reference/en/functions/math/map/
  4. Arduino reference to LCD 1602: https://docs.arduino.cc/learn/electronics/lcd-displays/

Be clear in your descriptions. Use words you and others can understand.

  • Always, always, always start out with a proposed schematic.

  • I often use this skeleton sketch with four switches on an Analog input pin.
//
//================================================^================================================
//                                    4 Switches on an Analog Pin
//================================================^================================================
//
//  URL
//
//  See schematic:  AnalogSwitches
//
//  Version    YY/MM/DD    Comments
//  =======    ========    ========================================================================
//  1.00       23/07/05    Running code
//
//
//
//
//  Notes:
//
//
//
//

//
//================================================^================================================
//
#define ENABLED           true
#define DISABLED          false


//                              G P I O s   A n d   V a r i a b l e s
//================================================^================================================
//

//Analogs
//================================================
//
const byte buttonPin1   = A0;   //Four switches in our resistor ladder.


//INPUTS
//================================================
//


//OUTPUTS
//================================================
//
const byte heartbeatLED = 13;   //Gives us an indication if code is blocking.


//VARIABLES
//================================================
//
bool buttonPin1Flag           = ENABLED;   //Enabled = we may check for a buttonPin being pressed.

const int noPressValue        = 100;       //Greater than this value means a switch might be pressed.
const int offsetValue         = 50;        //High and low offset used to define a voltage window.

const int SW1triggerPoint      = 1023;      //The expected ladder voltage when the switch is pressed.
const int SW2triggerPoint      = 768;
const int SW3triggerPoint      = 512;
const int SW4triggerPoint      = 255;

//================================
//Define TIMERs
//
unsigned long heartbeatTime;
const unsigned long heartbeatInterval  = 500ul;    //500ms

unsigned long checkSwitchTime;
const unsigned long switchInterval     = 50ul;     //50ms


//                                           s e t u p ( )
//================================================^================================================
//
void setup()
{
  Serial.begin(115200);

  pinMode(heartbeatLED, OUTPUT);

} //END of   setup()


//                                            l o o p ( )
//================================================^================================================
//
void loop()
{
  //========================================================================  T I M E R  heartbeatLED
  //Is it time to toggle the heartbeat LED ?
  if (millis() - heartbeatTime >= heartbeatInterval)
  {
    //Restart this TIMER.
    heartbeatTime = millis();

    //Toggle the heartbeat LED.
    digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
  }

  //========================================================================  T I M E R  check switches
  //Is it time to scan our switches ?
  if (millis() - checkSwitchTime >= switchInterval)
  {
    //Restart this TIMER.
    checkSwitchTime = millis();

    checkSwitches();
  }


  //================================================
  //       Other non blocking code goes here
  //================================================


} //END of   loop()


//                                   c h e c k S w i t c h e s ( )
//================================================^================================================
//
void checkSwitches()
{
  int analogReading;

  //========================================================================  b u t t o n P i n 1
  //                                                                          SW1, SW2, SW3, SW4
  analogReading = analogRead(buttonPin1);

  //10us settling time.
  delayMicroseconds(10);

  //For a more accurate measurement, read the analog pin a 2nd time.
  analogReading = analogRead(buttonPin1);

  //================================================
  //Are any of the buttonPin1 switches being pressed ?
  if (analogReading >= noPressValue)
  {
    //========================
    //Are we allowed to decode switches ?
    if (buttonPin1Flag == ENABLED)
    {
      //=========== >>>--->  S W 1
      //Are we within this switch's high and low thresholds ?
      if (analogReading >= SW1triggerPoint - offsetValue && analogReading <= SW1triggerPoint + offsetValue)
      {
        Serial.print(F("\nbuttonPin1 Reads = "));
        Serial.println(analogReading);
        Serial.println(F("Switch SW1 is Closed."));

        //Prevent further switch decoding until all the switches on this pin are released.
        buttonPin1Flag = DISABLED;
      }

      //=========== >>>--->  S W 2
      //Are we within this switch's high and low thresholds ?
      else if (analogReading >= SW2triggerPoint - offsetValue && analogReading <= SW2triggerPoint + offsetValue)
      {
        Serial.print(F("\nButtonPin1 Reads = "));
        Serial.println(analogReading);
        Serial.println(F("Switch SW2 is Closed."));

        //Prevent further switch decoding until all the switches on this pin are released.
        buttonPin1Flag = DISABLED;
      }

      //=========== >>>--->  S W 3
      //Are we within this switch's high and low thresholds ?
      else if (analogReading >= SW3triggerPoint - offsetValue && analogReading <= SW3triggerPoint + offsetValue)
      {
        Serial.print(F("\nButtonPin1 Reads = "));
        Serial.println(analogReading);
        Serial.println(F("Switch SW3 is Closed."));

        //Prevent further switch decoding until all the switches on this pin are released.
        buttonPin1Flag = DISABLED;
      }

      //=========== >>>--->  S W 4
      //Are we within this switch's high and low thresholds ?
      else if (analogReading >= SW4triggerPoint - offsetValue && analogReading <= SW4triggerPoint + offsetValue)
      {
        Serial.print(F("\nButtonPin1 Reads = "));
        Serial.println(analogReading);
        Serial.println(F("Switch SW4 is Closed."));

        //Prevent further switch decoding until all the switches on this pin are released.
        buttonPin1Flag = DISABLED;
      }

    } //END of   if (buttonPin1Flag == ENABLED)
  } //END of   if (analogReading >= noPressValue)

  //================================================
  //Switches are all released.
  else
  {
    //Allow switch decoding for this pin.
    buttonPin1Flag = ENABLED;
  }

  //END of this analog switch


  //========================================================================  Next Switch

} //END of   checkSwitches()



//================================================^================================================
1 Like

it is unclear what kind of hardware you are using.
You should always give links to the products you are using and post the specific code you are asking for.

My assumption is you are talking about a LCD shield with buttons.

The buttons are connected with different resistors to A0.
Therefore you need to read with analogRead the value from A0
Each button will generate a different reading.
Hence you will get different values around 0, 30, 150, 369, 535, 850. Your code needs to be able to check not only against these exact values but also some nuances. These values can differ from board to board and can also differ based on your power source.

In my sketches for the LCD keypbad shield I use a simple function which returns the value from an enumeration.

Something like:

enum {btnRIGHT, btnUP, btnDOWN, btnLEFT, btnSELECT, btnNONE};

// define some values used by the panel and buttons
byte lcd_key    = 0;                   // the pressed button
int adc_key_in  = 0;                   // the readed ADC value
int adc_old     = 0;                   // the previous ADC value

// read the buttons
byte read_LCD_buttons() {
  adc_key_in = analogRead(0);          // read the value from the sensor
  Serial.println(adc_key_in);
  // my buttons when readed vary around these values: 0,  99, 254, 407, 639
  // we add approx 50 to those values and check if we are close
  if (adc_key_in > 1000) return btnNONE;         // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  return btnNONE;                      // when all others fail, return this...
}

Thank you very much, I will try.

Sorry, did not realize.

  • Google resistor voltage divider.

Thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.