12 Button Macro box

Thought I'd start a fresh entirely and hope this outlines what it is i'm trying to do.

I am trying to make a macro box. I have wired 12 arcade style buttons to a pro micro board using pins 2,3,4,5,6,7,8,9,10,16,14,15 - They are soldered directly to the board. Nothing in between.

What I require it to do -

As I am a twitch stream I require to change screens on a piece of software called OBS. Now to make sure I don't mess up with any current buttons that are use I want the buttons to use F13 up to F24. So When I press 1 button which for example is pin 2 it presses F13 and it will push that through to the shortcuts window on OBS

Currently this is my code

#include <Keyboard.h>

const int keys[] = {2,3,4,5,6,7,8,9,10,16,14,15};
void setup() {
Keyboard.begin();
for (int i = 2; i < 11; ++i) {
pinMode(i, INPUT);
}
}

void loop() {
  for(int i = 2; i < 16; ++i){
    if(readButton(i)){
      doAction(i);
      }
    }
  }

boolean readButton(int pin){
  if (digitalRead(pin) == HIGH){
    delay(10);
    if(digitalRead(pin) == HIGH){
      return true;
      
    }
  }
  return false;
}

void doAction(int pin){
  switch(pin){
    case 2:
    Keyboard.press(KEY_F13);
    delay(100);
    Keyboard.releaseAll();
    case 3:
    Keyboard.press(KEY_F14);
    delay(100);
    Keyboard.releaseAll();
    case 4:
    Keyboard.press(KEY_F15);
    delay(100);
    Keyboard.releaseAll();
    case 5:
    Keyboard.press(KEY_F16);
    delay(100);
    Keyboard.releaseAll();
    case 6:
    Keyboard.press(KEY_F17);
    delay(100);
    Keyboard.releaseAll();
    case 7:
    Keyboard.press(KEY_F18);
    delay(100);
    Keyboard.releaseAll();
    case 8:
    Keyboard.press(KEY_F19);
    delay(100);
    Keyboard.releaseAll();
    case 9:
    Keyboard.press(KEY_F20);
    delay(100);
    Keyboard.releaseAll();
    case 10:
    Keyboard.press(KEY_F21);
    delay(100);
    Keyboard.releaseAll();
    case 16:
    Keyboard.press(KEY_F22);
    delay(100);
    Keyboard.releaseAll();
    case 14:
    Keyboard.press(KEY_F23);
    delay(100);
    Keyboard.releaseAll();
    case 15:
    Keyboard.press(KEY_F24);
    delay(100);
    Keyboard.releaseAll();
  }
}

Problem: I don't even have to press a button I hover over it and it starts firing ALL the buttons. And when I say hover I literally mean hover over the buttons not even touching them!

Your inputs are floating. Read Nick Gammon's helpful notes on how to wire a switch.

arduarn:
Your inputs are floating. Read Nick Gammon's helpful notes on how to wire a switch.

How do you mean Floating?

Rydema:
How do you mean Floating?

Did you read Nick's page?
Floating as in not-connected and acting like an antenna. You need either a pull-up or pull-down resistor (or the MCU internal pull-up) to give an input pin a definite value.

arduarn:
Did you read Nick's page?
Floating as in not-connected and acting like an antenna. You need either a pull-up or pull-down resistor (or the MCU internal pull-up) to give an input pin a definite value.

Ok so i did a bit of research using a video quickly. and tried changing all my inputs over to INPUT_PULLUP

And i've also stripped the code back to basic to see if I still have the same problem....

#include <Keyboard.h>

//Defining Buttons to pins
int Button1 = 2;
int Button2 = 3;
int Button3 = 4;
int Button4 = 5;
int Button5 = 6;
int Button6 = 7;
int Button7 = 8;
int Button8 = 9;
int Button9 = 10;
int Button10 = 16;
int Button11 = 14;
int Button12 = 15;


