Macro Buttons

Last Updated or created 2023-01-06

A box with macro buttons for your PC using Arduino Leonardo

#include <Keypad.h> // matrix read
#include <Keyboard.h> // keyboard out

#define ENABLE_PULLUPS // fuck the resistors
#define NUMBUTTONS 25 // matrix
#define NUMROWS 5 // matrix rows
#define NUMCOLS 5 // matrix cols

int analog1 = A3;
int analog2 = A7;


int inPinctrl = 3;  // function ctrl, pull to vcc
int inPinalt = 4;  // function alt, pull to vcc 
int valctrl = 0;    // variable to store shifter
int valalt = 0;    // variable to store shifter
int joyx = 0;
int joyy = 0;

//define the symbols per key
char buttons[NUMROWS][NUMCOLS] = {
  {'q','w','e','r','t'},
  {'y','u','i','o','p'},
  {'a','s','d','f','g'},
  {'h','j','k','l','z'},
  {'x','c','v','b','n'},
};
// q-1 (star)      e-zoomin    r-prev t-up
// y-2 u-clearstar             o-open p-down
// a-3 (rate)      d-zoomreset f-full g-left
// h-4 j-clearrate             l-esc  z-right
// x-5 c-slideshow v=zoomout   b-next

//    14 16 10 9  8 
// 21 q  y  a  h  x  
// 20 w  u  s  j  c
// 19 e  i  d  k  v 
// 18 t  p  g  z  n
// 15 r  o  f  l  b 

byte rowPins[NUMROWS] = {16,20,19,18,15}; //connect to the row pinouts of the keypad
byte colPins[NUMCOLS] = {14,7,10,9,8}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS); 


void setup() {

  Serial.begin(115200); // debug out baud
  pinMode(inPinctrl, INPUT);    // sets the digital pin 3 as input
  pinMode(inPinalt, INPUT);    // sets the digital pin 4 as input
  digitalWrite(inPinctrl, HIGH);       // turn on pullup resistors
  digitalWrite(inPinalt, HIGH);       // turn on pullup resistors
  Keyboard.begin();        // keyb starter
}



void loop() { // loop the program

  CheckAllButtons(); // check tha buttons

}


void CheckAllButtons(void) {
  joyx = analogRead(analog1);
  joyy = analogRead(analog2);
  Serial.println(joyx);
  Serial.println(joyy);
  if (joyx > 900) { 
      Keyboard.press(KEY_UP_ARROW);
      delay(150);
      Keyboard.releaseAll();
  }
  if (joyx < 200) { 
      Keyboard.press(KEY_DOWN_ARROW);
      delay(150);
      Keyboard.releaseAll();      
  }
  if (joyy > 900) { 
      Keyboard.press(KEY_LEFT_ARROW);
      delay(150);
      Keyboard.releaseAll();
  }
  if (joyy < 200) { 
      Keyboard.press(KEY_RIGHT_ARROW);
      delay(150);
      Keyboard.releaseAll();
  }

  
  char key = buttbx.getKey();

  if (key != NO_KEY)  {
      valctrl = digitalRead(inPinctrl);   // read the function pin
      valalt = digitalRead(inPinalt);   // read the function pin
      Serial.write(valctrl);  // debug
      Serial.println();  // enter
      Serial.write(valalt);  // debug
      Serial.println();  // enter
      Serial.write(key);  // debug
      Serial.println();  // enter
      // button 1 
          if (key == 'q') {
            if (valctrl == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_CTRL); 
            }
            if (valalt == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_ALT); 
            }
           Keyboard.press('1');
           delay(150);
           Keyboard.releaseAll();
          }
      // button 2
          if (key == 'y') {
            if (valctrl == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_CTRL); 
            }
            if (valalt == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_ALT); 
            }
           Keyboard.press('2');
           delay(150);
           Keyboard.releaseAll();
          }
      // button 3
          if (key == 'a') {
            if (valctrl == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_CTRL); 
            }
            if (valalt == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_ALT); 
            }
           Keyboard.press('3');
           delay(150);
           Keyboard.releaseAll();
          }
      // button 4
          if (key == 'h') {
            if (valctrl == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_CTRL); 
            }
            if (valalt == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_ALT); 
            }
           Keyboard.press('4');
           delay(150);
           Keyboard.releaseAll();
          }
      // button 5
          if (key == 'x') {
            if (valctrl == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_CTRL); 
            }
            if (valalt == 0) { // function shifter active?
               Keyboard.press(KEY_LEFT_ALT); 
            }
           Keyboard.press('5');
           delay(150);
           Keyboard.releaseAll();
          }
      // button i - clear rate
          if (key == 'i') {
           Keyboard.press(KEY_LEFT_CTRL); 
           Keyboard.press('0');
           delay(150);
           Keyboard.releaseAll();
          }
      // button k - clear label
          if (key == 'k') {
           Keyboard.press(KEY_LEFT_ALT); 
           Keyboard.press('0');
           delay(150);
           Keyboard.releaseAll();
          }
      // button v - slideshow
          if (key == 'v') {
           Keyboard.press(KEY_ESC); 
           Keyboard.press(KEY_LEFT_CTRL); 
           Keyboard.press('s');
           delay(150);
           Keyboard.releaseAll();
          }
      // button t - zoomin
          if (key == 't') {
           Keyboard.press('+'); 
           delay(150);
           Keyboard.releaseAll();
          }
      // button g - zoomreset
          if (key == 'g') {
           Keyboard.press('*');
           delay(150);
           Keyboard.releaseAll();
          }
      // button n - zoomout
          if (key == 'n') {
           Keyboard.press('-'); 
           delay(150);
           Keyboard.releaseAll();
          }
//r  o  f  l  b
      // button r - prev
          if (key == 'r') {
           Keyboard.press(KEY_LEFT_CTRL); 
           Keyboard.press(KEY_LEFT_ARROW); 
           delay(150);
           Keyboard.releaseAll();
          }
           
      // button o - open
          if (key == 'o') {
           Keyboard.press(KEY_RETURN); 
           delay(150);
           Keyboard.releaseAll();
          }
           
      // button f - full
          if (key == 'f') {
           Keyboard.press('f'); 
           delay(150);
           Keyboard.releaseAll();
          }
           
      // button l - esc
          if (key == 'l') {
           Keyboard.press(KEY_ESC); 
           delay(150);
           Keyboard.releaseAll();
          }
           
      // button b - next
          if (key == 'b') {
           Keyboard.press(KEY_LEFT_CTRL); 
           Keyboard.press(KEY_RIGHT_ARROW); 
           delay(150);
           Keyboard.releaseAll();
          }
           





          
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *