Pages: [1]   Go Down
Author Topic: teensyduino code  (Read 1356 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();


}
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 169
Posts: 12448
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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();
  }
}
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 13
Posts: 779
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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)
Logged


Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 169
Posts: 12448
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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();


}
 smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin smiley-grin
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46063
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    //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...
Logged

Pages: [1]   Go Up
Jump to: