#include "Keyboard.h" // library Keyboard.h #define PIN_LED 15 //Declare LED on pin 15 #define PIN_BTN_1 5 //Declare BTN UP on pin 5 #define PIN_BTN_2 8 //Declare BTN LEFT on pin 8 #define PIN_BTN_3 14 // Declare BTN DOWN on pin 14 #define PIN_BTN_4 4 // Declare BTN RIGHT on pin 4 #define PIN_BTN_5 31 // Declare BTN SPACEBAR on pin 31 #define N_BTN 5 // Declare Num BTN int pin_btns[] = {PIN_BTN_1, PIN_BTN_2, PIN_BTN_3, PIN_BTN_4, PIN_BTN_5}; //ARRAY OF PIN_BTN byte key_btns[] = {KEY_UP_ARROW,KEY_LEFT_ARROW,KEY_DOWN_ARROW,KEY_RIGHT_ARROW,' '}; //declare function of buttons int state_btns[] = {false,false,false,false,false}; //Button status creation in the down position int state_btns_new[] = {false,false,false,false,false}; //Creation of a 2nd state of the buttons in the down position for comparison void setup() { pinMode(PIN_LED, OUTPUT); for (int i=0; i < N_BTN; i++) { pinMode(pin_btns[i], INPUT_PULLUP); } Keyboard.begin(); SerialUSB.begin(0); } void loop() { int val; //Creation of a "val" to insert the number of the activated button bool state; //Creation of "state" with boolean type 0/1 bool led=false; //Creation of a boolean value to set the led to false for (int i = 0; i < N_BTN; i++) { val = digitalRead(pin_btns[i]); //In 'val' we will put read the position of the engaged button state_btns_new[i] = !val;The inverse of 'val' is assigned in 'state_btns_new'. } for (int i = 0; i < N_BTN; i++) { if (state_btns_new[i] && !state_btns[i]){ Keyboard.press(key_btns[i]); } if (!state_btns_new[i] && state_btns[i]){ Keyboard.release(key_btns[i]); } led |= state_btns_new[i]; //if true LED ON else OFF state_btns[i] = state_btns_new[i]; } digitalWrite(PIN_LED, led); delay(1); }