void setup(){

  //Starting Keyboard Mode
  Keyboard.begin();
  
  //Pin Mode
  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);
  pinMode(Button3, INPUT_PULLUP);
  pinMode(Button4, INPUT_PULLUP);
  pinMode(Button5, INPUT_PULLUP);
  pinMode(Button6, INPUT_PULLUP);
  pinMode(Button7, INPUT_PULLUP);
  pinMode(Button8, INPUT_PULLUP);
  pinMode(Button9, INPUT_PULLUP);
  pinMode(Button10, INPUT_PULLUP);
  pinMode(Button11, INPUT_PULLUP);
  pinMode(Button12, INPUT_PULLUP);

  //Digital Write 
  digitalWrite(Button1, HIGH);
  digitalWrite(Button2, HIGH);
  digitalWrite(Button3, HIGH);
  digitalWrite(Button4, HIGH);
  digitalWrite(Button5, HIGH);
  digitalWrite(Button6, HIGH);
  digitalWrite(Button7, HIGH);
  digitalWrite(Button8, HIGH);
  digitalWrite(Button9, HIGH);
  digitalWrite(Button10, HIGH);
  digitalWrite(Button11, HIGH);
  digitalWrite(Button12, HIGH);
}

void loop(){
  if (digitalRead(Button1) == 0){
    Keyboard.press(KEY_F13);
    delay(100);
    Keyboard.releaseAll();
  }
  }

Turns out it stops working altogether now

You don't need the digitalWrites since the pinMode INPUT_PULLUP already sets the pins high.

The problem is that you have soldered your switches between 5V and the input pin. A digitalRead will always return HIGH.

You need to keep the pull-ups enabled, but connect your switches between the input pin and GND. Then when you press the button it will pull the voltage towards GND, ie. LOW.
See Nick's page section "Internal pull-up resistor".

arduarn:
You don't need the digitalWrites since the pinMode INPUT_PULLUP already sets the pins high.

The problem is that you have soldered your switches between 5V and the input pin. A digitalRead will always return HIGH.

You need to keep the pull-ups enabled, but connect your switches between the input pin and GND. Then when you press the button it will pull the voltage towards GND, ie. LOW.
See Nick's page section "Internal pull-up resistor".

Just a quick yes or no to my brain wave....

These buttons have two pins...... solder wires to 2nd pin, tin them all together and GND? would that work?

If they have two pins then you must have already soldered both, otherwise you wouldn't have a circuit...

Just resolder one switch and test before doing all of them.

Rydema:
i've also stripped the code back to basic to see if I still have the same problem....

That's nowhere near basic enough.

Forget about the keyboard emulation for now. You need to learn the basics of how to use a switch before you can have any hope at success with this project. The keyboard thing just adds an unnecessary extra complexity. I recommend following this tutorial:
https://www.arduino.cc/en/Tutorial/InputPullupSerial
The Pro Micro doesn't have an LED on pin 13 like the Arduino boards that tutorial was written for. Here is a modified version of the the sketch for that tutorial that will use the RX pin on your board instead:
const byte LEDpin = LED_BUILTIN_RX;
void setup() {
//start serial connection
Serial.begin(9600);
//configure pin 2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(LEDpin, OUTPUT);

}

void loop() {
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
//print out the value of the pushbutton
Serial.println(sensorVal);

// Keep in mind the pull-up means the pushbutton's logic is inverted. It goes
// HIGH when it's open, and LOW when it's pressed. Turn on LEDpin when the
// button's pressed, and off when it's not:
if (sensorVal == HIGH) {
digitalWrite(LEDpin, LOW);
} else {
digitalWrite(LEDpin, HIGH);
}
}

arduarn:
If they have two pins then you must have already soldered both, otherwise you wouldn't have a circuit...

Just resolder one switch and test before doing all of them.

flips a table I'll be damned! That was is! I soldered the 2nd pins on the button to the ground and now the damn thing works correctly! Thank you!! I have also managed to solder up the last 4 buttons making a 16 button box now!