AnalogButtons Help

I'm trying to us the analogButtons library to read a bunch of buttons. 13 to be exact.

I've used 2 analog pins because I couldn't fit all 13 on 1 pin with the R 2R ladder. In doing so I have overlapping values which shouldn't really be an issue where there on different pins.

The problem is I have no idea how to write the code.

Will the library have to be modified to make this work??

Any help would be greatly appreciated!

How many buttons on each analog pin ? I supose 6 on one and 7 to the other.

considering the resistors you use for the buton lader, you have to check the values that analogRead get back for each button pressed.

so from considering the pins your select for each lader (let's say A0 and A1)

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int b1, b2;

 b1 = analogRead(A0);   // first lader
 b2 = analogRead(A1);   // secind one

 Serial.print("Button 1 : "); 
 Serial.print(b1, DEC);
 Serial.print(" / Button 2 : ");
 Serial.println(b2, DEC);
}

so from, you will get values when you press buttons.

lets take one example :

No button pressed = 1023
Button 1 : 800
Button 2 : 650
Button 3 : 430
Button 4 : 300
Button 5 : 175
Button 6: 90

so from take the mid difference between 2 consecutive buttons to be the base level to identify it.

I mean :

(Button 1 - Button 2) / 2 = (800-650)/2 = 75

then we add this value to the button that got the less value. that will give us for almost shure this button never reach this value. So from we can define floor value to separate eaxh button.

so from if the value is > Button2 + 75 this means that button 1 was pressed (except when no button is pressed because no button means 1023), it's 650+75 = 725

if we do each time like that for each button we got (with rounding) :

No button = 1023
floor1 = (1023 - 800)/2 + 800 = 911
floor2 = (800 - 650) /2 + 650 = 725
floor3 = (650 - 430)/2 + 430 = 540
floor4 = (430 - 300)/2 + 300 = 365
floor5 = (300 - 175 )/2 +175 = 238
floor6 = (175 - 90)/2 + 90 = 133

now we have these values, it's easy to do a function that will return the number pressed considering the analogRead function.

we could write a function like :

byte FirstLader()
{
  int val;

  val = analogRead(A0);

  if (val > 911) return 0; // no button pressed
  if (val > 725) return 1; 
  if (val > 540) return 2;
  if (val > 365) return 3;
  if (val > 238) return 4;
  if (val > 133) return 5;
  return 6;
}

so from, into the loop function you could use this function like :

void loop()
{
  byte button1;

  button1 = FirstLader();

  switch(button1)
  {
    case 0 : Serial.println("No button pressed"); break;
    case 1 : Serial.println("Button #1 pressed"); break;
    case 2 : Serial.println("Button #2 pressed"); break;
    case 3 : Serial.println("Button #3 pressed"); break;
    case 4 : Serial.println("Button #4 pressed"); break;
    case 5 : Serial.println("Button #5 pressed"); break;
    case 6 : Serial.println("Button #6 pressed"); break;
 }
}

Hope it can help you.

Regards