Using Analogbuttons with the button library

Hi everyone

Would it be possible to use Analog buttons connected to the I2c bus with the button library which has features such as isPressed and uniquePress
I am trying to modify some code that uses this library and I cannot work out how to modify it to use adc buttons.

I have a standard lcd shield with 6 buttons on it, the board this sketch was intended to run on uses 4 individual buttons.
I have found and run a sketch that I can run to get the analog value for each key pressed and I am thinking I should be able to assign those somehow to the buttons in the original sketch but I am not certain it will work.

Has anyone any pointers for me or helpful nudges they could provide please.
I have searched google quite a bit trying to find a solution but haven't been successful so far, perhaps I am asking the wrong question or asking it the wrong way?
Thanks for reading
Steve

You have a link for Analog buttons?

Would it be possible to use Analog buttons ...

What Analog buttons?

... with the button library ...

What button library?

I am trying to modify some code that uses this library ...

What code?

I have found and run a sketch ...

What sketch?

perhaps I am asking the wrong question or asking it the wrong way?

The more information you provide the better the answer will be.

hahaha... that was entertaining. XD

Hey
Sorry guys. I never received a topic reply for some reason even though I selected to be notified.

Heres what I am trying for and what I have found so far.
I have used the below code to get the analog values from my lcd shield

/*************************************************************************************

  Mark Bramwell, July 2010

  This program will test the LCD panel and the buttons.When you push the button on the shield?
  the screen will show the corresponding one.
 
  Connection: Plug the LCD Keypad to the UNO(or other controllers)

**************************************************************************************/

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int a=0;
int bkl         = 6; 
        
byte bklIdle    = 10;       
byte bklOn      = 100;       
int bklDelay    = 10000;   
unsigned long bklTime = 0;


void setup()
{
  pinMode(bkl, OUTPUT);
  digitalWrite(bkl, HIGH);

lcd.begin(16, 2);

pinMode(A0, INPUT);

}
void loop()
{
a = analogRead(0);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("  analogRead() ");
lcd.setCursor(0,1);
lcd.print("  value is :");
lcd.print(a);
delay(250);
}

I then used the following code and modified the values to match my analog values on this line "int adc_key_val[5] ={0, 99, 257, 409, 643 };"
It works well but for some reason I could never get the select key to register

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

char msgs[5][16] = {"Select Key OK ",
                    "Right Key OK  ",               
                    "Up Key OK  ",
                    "Down Key OK  ",
                    "Left Key OK" };

