Need Help Cant figure this out

Hi Im trying to make a keyboard emulator with my Leonardo but im trying to add a Else if statement

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

    Keyboard.begin();

    while(digitalRead(2))
    {
    }

    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.write(48); //character 0
    delay(2500);
    Keyboard.press(KEY_BACKSPACE);
    delay(500);
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.releaseAll();
}
void loop()
{
}

If anyone could help me that would be great!

Im trying to wire this up to a button so when you press the button it prints what i want I know how to change that part its just that when i lift off and press again it doesn’t print a second time

setup() does not repeat. loop() repeats. Once your message is sent the first time from setup() it will never be sent again and you sketch will be running the empty loop() over, and over, and over...

So how would i write that like this?

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

    Keyboard.begin();

    while(digitalRead(2))
    {
    }

    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.write(48); //character 0
    delay(2500);
    Keyboard.press(KEY_BACKSPACE);
    delay(500);
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.releaseAll();
}
void loop()
void setup()
{
}

Hi I have tried may someone add a loop for me and fix my code up

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

    Keyboard.begin();

    while(digitalRead(2))
    {
    }
void loop(
    
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.write(48); //character 0
    delay(2500);
    Keyboard.press(KEY_BACKSPACE);
    delay(500);
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.println("HELLO WORLD");
    delay(1000);
    Keyboard.println("I AM ALIVE");
    delay(1000);
    Keyboard.releaseAll();
    )
}
{
}

Threads merged.

Even if someone were willing (which is doubtful), no one but you knows what it's supposed to do.

Neither of your codes compile on my system. For the code in reply #2, get rid of the extra line void setup() so it looks lik this

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

  Keyboard.begin();

  ...
  ...
}

void loop()
{
}

The setup() function is only executed once. The code in loop() is executed repeatedly; that’s what @johnwasser already indicated.

You will have to move the checking of the button as well as the sending of the keystrokes to the loop() function.

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

  Keyboard.begin();

}
void loop()
{
  ...
  ...
}

Why are you using digitalWrites on an input pin in setup()? Do you know what it’s doing? I will like an answer on that question; if you don’t understand, we can explain to you.

I suggest that you remove those digitalWites and change the pinMode to

pinMode(2, INTERNAL_PULLUP);

Now you need to wire the button between pin and GND and remove external pull-down resistor (if you used it).

In loop() you can now check for LOW to determine if the button is pressed and for HIGH if the button is not pressed. Your

while (digitalRead(2))

is correct for that. As long as the button is not pressed, it will read HIGH and be stuck in the while().

Last advice: get rid of magic numbers. Define/declare a pin in the beginning of your code and replace the hard-coded ‘2’ in pinMode and digitalRead by the name of the pin.

Look like what you wanted to do is something like:

#include <Keyboard.h>

const byte ButtonPin = 2;

void setup() {
  pinMode(ButtonPin, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {

  while (digitalRead(ButtonPin))
  {
    // Do nothing while button is not pressed
  }

  Keyboard.println("HELLO WORLD");
  delay(1000);
  Keyboard.println("I AM ALIVE");
  delay(1000);
  Keyboard.write(48); //character 0
  delay(2500);
  Keyboard.press(KEY_BACKSPACE);
  delay(500);
  Keyboard.println("HELLO WORLD");
  delay(1000);
  Keyboard.println("I AM ALIVE");
  delay(1000);
  Keyboard.println("HELLO WORLD");
  delay(1000);
  Keyboard.println("I AM ALIVE");
  delay(1000);
  Keyboard.releaseAll();
}