How replace code from LCDkeypad to button

The sketch where I’m fighting with for weeks is written for a LCDscreen with buttons.

But i’m using only a big LCD screen without keypad buttons.

I only need to use two buttons SELECT and DOWN but I tried everything on port A0 as written
here i don’t get it working.

The code is complicated for me so i hope somebody can point me the right direction.

I assume the changes must be made in this part of the code

LCDKeypad::LCDKeypad() : LiquidCrystal(8, 9, 4, 5, 6, 7){}

int LCDKeypad::button(){
  static int NUM_KEYS=5;
  static int adc_key_val[5] ={30, 150, 360, 535, 760};
  int k, input;
  input=analogRead(0);
  for (k = 0; k < NUM_KEYS; k++){
    if (input < adc_key_val[k]){
      return k;
    }
  }
  if (k >= NUM_KEYS){
    k = -1;     // No valid key pressed
  }
  return k;
}

I hope somebody can help me out

buttonprob.ino (8.86 KB)

notes.h (1.86 KB)

pitches.h (1.96 KB)

ScrollableMenu.cpp (1.62 KB)

ScrollableMenu.h (676 Bytes)

The schematic on the link for using an analog keypad is not 100% correct, that is one thing also i found it confusing that there is more than 1 option explained without a clear separation in code solutions. But Ok, This should be the schematic for the Analog solution, now with the other end of the rail connected to the 5v+, and also i added 1 extra resistor between the switches and pin A0. All resistors should have the same value (somewhere between 1k and 3.3k would be fine sort of) for the digital pin solution you would need a different sowftware solution.

Sorry, small correction on the image

Analog keymap.JPG

The schematic on the link for using an analog keypad is not 100% correct

It was just an example to show what I want. i'm open for any suggetion.

All I need is two pushuttons "select" and "down" working like it would be in the keypad .

Do you understand how that button code works?

It reads the analog input and based on the value determins which buttons was pressed (if any) and returns that.

If you need two buttons, do you want them on the analog input as the shield or just on some pins? For the latter, just replace the code inside the LCDKeypad::button() by some digitalReads.

Or get rid of the whole thing althogether and just do digitalreads in loop() or wherever it's needed.

Do you understand how that button code works?

It reads the analog input and based on the value determins which buttons was pressed (if any) and returns that.

I thought it was a system what used resistors. So every ohm value would stand for a button. I experimented but no results. The code is the original code from the keypad i did not edit anything in that.

But if i leave the A0 empty then the options keep scrolling in the display. When I connect A0 to ground it stops scrolling. And there is my confusion. If different ohm values are for ever button how can no value createan action.

Maybe i'm totally wrong in my thinking i'm new with arduino so I still have to learn a lot.

The easiest solution for me is just to simulate the down en select button as they were in the keypad.

The easiest solution for me is just to simulate the down en select button as they were in the keypad

The easiest solution is surely to use digitalRead() to determine the state of 2 inputs and to change the code to act on the state returned by each. The wiring is easy (arrange for the inputs to be taken LOW when the button is pressed), the code changes are minor pinMode(pinNum, INPUT_PULLUP); and

if (digitalRead(pinNum) == LOW)
  {
    //do something
  {

Thanks for your reply heliBob.

In the arduino for Dummies I saw this way of buttons only my knowledge is not big enough to know how to replace it in the sketch.

Very sorry, but I think that you should first start studying some stuff instead of taking up projects that you have no idea how they work.

Anyway, add the following just be for the classes; the dots are existing code and I left somelines in for reference

...
...// choose two pins for the buttons
const byte selectPin = 2;
const byte downPin = 3;

/************** Classes ******************/
/*****************************************/
...
...

You need to adjust the pin numbers to something that suites your needs; 2 and 3 are free as far as I can see.

Add the following in setup after the other pinMode lines

 pinMode(selectPin, INPUT_PULLUP);
  pinMode(downPin, INPUT_PULLUP);

Wire the buttons between pin and GND; the INPUT_PULLUP prevents floating inputs that will give false readings.

Replace

int LCDKeypad::button()
{
  static int NUM_KEYS=5;
  static int adc_key_val[5] ={30, 150, 360, 535, 760};
  int k, input;
  input=analogRead(0);
  for (k = 0; k < NUM_KEYS; k++){
    if (input < adc_key_val[k]){
      return k;
    }
  }
  if (k >= NUM_KEYS){
    k = -1;     // No valid key pressed
  }
  return k;
}

by

int LCDKeypad::button()
{
  if (digitalRead(downPin) == LOW)
  {
    return KEYPAD_DOWN;
  }
  if (digitalRead(selectPin) == LOW)
  {
    return KEYPAD_SELECT;
  }
}

Note that because we use INPUT_PULLUP, the logic is inversed; a pressed button reads low, a released button reads high.

This is probably the simplest implementation for you.

And now back to the books and start to learn.[/code]

Thanks again i give it a try.

And now back to the books and start to learn.

Yes I did and i got a solution also. just make it like this.
It worked.

See picture

Sterretje, i tried your code. the buttons are responding but is does not work good.

But I got it working with some resistors.

I want to thank you very much for the time you have spend helping me.