Go Down

Topic: teensyduino code (Read 1 time) previous topic - next topic

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;

}


void loop()
{
//  //debugging the start button...
  digitalWrite ( pinLEDOutput, digitalRead(pinBtnUp));

  //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 );
  }
   
  //Send all of the set keys.
  Keyboard.send_now();


}

robtillaart

DOn't have a teensy but assuming that if your code works, this should give you insight how it is done.

(code not tested or compiled)
Code: [Select]

const int pinBtnUp   = 0;
const int pinBtnDown = 1;  // assumption pin 1 is the down

const int pinLEDOutput = 11;

//Variables for the states of the SNES buttons
boolean boolBtnUp = false;
boolean boolBtnDown = false;


void setup()
{
  pinMode( pinLEDOutput, OUTPUT );
  pinMode( pinBtnUp, INPUT_PULLUP );
  pinMode( pinBtnDown, INPUT_PULLUP );
}


void loop()
{
  digitalWrite ( pinLEDOutput, digitalRead(pinBtnUp));

  fcnProcessButtons();
}

void fcnProcessButtons()
{
  boolBtnUp   = !digitalRead(pinBtnUp);
  boolBtnDown = !digitalRead(pinBtnDown);

  if ( boolBtnUp )
  {
    Keyboard.set_key1( KEY_U );
Keyboard.send_now();
  }
  if ( boolBtnDown )
  {
    Keyboard.set_key1( KEY_D );
Keyboard.send_now();
  }
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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.

111swords

jraskell

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:
Code: [Select]

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)

robtillaart

Quote
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!
Code: [Select]

  if ( boolBtnUp )
  {
    Keyboard.set_key1( KEY_U );
    Keyboard.send_now();
    BoolBtnUp = false;
  }
  if ( boolBtnDown )
  {
    Keyboard.set_key1( KEY_D );
    Keyboard.send_now();
    boolBtnDown = false;
  }


give it a try
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

ds2ktj

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.


http://www.arduino.cc/en/Tutorial/Debounce

This little web video explains this a bit and provides some details on various hardware debouncng strategies.

http://jeremyblum.com/2011/03/07/arduino-tutorial-10-interrupts-and-hardware-debouncing/

I tried BoolBtnUp = false; but they just keep on coming.
This is way harder than I thought. :smiley-roll-sweat:

Thanks for all of your help I got it to work.

This is my finished code

const int pinBtnUp = 0;
const int pinBtnDown= 1;
const int pinLEDOutput = 11;


boolean boolBtnUp;
boolean boolBtnDown;

void setup()
{
  //Setup the pin modes.
  pinMode( pinLEDOutput, OUTPUT );
 
  pinMode( pinBtnUp, INPUT_PULLUP );
  pinMode( pinBtnDown, INPUT_PULLUP );
  //Zero the SNES controller button keys:
  boolBtnUp = false;
  boolBtnDown = false;
}


void loop()
{
//  //debugging the start button...
  digitalWrite ( pinLEDOutput, digitalRead(pinBtnUp));

 
  fcnProcessButtons();
 
}


void fcnProcessButtons()
{
 
  boolean boolBtnUp = !digitalRead(pinBtnUp);
  boolean boolBtnDown = !digitalRead(pinBtnDown);
  if ( boolBtnUp )
  {
    //Set key1 to the U key
    Keyboard.set_key1( KEY_U );
  } else {
    Keyboard.set_key1( 0 );
  }
    if ( boolBtnDown )
  {
    //Set key1 to the U key
    Keyboard.set_key2( KEY_D );
  } else {
    Keyboard.set_key2( 0 );
  }
     
  //Send all of the set keys.
  Keyboard.send_now();


}
:D :D :D :D :D :D :D :D :D :D :D

PaulS

Code: [Select]
    //Set key1 to the U key
    Keyboard.set_key2( KEY_D );

When you are going to have useless comments, at least they should be correct useless comments...

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy