I am making my own macro keyboard. Could please add a debounce function to the code? I am really not able to figure that out. Thanks in advance



#include "Keyboard.h"


#include <Keypad.h>


const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns


char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'},
};

byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9 }; 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup() {
  pinMode(A0, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);
    pinMode(16, INPUT_PULLUP);
    pinMode(15, INPUT_PULLUP);
    pinMode(14, INPUT_PULLUP);
     

  Serial.begin(9600);
  Keyboard.begin();
}


void sendMacroCommand(uint8_t key) {
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(key);
}

void loop() {

  
  while (digitalRead(A0) == HIGH) {

    // do nothing until pin 2 goes low

    delay(500);

  }

  char key = keypad.getKey();

  if (key) {
    Serial.println(key);
    switch (key) 
    {
      case '1':
  Keyboard.press(KEY_LEFT_ALT);
   Keyboard.press(KEY_LEFT_ARROW);
        break;
        
      case '2':
       Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press(KEY_RIGHT_ARROW); 
        break;
      case '3':
        sendMacroCommand(KEY_F3);
        break;
      case '4':
        sendMacroCommand(KEY_F4);
        break;
      case '5':
         Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
   Keyboard.press('b');

        break;
      case '6':
        sendMacroCommand(KEY_F6);
        break;
      case '7':
        sendMacroCommand(KEY_F7);
        break;
      case '8':
        sendMacroCommand(KEY_F8);
        break;
      case '9':
        sendMacroCommand(KEY_F9);
        break;
      case '0':
        sendMacroCommand(KEY_F10);
        break;
      case '*':
        sendMacroCommand(KEY_F11);
        break;
      case '#':
        sendMacroCommand(KEY_F12);
        break;
      case 'A':
       Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
   Keyboard.press('m');
        break;
      case 'B':
        sendMacroCommand('b');
        break;
      case 'C':
        sendMacroCommand('c');
        break;
      case 'D':
        Keyboard.press(KEY_LEFT_GUI); 
        Keyboard.press('r'); 

        Keyboard.release(KEY_LEFT_GUI); 
        Keyboard.release('r'); 
        delay(50); //give your system time to catch up with these android-speed keyboard presses
        Keyboard.println("chrome"); 
        delay(600);
        Keyboard.println("https://youtu.be/dQw4w9WgXcQ"); 
           delay(3000);
        break;
         
    }
 


  }

  else {
        if (digitalRead(10) == LOW){
           Keyboard.press(KEY_LEFT_GUI); 
        Keyboard.press('r'); 
        Keyboard.release(KEY_LEFT_GUI); 
        Keyboard.release('r'); 
        delay(50); //give your system time to catch up with these android-speed keyboard presses
        Keyboard.println("chrome"); 
        delay(600);
        Keyboard.println("https://youtu.be/dQw4w9WgXcQ");
         delay(3000);
          }

         else if (digitalRead(15) == LOW){
           Keyboard.press(KEY_LEFT_GUI); 
        Keyboard.press('r'); 
        Keyboard.release(KEY_LEFT_GUI); 
        Keyboard.release('r'); 
        delay(50); //give your system time to catch up with these android-speed keyboard presses
        Keyboard.println("chrome"); 
        delay(600);
        Keyboard.println("https://youtu.be/dQw4w9WgXcQ");
         delay(3000);
          }

         else if (digitalRead(16) == LOW){
           Keyboard.press(KEY_LEFT_GUI); 
        Keyboard.press('r'); 
        Keyboard.release(KEY_LEFT_GUI); 
        Keyboard.release('r'); 
        delay(50); //give your system time to catch up with these android-speed keyboard presses
        Keyboard.println("chrome"); 
        delay(600);
        Keyboard.println("https://youtu.be/dQw4w9WgXcQ");
         delay(3000);
          }

          else if (digitalRead(14) == LOW){
           Keyboard.press(KEY_LEFT_GUI); 
        Keyboard.press('r'); 
        Keyboard.release(KEY_LEFT_GUI); 
        Keyboard.release('r'); 
        delay(50); //give your system time to catch up with these android-speed keyboard presses
        Keyboard.println("chrome"); 
        delay(600);
        Keyboard.println("https://youtu.be/dQw4w9WgXcQ");
         delay(3000);
          }

         
  }
  

    Keyboard.releaseAll(); // this releases the buttons
     delay(100);
  
}

ankur_macro_keyboard.ino (4.5 KB)

You only need one pinMode per pin

but on my Pro Micro, 14 and A0 are different pins

Could please add a debounce function to the code?

don't see any code relayed to debounce in the posted code. i have found a 10 msec delay is usually adequate after a button change of state. (of course there may be hard real-time cases where other approaches may be needed).

how do i implement it in my code?

are they both the same pins as it seems the code is running infinitely?

First, can you please tell us WHAT COMPONENTS you are using? i.e. which arduino and which keyboard and how they are connected?

I have no idea why you are doing this

 while (digitalRead(A0) == HIGH) {
    // do nothing until pin 2 goes low
    delay(500);
  }

instead of looking for a key press on the keypad.

for debouncing I could not put this better myself:

Let's be clear about this. If you have a keypad matrix you are already using processing power to apply sequential logic voltages to the rows or columns then reading the columns or rows back in order to determine the button pressed.

So, each time you get a "result" i.e. you detect that a button has been pressed, you mark that event as "pending" and some time later (10 to 20 ms) you check again to see if the button press you marked as "pending" can be judged to be "actual".

I'd suggest you start by looking here
https://playground.arduino.cc/Main/KeypadTutorial/

Hi,
Have you built your project?
Does your code work?
What are you using as keyboard switches?

Can you please post a copy of the schematic of your project?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

I am using a generic 4X4 keypad connected to an Arduino Micro.

and I am doing that while loop as a physical kill switch which I can pull in case things go wrong and it starts spamming stuff when connected to the computer.

I just followed the schematic from here: The Simplest DIY Macro Keypad with Arduino - YouTube

but mainly I spent days polishing the code as the code from the video Is bad and has errors for many setups and now it's finally done and working according to my needs.

wait, what does this mean? is this a warning to me or the other guys replying to me?

It is a polite request for you to follow the rules of the forum.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.