Button Pressing

Hey I'm trying to make this code where it pretty much when I press the button (Pin 2) it prints the letter n.
Then when I press it again it prints it again but I'm having a bit of trouble right now the pin isn't even connected and it just keeps non stop printing n

#include <Keyboard.h>
void setup()
{
    pinMode(2,INPUT);
    digitalWrite(2,LOW);
    digitalWrite(2,HIGH);

    Keyboard.begin();

    while(digitalRead(2));
}

void loop()
{
    Keyboard.println("n");
    delay(1000);
    Keyboard.releaseAll();
    
}

Can you adapt this example to achieve the desired result:

Thanks But how do I add multiple buttons to different pins how would I write that?

#include <Keyboard.h>
void setup() {
  // make pin 2 an input and turn on the 
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button is pressed
  if(digitalRead(2)==LOW)  {
    //Send the message
    Keyboard.print("Hello!");
    delay(1500);
  }
}

Simplest way (for two buttons)

#include <Keyboard.h>
void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button on pin 2 is pressed
  if(digitalRead(2)==LOW)  {
    //Send the message
    Keyboard.print("Hello!");
    delay(1500);
  }
  //if the button on pin 3 is pressed
  if(digitalRead(3)==LOW)  {
    //Send the message
    Keyboard.print("World!");
    delay(1500);
  }
}

It's crude but will work. How many buttons?

how do I add multiple buttons to different pins how would I write that?

Put the pin numbers in an array and iterate through that. When you find that one of the buttons has become pressed, note become pressed not is pressed then use the index number of the button pin to access an array of values to be sent.

As an example, this illustrates what I described above but needs to be changed to slow down the repetition rate (do NOT use delay()) or to detect when a button becomes pressed, see the StateChangeDetection example in the IDE.

void loop()
{
  for (int pin = 0; pin < 3; pin++)
  {
    if (digitalRead(buttonPins[pin]) == LOW)
    {
      Serial.println(letters[pin]);
    }
  }
}

sterretje:
Simplest way (for two buttons)

#include <Keyboard.h>

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button on pin 2 is pressed
  if(digitalRead(2)==LOW)  {
    //Send the message
    Keyboard.print("Hello!");
    delay(1500);
  }
  //if the button on pin 3 is pressed
  if(digitalRead(3)==LOW)  {
    //Send the message
    Keyboard.print("World!");
    delay(1500);
  }
}



It's crude but will work. How many buttons?

Thanks it worked but for some reason when I try to make it, it doesn't work could you please make the file with 11 buttons.

Thanks it worked but for some reason when I try to make it, it doesn't work

So it worked but it did not work ???

Please post the code that you tried, but if you have 11 buttons then 11 copies of the same piece of code is not the way to do it. See my previous post suggesting using arrays.

That's what I used

#include <Keyboard.h>
void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
    //if the button on pin 2 is pressed
  if(digitalRead(2)==LOW){
    //Send the message
    Keyboard.print("Hello!");
    delay(1500);
}
    //if the button on pin 3 is pressed
    if(digitalRead(3)==LOW){
    //Send the message
    Keyboard.print("World!");
    delay(1500); }
}

Did you get any error messages when you compiled it or just no output ?

How are the inputs wired ?

UKHeliBob:
Did you get any error messages when you compiled it or just no output ?

How are the inputs wired ?

They way its wired is fine the problem is when I copied and pasted

//if the button on pin 3 is pressed
if(digitalRead(3)==LOW){
//Send the message
Keyboard.print("World!");
delay(1500); }
}

It gave me

sketch_nov14b:25: error: expected unqualified-id before 'if'

if(digitalRead(3)==LOW){

^

sketch_nov14b:29: error: expected declaration before '}' token

}

^

exit status 1
expected unqualified-id before 'if'

It compiles OK for me if I set the board to be a Leonardo

Here is the code laid out slightly differently but fundamentally unchanged

#include <Keyboard.h>
void setup()
{
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
}

void loop()
{
  //if the button on pin 2 is pressed
  if (digitalRead(2) == LOW)
  {
    //Send the message
    Keyboard.print("Hello!");
    delay(1500);
  }
  //if the button on pin 3 is pressed
  if (digitalRead(3) == LOW)
  {
    //Send the message
    Keyboard.print("World!");
    delay(1500);
  }
}

Try copy/pasting it into an empty IDE window and compiling it

The solution for anything over three buttons is to start using a struct or class. Below uses a struct.

struct BUTTONACTION
{
  byte pin;
  char *txt;
  bool addNewline;
};

A struct is like a record in a phone book with e.g. first name, last name and phone number. The above links a pin (where the button is connected) to a specific text (that will be 'printed' when the button is pressed). The addNewline allows you to send a new line after the text is 'printed'. Keyboard.println() does not seem to send the cursor to the beginning of a new line).

Next you can declare an array with an entry for each button action.

BUTTONACTION buttonActions[] =
{
  {2, "Hello", false},  // pin 2, text to 'print', do not send KEY_RETURN
  {3, "World", true},  // pin 3, text to 'print', send KEY_RETURN
  // add more here
};

In setup(), you can set all the pins to INPUT_PULLUP.

void setup()
{
  // start keyboard functionality
  Keyboard.begin();

  //setup all button pins for INPUT_PULLUP; buttons need to be wired between pin and ground
  for (int cnt = 0; cnt < sizeof(buttonActions) / sizeof(buttonActions[0]); cnt++)
  {
    pinMode(buttonActions[cnt].pin, INPUT_PULLUP);
  }
}

And in loop you can loop through the array

void loop()
{
  static int buttonNumber = 0;

  if(digitalRead(buttonActions[buttonNumber].pin) == ISPRESSED)
  {
    Keyboard.print(buttonActions[buttonNumber].txt);
    if(buttonActions[buttonNumber].addNewline== true)
    {
        Keyboard.write(KEY_RETURN);
    }
    
    // a crude debounce
    delay(100);

    // wait for button to be released
    while (digitalRead(buttonActions[buttonNumber].pin) == ISPRESSED)
    {
      // do nothing
    }
  }

  // next button
  buttonNumber++;
  if(buttonNumber == sizeof(buttonActions) / sizeof(buttonActions[0]))
  {
    buttonNumber = 0;
  }
}

Compiles, not tested.