You are on page 1of 2

https://www.scribd.

com/document/374788499/Arduino-Nano-as-Keyboard

d0,d1,d2,d4

/*BEGINNING OF NOTES

This is the basic code for a 2 key setup with LEDs.


This works 100% with the arduino Micro, Leonardo and Uno.
If you want a controller for Mania or other games, just add more pins to the const
int lines, add them as INPUTS in pinMode, and initialize the digitalRead function
in the loop!
As an example, lets say you wanted to add the M key as a new button.
You would add this to the existing code:

const int buttonM = 6;

pinMode(buttonM, INPUT);

if (digitalRead(buttonM) == LOW)
{
Keyboard.press('m');
}

else
{
Keyboard.release('m');
}

Don't forget to put them in the right place! If they are outside the correct loop,
it won't work!

END OF NOTES*/

#include<Keyboard.h> //You NEED this line


otherwise it won't work.

const int led1 = 5; //These rename the pins to


something more recognisable, you don't have to name them like this, I just do it
for practicality
const int led2 = 4;
const int buttonX = 3;
const int buttonZ = 2;

void setup() //This executes the next


commands only once
{
pinMode(led1, OUTPUT); //Set up the pins 4 and 5 as
output.
pinMode(led2, OUTPUT); //
pinMode(buttonX, INPUT); //Set up the pins 2 and 3 as
input.
pinMode(buttonZ, INPUT); //

Keyboard.begin(); //This allows control over


the keyboard.
}
void loop() //This repeats the next commands as
long as the Arduino is powered on.
{
if (digitalRead(buttonX) == LOW) //if the input reads that it
is activated,
{
Keyboard.press('x'); //press the 'x' key.
digitalWrite(led1, HIGH); //turn the first led on.
}

else //Otherwise,
{
Keyboard.release('x'); //release the 'x' key.
digitalWrite(led1, LOW); //turn the first led off.
}

if (digitalRead(buttonZ) == LOW) //if the input reads that it


is activated,
{
Keyboard.press('z'); //press the 'z' key.
digitalWrite(led2, HIGH); //turn the second led on.
}

else //Otherwise,
{
Keyboard.release('z'); //release the 'z' key.
digitalWrite(led2, LOW); //turn the second led off.
}
} //Don't forget this little
bracket ;)

You might also like