External game controller with ProMicro

From Notebook
Jump to: navigation, search

Use

This was used to create an external button controllers for a game. 4 soft buttons using conductive fabric and fleece are connected to a Sparkfun ProMicro board. The code enables the computer to read the promicro as an external keyboard (human interface device, HID). Each button corresponds to a key on the keyboard which control the players movements in the game (WASD in this case). The button for 'R' restarts the game.

Code

#include <Keyboard.h>

int wait = 0;

void setup() {

pinMode(2,INPUT_PULLUP);

pinMode(3,INPUT_PULLUP);

pinMode(4,INPUT_PULLUP);

pinMode(5,INPUT_PULLUP);

pinMode(6,INPUT_PULLUP);

Serial.begin(9600); } void loop() { Keyboard.begin();

if (digitalRead(2) == 0)

 {
   Keyboard.write('a'); 
   delay(wait);          
 }

if (digitalRead(3) == 0)

 {
   Keyboard.write('d');  
   delay(wait);          
 }

if (digitalRead(4) == 0)

 {
   Keyboard.write('w');
   delay(wait);           
 }

if (digitalRead(5) == 0){

   Keyboard.write('s');
   delay(wait);

}

if (digitalRead(6) == 0)

 {
   Keyboard.write('r');
   delay(wait);           
 }
 

Keyboard.end(); }

References