Macro Pad for Ardino UNO With 4x4 Membrane Switch Module

Introduction: Macro Pad for Ardino UNO With 4x4 Membrane Switch Module

So i made this Instruction cuz i found it nowhere on the Internet how to make an Macropad with the 4x4 Membrane Switch Module!

Supplies

1x 4x4Membrane Switch Module
1x Arduino uno
8x Jumper Cables

Step 1: Install Librarys

So first you have to Install all the Librarys
1. Keypad Library
2. Keyboard Library
How to install Librarys: https://www.arduino.cc/en/Guide/Libraries

Step 2: The Sketch

//Define buttons used.
#define KEY_LEFT_CTRL 1

#define KEY_F6 63 //Button 1
#define KEY_F7 64 //Button 2
#define KEY_F8 65 //Button 3
#define KEY_F13 104 //Button 4
#define KEY_F14 105 //Button 5
#define KEY_F15 106 //Button 6
#define KEY_F16 107 //Button 7
#define KEY_F17 108 //Button 8
#define KEY_F18 109 //Button 9
#define KEY_F19 110 //Button 10
#define KEY_F20 111 //Button 11
#define KEY_F21 112 //Button 12
#define KEY_F22 113 //Button 13
#define KEY_F23 114 //Button 14
#define KEY_F24 115 //Button 15
#define KEY_F25 116 //button 16

uint8_t buf[8] = {0}; //Keyboard timing buffer.

int state = 1; //Base state for reference.

void setup()
{
  Serial.begin(9600); //Baud rate for keyboard.
  
  //Define the pinMode for each pin.
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
  pinMode(7, INPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
 


  delay(200); //Wait before continuing on with the code.
}




#include 
#include 

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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

  
void loop(){
  char key = keypad.getKey();
  
    
  if (key == '1'){
    buf[2] = KEY_F18; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
  if (key == '2'){
    buf[2] = KEY_F19; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
  if (key == '3'){
    buf[2] = KEY_F20; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
  if (key == '4'){
    buf[2] = KEY_F21; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
  if (key == '5'){
    buf[2] = KEY_F22; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
  if (key == '6'){
    buf[2] = KEY_F23; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
  if (key == 'D'){
    buf[2] = KEY_F25; //Presses the F24 key.
    Serial.write(buf, 8); //Sends Keypress.
    releaseKey(); //Calls the releaseKey method.
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);  
   
  } 
}
void releaseKey()
{
  buf[0] = 0; //Resets number.
  buf[2] = 0; //Resets number.
  Serial.write(buf, 8); //Release key.
  delay(200);
}

Step 3: Connecting the Arduino to the Keypad

Using the diagram above as a reference the leftmost pin is pin 8 on the keypad and the rightmost is pin 1.

Pins 8, 7, 6, 5 on the keypad should be connected to digital pins 5, 4, 3, 2 on the Arduino respectively. Pins 4, 3, 2, 1 on the keypad should be connected to digital pins 9, 8, 7, 6 on the Arduino respectively.

Step 4: Changing the Code

You can ofcourse delete the

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(1000); its pretty much just for debugging purposes and it adds a delay of 2 seconds after every key press

Step 5: Flashing the Arduino

Arduino Uno is not made for using it as an USB HID device. With some tricks, however, you can make it work. You can do this by flashing custom keyboard firmware to the arduino with the program FLIP.

First of all you need to set your arduino to DFU mode. You can do this by connecting the 2 pins in the image for a split second (while connected to your pc). Then, you need to upload the "Arduino-keyboard-0.3.hex" file through flip. Press ctrl+u and click on 'open'. Then, press the 'load hex' button and select the file. Disconnect your arduino and reconnect it. Now it will be detected as a keyboard. You may notice you are not able to upload files to your arduino anymore. For this, you need to follow the last steps again but now with the "Arduino-usbserial-mega.hex". Do this every time you want to edit your code.

Be the First to Share

    Recommendations

    • Fruit and Veggies Speed Challenge

      Fruit and Veggies Speed Challenge
    • Metal Contest

      Metal Contest
    • Make it Fly Challenge

      Make it Fly Challenge

    Comments

    0
    mutz03 Zockt
    mutz03 Zockt

    Tip 1 year ago on Step 2

    The time for the Builtin LED is to high, I recomend to set it lower, 300 os a good value, and the second delay you dont need. so you can press a button every 300milliseconds, what is goot, in comperason to the two seconds.