Want to create the same ?

#003 - Numeric Keypad - mechanical keyboard based on arduino pro micro that you can directly connect to your pc

#003 - Numeric Keypad

  • PCB x 1
  • Rectifier diode 1N4148 100V / 0.15A - x 17
  • Keyswitch Cherry MX Blue - x 17
  • Arduino pro micro - x 1
  • Plexi case - bottom x 1
  • Distance bolt x 4
  • Screw x 4
  • Goldpins

Code


#include 
#include "Keyboard.h"

const byte ROWS = 5;
const byte COLS = 4;
const bool ENABLE_PULLUPS = true;  // make false if you are using external pull-ups
const unsigned long DEBOUNCE_TIME = 10;     // milliseconds
const unsigned long HEARTBEAT_TIME = 2000;  // milliseconds
const bool DEBUGGING = true;               // make true for human-readable output

// define here where each row and column is connected to
const byte rowPins[ROWS] = {9, 8, 7, 6, 5};
const byte colPins[COLS] = {15, A0, A1, A2};


// See: http://c-faq.com/misc/bitsets.html

#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITCLEAR(a, b) ((a)[BITSLOT(b)] &= ~BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))

// number of items in an array
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

// total number of keys
const byte TOTAL_KEYS = ROWS * COLS;

// remember previous setting of each key
char lastKeySetting [(TOTAL_KEYS + CHAR_BIT - 1) / CHAR_BIT];  // one bit each, 0 = up, 1 = down
unsigned long lastKeyTime [TOTAL_KEYS];       // when that key last changed
unsigned long lastHeartbeat;                  // when we last sent the heartbeat

void setup () {
  Serial.begin (115200);

  if (ENABLE_PULLUPS) {
    for (byte i = 0; i < COLS; i++) {
      pinMode (colPins [i], INPUT_PULLUP);
    }
  }

  Keyboard.begin();
}

void print_key(byte keyNumber) {
  Serial.println(keyNumber);
  if(keyNumber == 17) { Keyboard.print(0); }
  if(keyNumber == 12) { Keyboard.print(1); }
  if(keyNumber == 13) { Keyboard.print(2); }
  if(keyNumber == 14) { Keyboard.print(3); }
  if(keyNumber == 8) { Keyboard.print(4); }
  if(keyNumber == 9) { Keyboard.print(5); }
  if(keyNumber == 10) { Keyboard.print(6); }
  if(keyNumber == 4) { Keyboard.print(7); }
  if(keyNumber == 5) { Keyboard.print(8); }
  if(keyNumber == 6) { Keyboard.print(9); }
  if(keyNumber == 18) { Keyboard.print("."); }
  if(keyNumber == 0) {
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('t');
    delay(200);
    Keyboard.releaseAll();
    delay(300);
    Keyboard.print("gnome-calculator");
    Keyboard.print("\n");

  }
  if(keyNumber == 1) { Keyboard.print("/"); }
  if(keyNumber == 2) { Keyboard.print("*"); }
  if(keyNumber == 3) { Keyboard.print("-"); }
  if(keyNumber == 11) { Keyboard.print("+"); }
  if(keyNumber == 19) { Keyboard.print("\n"); }
}

void loop () {
  byte keyNumber = 0;
  unsigned long now = millis ();

  for (byte row = 0; row < ROWS; row++) {
    pinMode (rowPins [row], OUTPUT);
    digitalWrite (rowPins [row], LOW);

    // check each column to see if the switch has driven that column LOW
    for (byte col = 0; col < COLS; col++) {
      // debounce - ignore if not enough time has elapsed since last change
      if (now - lastKeyTime [keyNumber] >= DEBOUNCE_TIME) {
        bool keyState = digitalRead (colPins [col]) == LOW; // true means pressed
        if (keyState != (BITTEST (lastKeySetting, keyNumber) != 0)) {
          lastKeyTime [keyNumber] = now;  // remember time it changed
          if (keyState) {
            BITSET (lastKeySetting, keyNumber);
          } else {
            BITCLEAR (lastKeySetting, keyNumber);
          }


          if (keyState) {
            print_key(keyNumber);
          }


        }  // if key state has changed
      }  // debounce time up
      keyNumber++;
    } // end of for each column

    // put row back to high-impedance (input)
    pinMode (rowPins [row], INPUT);
  }

  // Send a heartbeat code (0xFF) every few seconds in case
  // the receiver loses an occasional keyup.
  // Only send if all keys are not pressed (presumably the normal state).
  if (now - lastHeartbeat >= HEARTBEAT_TIME) {
    lastHeartbeat = now;
    bool allUp = true;
    for (byte i = 0; i < ARRAY_SIZE (lastKeySetting); i++)
      if (lastKeySetting [i])
        allUp = false;

  }
}