Arduino Leonardo, keyboard pressing script pressing random buttons

Basically im quite new to ardunio and want to make a button pad were i can press buttons and they perform macro like functions in game for example. I press button 1 and it inputs Ctrl Shift A which is bound ingame to something. have made this script but it seems to just input KL= constantly into the computer without any buttons being pressed im not sure if I have coded anything wrong it compiles fine but im unsure. I have tried to use prints to see if its using any of the statements but nothing shows up ?
Thank you

#include <Keyboard.h>
char ctrlKey = "KEY_LEFT_CTRL";
char shiftKey = "KEY_RIGHT_SHIFT";



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  setupPins();
}

void setupPins(void){

  for (int i = 2; i <= 12; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
  pinMode(A0, INPUT);
  digitalWrite(A0, HIGH);
  pinMode(A1, INPUT);
  digitalWrite(A1, HIGH);
  pinMode(A2, INPUT);
  digitalWrite(A2, HIGH);
  pinMode(A3, INPUT);
  digitalWrite(A3, HIGH);
}
void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(2) == LOW) {
    Serial.println(digitalRead(2));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('a');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(3) == LOW) {
    Serial.println(digitalRead(3));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('b');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(4) == LOW) {
    Serial.println(digitalRead(4));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('c');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(5) == LOW) {
    Serial.println(digitalRead(5));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('d');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(6) == LOW) {
    Serial.println(digitalRead(6));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('e');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(7) == LOW) {
    Serial.println(digitalRead(7));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('f');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(8) == LOW) {
    Serial.println(digitalRead(8));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('g');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(9) == LOW) {
    Serial.println(digitalRead(9));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('h');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(10) == LOW) {
    Serial.println(digitalRead(10));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('i');
    delay(100);
    Keyboard.releaseAll();
    
  };
  if (digitalRead(11) == LOW) {
    Serial.println(digitalRead(11));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('j');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(12) == LOW) {
    Serial.println(digitalRead(12));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('k');
    delay(100);
    Keyboard.releaseAll();
  };

  if (digitalRead(13) == LOW) {
    Serial.println(digitalRead(13));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('l');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(A0) == LOW) {
    Serial.println(digitalRead(A0));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('m');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(A1) == LOW) {
    Serial.println(digitalRead(A1));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('n');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(A2) == LOW) {
    Serial.println(digitalRead(A2));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('o');
    delay(100);
    Keyboard.releaseAll();
  };
  if (digitalRead(A3) == LOW) {
    Serial.println(digitalRead(A3));
    Keyboard.press(ctrlKey);
    Keyboard.press(shiftKey);
    Keyboard.press('p');
    delay(100);
    Keyboard.releaseAll();
  };

}

Welcome to the forum

How are the inputs wired ?

It looks like you are trying to use the built in pullup resistors for the inputs. If so then simplify your code in setupPins()

void setupPins()
{
  for (int i = 2; i <= A3; i++)
  {
    pinMode(i, INPUT_PULLUP);
  }
}

Here is a picture of the switch im using and its wired as shown in the picture to the ardunio. Im AFK at the moment so will test your fix when im home.
Thank you

Here is the link to ali expressAli Express

THANK YOU THAT WAS THE ISSUE

It is not a fix for your problem, just a better way of setting the pinMode()s

The picture shows nothing about how the switches are wired. You need to wire them to take the pin to GND when the switch is closed

Well changing that seems to have fixed the issue. I don't know haha but yeah when the switch is pushed up or down it closes the circuit. This is how I understood it to work hence the GRD wire being connected to the center this allows me to use 2 inputs pins for one button. Sorry if I don't understand what you mean but it works now. does this make sense

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