18 button keyboard

Hi Folks,

First Im a really noob for programming. :slight_smile: I have this code and working fine. Digital pins all ok but someone can do help to add remaining analog ports to work like A3? Arduino Pro Micro A0, A1, A2 pins are remaining..

No need shorter code or better code! :slight_smile:

Thank you!

#include <Keyboard.h>
#include <Bounce2.h>



// Assume the stick button is connected to pin A3.
const int BUTTON_PIN = A3; 
const int DEBOUNCE_INTERVAL = 10;
Bounce Button = Bounce();


#define BUTTON_KEY  'm'
#define BOUNCE_LOCK_OUT 1
#define BOUNCE_WAIT 1
#define BOUNCE_COUNT 1
#define NBUTTONS 14

int buttonPins[] = {
  0, // button 6   
  1, // button 7 
  2, // button 8
  3, // button 9
  4, // button b
  5, // button n
  6, // button y
  7, // button u
  8, // button i
  9, // button o
  10, // button p
  14, // button h
  15, // button j
  16  // button k
};

char buttonPresets[] = { 
  '6', //button 0
  '7', //button 1
  '8', //button 2
  '9', //button 3
  'b', //button 4
  'n', //button 5
  'y', //button 6
  'u', //button 7
  'i', //button 8
  'o', //button 9
  ' ', //button 10
  'h', //button 11
  'j', //button 12
  'k', //button 13
  };

boolean buttonPressed[NBUTTONS]; // Instantiate a Bounce object array to store each debouncer in
Bounce debouncers[NBUTTONS];     //Instantiate a counter array for each button to debounce count timer
int debounceCount[NBUTTONS];

void setup() {
  Serial.begin(115200);
  Keyboard.begin();
  Button.attach(BUTTON_PIN, INPUT_PULLUP);
  Button.interval(DEBOUNCE_INTERVAL);
  
  for (int i = 0; i < NBUTTONS; i++) {
     debouncers[i] = Bounce();
     debounceCount[i] = 0;
     pinMode(buttonPins[i],INPUT_PULLUP);
     (debouncers[i]).attach(buttonPins[i]);
     (debouncers[i]).interval(BOUNCE_WAIT);
     delay(100);
     buttonPressed[i] = false;
   }
}

void loop() {
 

  Button.update();
  if (Button.fell()) {
    Keyboard.press(BUTTON_KEY);
  }
  if (Button.rose()) {
    Keyboard.release(BUTTON_KEY);
  }


{
  for (int j = 0; j < NBUTTONS; j++) { // iterate over each button (pin)
    
     (debouncers[j]).update();         //check current value
     int value = (debouncers[j]).read();
     
     if ( value == LOW ) { // if button pressed
       
       
       // The button has been held down long enough and it hasn't been previously registered as pressed
       if(debounceCount[j] == BOUNCE_COUNT && buttonPressed[j] == false) { 
          Keyboard.press(char(buttonPresets[j])); //Keyboard.write('1');
          buttonPressed[j] = true;
        } else {
            if(debounceCount[j] < BOUNCE_COUNT) { 
              debounceCount[j] = debounceCount[j] + 1; 
            }
        }
        
      } else { //button is not pressed
        
        if(debounceCount[j] > 0) {
          debounceCount[j] = debounceCount[j] - 1; // keep decreasing count unless 0
        } else {
           Keyboard.release(char(buttonPresets[j])); // if 0 then release button
           buttonPressed[j] = false;
        }
      }
  }
}
}

Your method would work fine except it is I/O intensive. Without a schematic it appears each button is using a port pin. Try searching for "arduino keyboard example" there are a lot of them available. Since I have had the same problem I many times will use a keyboard encoder chip.

just add them to your list of buttons (e.g A0, A1, A2) they will just the same as digital pins

Hey, I tried everything based on my knowledge (noob level) but I can't success. Here are the affected lines of code. I have commented which lines need help.


const int BUTTON_PIN = A3; 
const int BUTTON_PIN2 = A2;  // added A2, A1, A0 pins
const int BUTTON_PIN3 = A1;
const int BUTTON_PIN4 = A0;



#define BUTTON_KEY  'm'
#define BUTTON_KEY2  'l'  // added wich characters need to print 
#define BUTTON_KEY3  'n'
#define BUTTON_KEY4  'o'


