I have had an arduino for a year now and just got a teensyduino 2.0.
I am still having a hard time with the code.
I wanted to make a usb controller for my computer it wold have about ten buttons on it.
This is the code I found.
I wonted to add more buttons to the code but I cant.
Can some one help.
const int pinBtnUp = 0;
const int pinLEDOutput = 11;
//Variables for the states of the SNES buttons
boolean boolBtnUp;
void setup()
{
//Setup the pin modes.
pinMode( pinLEDOutput, OUTPUT );
//Special for the Teensy is the INPUT_PULLUP
//It enables a pullup resitor on the pin.
pinMode( pinBtnUp, INPUT_PULLUP );
//Zero the SNES controller button keys:
boolBtnUp = false;
//Progess the SNES controller buttons to send keystrokes.
fcnProcessButtons();
}
//Function to process the buttons from the SNES controller
void fcnProcessButtons()
{
//Assign temporary values for the buttons.
//Remember, the SNES buttons are read as active LOW.
//Capture their status here:
boolean boolBtnUp = !digitalRead(pinBtnUp);
if ( boolBtnUp )
{
//Set key1 to the U key
Keyboard.set_key1( KEY_U );
} else {
Keyboard.set_key1( 0 );
}
Thanks but when I type U or D once they don't stop.
I press the button once and instead of there being one d it does this dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd.
If you want to be able to handle more than 6 buttons, the code will get a bit more complicated as the USB Keyboard protocol only allows registering up to 6 keypresses at the same time (this is why the Teensyduino keyboard class has six separate methods for issuing a keypress).
Up to 6 buttons can be done fairly easily by duplicating this block of code:
boolean boolBtnUp = !digitalRead(pinBtnUp);
if ( boolBtnUp )
{
//Set key1 to the U key
Keyboard.set_key1( KEY_U );
} else {
Keyboard.set_key1( 0 );
}
up to six times, changing the set_key# function that is called for each one, from set_key1() to set_key6(). As I said at the beginning though, writing code to handle 10 buttons becomes a non-trivial task (and you still would not be able to register more than 6 button presses at any one time)
I press the button once and instead of there being one d it does this dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd.
You need to reset the boolean to false when the key is send!
Are you debouncing your inputs (either with hardware or another method?). Pressing the button "once" it still bounces open and closed for a few milliseconds (perhaps up to a couple hundred)....in a loop that runs in micro seconds this looks like a whole ton of button presses.
This little web video explains this a bit and provides some details on various hardware debouncng strategies.