Using Analogbuttons.h

Hi everyone! I'm trying to use the analogbuttons.h library and am trying to understand how to use it. I just dont know how to call the buttons being pressed. I mean, i've used the button.h library and I type b1.uniquePress() to use button 1. I uncommented the configuration line and can see the values listed there for the resistance.

Like for instance, if I was to use the analogbuttons.h library, how would I write if b1 is pressed that pin 13 would go high? I know this is pretty noob. Thanks!

The header file shows that the constructor you should be using takes 3 or 4 values - a number, a low analog reading, a high analog reading, and possibly a duration.

The low and high analog readings are what you see if you do an analogRead() on the pin with the resistors that you are using in place. The value should be between the low and high readings that you pass to the constructor.

So, how many switches do you have? What to the constructors look like?

Your code should look something like the example provided.

/*
  AnalogButtons,
  
  created 02 Jan 2009 V 0.1

 Connect more than one button to a single analog pin, 
 register a call-back function.which gets called when a button
 is pressed or held down for the defined number of seconds. Includes
 software key debouncing which may need to be adjusted, the the second 
 argument to AnalogButtons class. Define the ANALOG_PIN in the constructor
 of AnalogButtons.

 The circuit:

 * 5 buttons, 1 side of all buttons connected together to +5V. 
   The other side of each button is connected via a different value
   resister (tested with) 1k, 2k5, 5k8, 10k, 18k to one side of a
   100k resister which is in turn connected to GND. At the point
   where all the different resisters are joined you make a connection
   to your analog input. Basicly a different voltage divider is setup 
   depending upon which button is pressed. You have to configure the 
   Buttons Hi/Low values, see the comments in example code below and the
   AnalogButtons::configure(ANALOG_PIN) function.
   
   More or less than 5 buttons could be added, just pick different values
   of the resister sot hat all buttons have different values which arn't too
   close in size.
   
   I'm not sure what happens when Arduino is powered from batteries and Power V
   drops below V5.

 by Neil DUdman and everyone who's ever used Arduino

 */
#include "AnalogButtons.h"

#define ANALOG_PIN 0

// A call back function that you pass into the constructor of AnalogButtons, see example
// below. Alternitivly you could extend the Button class and re-define the methods pressed() 
// or held() which are called 
void handleButtons(int id, boolean held)
{  
  if (held) {
    Serial.print("button id="); Serial.print(id); Serial.println(" was pressed and held"); 
  } else{
    Serial.print("button id="); Serial.print(id); Serial.println(" was pressed only");
  }
}

AnalogButtons analogButtons(ANALOG_PIN, 30, &handleButtons);
Button b1 = Button(1, 51, 55);
Button b2 = Button(2, 70, 78);
Button b3 = Button(3, 105, 112);


void setup() 
{
  Serial.begin(9600); 
  Serial.println("Testing your Analog buttons");
  
  analogButtons.addButton(b1);
  analogButtons.addButton(b2);
  analogButtons.addButton(b3);

 }

void loop() 
{  
  // To check values when button are pressed
  analogButtons.checkButtons();

  // To configure the MAX/Min values for each 
  // Button, uncomment this line, make sure you've called Serial.begin(9600); 
  // Then in turn hold town each botton, noting the max/min values
  //AnalogButtons::configure(ANALOG_PIN); //delay(1000);
}

If I remove the //analogbuttons:configure I can see the correct values when the buttons are pressed and held. But im confused on how to write the code to say, if button1 is pressed, then do something. else if button2 is pressed do something else

When the AnalogButtons object is created, the final argument is a function to be called when the checkButtons() function is called, and a button is pressed.

In that function (handleButtons()), you are told which button was pressed or held. In that function, you can do whatever you need to do, if the appropriate button is pressed or held.

if(id == 3)
{
  // The button with id of 3 was pressed or held. Do whatever...
}