void setup() {
  Serial.begin(115200);
  Keyboard.begin();
  Button.attach(BUTTON_PIN, INPUT_PULLUP);  // Need help for here to extend code wich BUTTON_PIN activated  now only one pin work
 

  Button.update();
  if (Button.fell()) {
    Keyboard.press(BUTTON_KEY);  // Need help for here to extend code wich defined BUTTON_KEY print ony one character can be print
  }
  if (Button.rose()) {
    Keyboard.release(BUTTON_KEY);  // Need help for here too  
  }

Thank you!!!

Please show the full sketch, even if you change only a comma.
If you leave parts of the code out, then those are the parts that are the most of interest to us :face_with_raised_eyebrow:

Koepel,

okay sorry for that. Here is the expanded code:

#include <Keyboard.h>
#include <Bounce2.h>



// Assume the stick button is connected to pin A3.
const int BUTTON_PIN = A3; 
const int BUTTON_PIN2 = A2;  // added A2, A1, A0 pins
const int BUTTON_PIN3 = A1;
const int BUTTON_PIN4 = A0;

const int DEBOUNCE_INTERVAL = 10;
Bounce Button = Bounce();


#define BUTTON_KEY  'm'
#define BUTTON_KEY2  'l'  // added wich characters need to print 
#define BUTTON_KEY3  'n' 
#define BUTTON_KEY4  'o' 

#define BOUNCE_LOCK_OUT 1
#define BOUNCE_WAIT 1
#define BOUNCE_COUNT 1
#define NBUTTONS 14

int buttonPins[] = {
  0, // button 6   
  1, // button 7 
  2, // button 8
  3, // button 9
  4, // button b
  5, // button n
  6, // button y
  7, // button u
  8, // button i
  9, // button o
  10, // button p
  14, // button h
  15, // button j
  16  // button k
};

char buttonPresets[] = { 
  '6', //button 0
  '7', //button 1
  '8', //button 2
  '9', //button 3
  'b', //button 4
  'n', //button 5
  'y', //button 6
  'u', //button 7
  'i', //button 8
  'o', //button 9
  ' ', //button 10
  'h', //button 11
  'j', //button 12
  'k', //button 13
  };

boolean buttonPressed[NBUTTONS]; // Instantiate a Bounce object array to store each debouncer in
Bounce debouncers[NBUTTONS];     //Instantiate a counter array for each button to debounce count timer
int debounceCount[NBUTTONS];

void setup() {
  Serial.begin(115200);
  Keyboard.begin();
  Button.attach(BUTTON_PIN, INPUT_PULLUP);  // This is the original code need help for here to expand code wich BUTTON_PIN activated  now only one pin work
 
  Button.interval(DEBOUNCE_INTERVAL);
  
  for (int i = 0; i < NBUTTONS; i++) {
     debouncers[i] = Bounce();
     debounceCount[i] = 0;
     pinMode(buttonPins[i],INPUT_PULLUP);
     (debouncers[i]).attach(buttonPins[i]);
     (debouncers[i]).interval(BOUNCE_WAIT);
     delay(100);
     buttonPressed[i] = false;
   }
}

void loop() {
 

  Button.update();
  if (Button.fell()) {
    Keyboard.press(BUTTON_KEY);   // Need help for here to expand code wich defined BUTTON_KEY print. Now ony one character can be print.
  }
  if (Button.rose()) {
    Keyboard.release(BUTTON_KEY);  // Need help here too
  }


{
  for (int j = 0; j < NBUTTONS; j++) { // iterate over each button (pin)
    
     (debouncers[j]).update();         //check current value
     int value = (debouncers[j]).read();
     
     if ( value == LOW ) { // if button pressed
       
       
       // The button has been held down long enough and it hasn't been previously registered as pressed
       if(debounceCount[j] == BOUNCE_COUNT && buttonPressed[j] == false) { 
          Keyboard.press(char(buttonPresets[j])); //Keyboard.write('1');
          buttonPressed[j] = true;
        } else {
            if(debounceCount[j] < BOUNCE_COUNT) { 
              debounceCount[j] = debounceCount[j] + 1; 
            }
        }
        
      } else { //button is not pressed
        
        if(debounceCount[j] > 0) {
          debounceCount[j] = debounceCount[j] - 1; // keep decreasing count unless 0
        } else {
           Keyboard.release(char(buttonPresets[j])); // if 0 then release button
           buttonPressed[j] = false;
        }
      }
  }
}
}

This is the Bounce2 example for two buttons: bounceTwo.ino
This is the Bounce2 example for many buttons: bounceMore.ino

The example uses a pointer and a 'new' keyword. That was probably easier to initialize the objects. I suggest you copy that. Put a link to the example in comments in your sketch.
What you do in setup with Bounce() might also work. I have not tested it.

Do you see in "bounceMore.ino" how all the buttons are initialized in setup() and all the buttons are checked in the loop() ? That is exactly what you need.
Connect the Button.fell() to Keyboard.press() and the Button.rose() to Keyboard.release() for all the buttons in the for-loop.

why not

  9, // button o
  10, // button p
  14, // button h
  15, // button j
  16,  // button k
  A3,
  A2,
  A1,
  A0
};

Oh my good. What a noob I am. Thank you gcjr, it was soo easy and how obvious it is. :smiley: