/* ButtonKeyboardControl as GamePad for Game "Frets On Fire" Controls the Keyboard from five pushbuttons on an Arduino Leonardo. Hardware: * 5 pushbuttons attached to D3, D4, D5, D6, D7, Direction Key map to D8 * Return key and Escape maped by Joystick, and attach to A2 adn A1 The Keyboard movement is always relative. This sketch reads four pushbuttons, and uses them to set the movement of the Keyboard. IMPORTANT NOTE: Frets On Fire Key Setting: Fret #1 - w Fret #2 - s Fret #3 - a Fret #4 - e Fret #5 - h Tick - Return Scondary Tick - d //It needed!! Left - q Right - r up - u down - d Quit - q WARNING: When you use the Keyboard.move() command, the Arduino takes over your Keyboard! Make sure you have control before you use the Keyboard commands. created 15 Nov 2012 modified 15 Nov 2012 by Jose this code is in the public domain */ int ButtonState1; int ButtonState2; int ButtonState4; int ButtonState3; int ButtonState5; int returnKey; int DirectionKey; int TickKey; // set pin numbers for the five buttons: const int Button5 = 3; const int Button4 = 4; const int Button3 = 5; const int Button2 = 6; const int Button1 = 7; const int Return = 8; const int TickX = A1; const int TickY = A2; int range = 5; // output range of X or Y movement; affects movement speed int responseDelay = 10; // response delay of the Keyboard, in ms void setup() { // initialize the buttons' inputs: pinMode(Button1, INPUT); pinMode(Button2, INPUT); pinMode(Button3, INPUT); pinMode(Button4, INPUT); pinMode(Button5, INPUT); pinMode(Return, INPUT); pinMode(TickX, INPUT); pinMode(TickY, INPUT); digitalWrite(Button1, HIGH); digitalWrite(Button2, HIGH); digitalWrite(Button3, HIGH); digitalWrite(Button4, HIGH); digitalWrite(Button5, HIGH); digitalWrite(Return, HIGH); digitalWrite(TickX, HIGH); digitalWrite(TickY, HIGH); // initialize Keyboard control: Keyboard.begin(); } void loop() { int buttonBuff = digitalRead(Button1); if(ButtonState1 != buttonBuff) { ButtonState1 = buttonBuff; if (ButtonState1 == LOW) Keyboard.press('w'); else Keyboard.release('w'); } //s buttonBuff = digitalRead(Button2); if(ButtonState2 != buttonBuff) { ButtonState2 = buttonBuff; if (ButtonState2 == LOW) Keyboard.press('s'); else Keyboard.release('s'); } //d buttonBuff = digitalRead(Button4); if(ButtonState4 != buttonBuff) { ButtonState4 = buttonBuff; if (ButtonState4 == LOW) Keyboard.press('e'); else Keyboard.release('e'); } //a buttonBuff = digitalRead(Button3); if(ButtonState3 != buttonBuff) { ButtonState3 = buttonBuff; if (ButtonState3 == LOW) Keyboard.press('a'); else Keyboard.release('a'); } /////////////////////////////////// //A buttonBuff = digitalRead(Button5); if(ButtonState5 != buttonBuff) { ButtonState5 = buttonBuff; if (ButtonState5 == LOW) Keyboard.press('h'); else Keyboard.release('h'); } //A buttonBuff = digitalRead(Return); if(returnKey != buttonBuff) { returnKey = buttonBuff; if (returnKey == LOW) Keyboard.press('\n'); else Keyboard.release('\n'); } //Left & Right buttonBuff = analogRead(TickX); Serial.println(buttonBuff); if(buttonBuff != DirectionKey) { DirectionKey = buttonBuff; if(DirectionKey > 650) Keyboard.press('q'); else Keyboard.release('q'); if(DirectionKey < 450) Keyboard.press('r'); else Keyboard.release('r'); } //Up & Down buttonBuff = analogRead(TickY); Serial.println(buttonBuff); if(buttonBuff != TickKey) { TickKey = buttonBuff; if(TickKey > 700) // Tick Key Keyboard.press('d'); else Keyboard.release('\d'); if(TickKey < 300) // Quit Key Keyboard.press('u'); else Keyboard.release('u'); } // a delay so the Keyboard doesn't move too fast: delay(responseDelay); }