int adc_key_val[5] ={0, 99, 257, 409, 643 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

void setup()
{
  pinMode(A1, OUTPUT); // analog 1 to digital 15
  digitalWrite(A1, HIGH);
  pinMode(A2, OUTPUT); // analog 2 to digital 16
  digitalWrite(A2, HIGH);
  pinMode(A3, OUTPUT); // analog 3 to digital 17
  digitalWrite(A3, HIGH);
  pinMode(A4, OUTPUT); // analog 4 to digital 18
  digitalWrite(A4, HIGH);
  
  lcd.clear(); 
  lcd.begin(16, 2);
  lcd.setCursor(0,0); 
  lcd.print("ADC key testing"); 
}

void loop()
{
  adc_key_in = analogRead(0);    // read the value from the sensor 
  key = get_key(adc_key_in);  // convert into key press
 
  if (key != oldkey)   // if keypress is detected
   {
    delay(50);  // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor 
    key = get_key(adc_key_in);    // convert into key press
    if (key != oldkey)    
    {   
      lcd.setCursor(0, 1);
      oldkey = key;
      if (key >=0){
           lcd.print(msgs[key]);              
      }
    }
  }
 delay(100);
}

// Convert ADC value to key number
int get_key(unsigned int input)
{
    int k;
   
    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;
}

The following is the code I am trying to use but with ADC buttons on the shield instead of a dedicated board

Code is too long to post so I have attached it
Link to the button library is at the top of the sketch

I realise a lot of people on here ask questions that are way too vague and I apologise for not including any actual info in my post. I thought I was at the time.
You should now have all the info needed to actually give me the answer I am looking for.
Namely can I use my 6 buttons on the LCD shield which are attached to an I2C bus instead of the defined buttons in the attached sketch. By defining them as digital pins somehow perhaps?

I am using the lcd and keypad shield at the following link
http://www.dfrobot.com/wiki/index.php/Arduino_Shield

Typhon3.ino (23.3 KB)

Yes. The buttons aee not I2C buttons. If you are going to bring I2C into tge discussion you have to define tha MASTER and the SLAVES
I2C iz ADDRESS based. Who's talking yo who?
I don't see what yiur problem is. Ic yiu understand HIOW the "analog buttons" ( which is just a fancy name for switches connected to a voltage divider) works you can do whstever you want. Do you understand it? If so, what's tge problem?
I can't believe you aee asking the question you are asking?
How about this:
" If I feed my cat dog food will he do tricks?"

Apologies. I meant ADC not I2c.
I am using analog pin 0 for all 6 of the buttons.

Noone is talking to me. I am trying to work it out on my own.
I came here for help because you guys are more knowledgable than I am.
I am not here for people to take the piss or just tell me I don't know what I am talking about.

The question I am asking is would it be possible to modify the typhoon code to use my board with ADC buttons instead of the button library that it uses. Because it has some code for unique presses etc which I am not sure I can recreate using the ADC button code.

Don't get your knickers in a twist.
If someone doesn't understand your question , maybe it doesn't make sense.
If you want an answer, state the problem.
Instead of telling us what you want to do, try telli g us why you can't do it.
State the problem.
What is the problem?

And just for tge record "Who's talking to who?" means
What is the I2C MASTER and WHO are the I2C SLAVES ?

I am trying not to but I have seen a huge amount of posts on the arduino forum where knowledgable people are just telling newbies sarcastic responses instead of helping at all.

Sorry if I took it the wrong way.

What I am having trouble with is merging the 2 codes. I am wanting to know if I can somehow define the analog values of the ADC buttons and use those values in the code where is references digital pins in the existing code or if I have to re write the whole thing and delete any reference to the button library and therefore lose all the features of that library.

The latter.
The analog buttons (the LCD SHIELD switches connected to the built-in voltage divider) only allow you to cspture button presses using a form of CASE conditionals that are able to identify specific buttons by the voltage at those points in the voltsge divider. What code you run when a button is pressed is up to you. If you want to turn On a digital output when button-1 is pressed you just insert that cide.
I ca't look at tge library becsyse I'm not near a computer.
I'm on my mobile. What is tge library you rwferred to written to do?

Thats a shame.
Hate taking the long hard road :slight_smile:

It is basically designed to hide and remove the need to use the pinMode and digitalRead calls and has some booleans built in like unique press, was pressed, state changed etc which I thought might be too difficult to do with the ADC buttons

Analog buttons: It is what it is...(not digital)

Okay so do I need to look into debouncing 6 ADC buttons.
I found the following code that may help

int old_button = 0;

int getButton()
{
  int i, z, sum
  int button;

  sum = 0;
  for (i=0; i < 4; i++)
  {
     sum += analogRead(5);
  }
  z = sum / 4;
  if (z > 1021) button = 0;                                           
  else if (z > 511 && z < 514) button = 1;                     
  else if (z > 680 && z < 684) button = 2;                
  else if (z > 766 && z < 770) button = 3;                
  else if (z > 817 && z < 822) button = 4;             
  else if (z > 851 && z < 856) button = 5; 
  else if (z > 875 && z < 880) button = 6;
  else button = 0;

  return button;
}


void loop ()
{
  int button, button2, pressed_button;  
  button = getButton();
  if (button != old_button)
  {
      delay(50);        // debounce
      button2 = getButton();

      if (button == button2)
      {
         old_button = button;
         presed_button = button;
         Serial.println(pressed_button);
      }
   }
}

and then I can reference to 1,2,3,4,5 and 6 for the button presses in the code and remove all the current button press references.
At the end of the day I think all the boolean stuff is not needed. All I need is the select button, a menu button and an up and down and for them to work when I press them.
I will go and modify this to use on my lcd and see if it works

Post a link for your lcd shield and I shoulld be able to find the button example code that does thst.

Does the cat/dog analogy make sense now?

This is the link to the shield. It may be my lack of knowledge but it doesn't look debounced to me?

Of course not. It's an analog voltage connected to the analog pin by a switch.
There is absolutely NO correlation to a DIGITAL (HIGH/LOW) button.
There is no debouncing because there is NO BOUNCE.
NO HIGH
NO LOW
hence, no bounce. Get it?
APPLES & ORANGES

Ah I see. Sorry. So being analog its a little simpler.
Pressed, you get a voltage and a reading, not pressed you get a 0 voltage .
Why are there analog debounce codes all over the internet if its not needed?

Got it now.
I think I should be able to alter the code with that to do what I need.

Thanks for putting me straight. Don't know why I overlooked that.

You already have the Example code from the page you linked.

Okay

I have attempted to eradicate all the button references and replace them with arc values through the code.
It compiles okay and displays the title "Typhon reef" with backlight but then the screen flickers, displays faint words and goes black periodically.
Could you please have a look and see if anything obvious sticks out?
I am still trying to debug it also.

TyphonADC.ino (24.1 KB)