Using Teensy LC for an esc button for retro pie

Status
Not open for further replies.
Please i need help! what would be the code to make pin 0 press the esc button once on teensy LC? i am making a super nintendo retro pie system and this will make the reset button on the super nintendo work. this is my first project and help is appreciated thank you.
 
You can download and install Arduino and Teensyduino, without buying anything.

After installing, run Arduino, and open the keyboard example, from File > Examples > Teensy > USB_Keyboard > Buttons. Instead of Keyboard.print() to type a whole message, you'd edit the code to use Keyboard.press(KEY_ESC) and Keyboard.release(KEY_ESC).

For testing, you can just touch a wire or paperclip between GND and pin 0. Hopefully that example code will make sense when you read it?
 
Wich do i change and to what? this is my first experience with programing

Buttons to USB Keyboard Example

You must select Keyboard from the "Tools > USB Type" menu

This example code is in the public domain.
*/

#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 10); // to rapid touch, you can
Bounce button7 = Bounce(7, 10); // increase this time.
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);

void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
}

void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();

// Check each button for "falling" edge.
// Type a message on the Keyboard when each button presses
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Keyboard.println("B0 press");
}
if (button1.fallingEdge()) {
Keyboard.println("B1 press");
}
if (button2.fallingEdge()) {
Keyboard.println("B2 press");
}
if (button3.fallingEdge()) {
Keyboard.println("B3 press");
}
if (button4.fallingEdge()) {
Keyboard.println("B4 press");
}
if (button5.fallingEdge()) {
Keyboard.println("B5 press");
}
if (button6.fallingEdge()) {
Keyboard.println("B6 press");
}
if (button7.fallingEdge()) {
Keyboard.println("B7 press");
}
if (button8.fallingEdge()) {
Keyboard.println("B8 press");
}
if (button9.fallingEdge()) {
Keyboard.println("B9 press");
}

// Check each button for "rising" edge
// Type a message on the Keyboard when each button releases.
// For many types of projects, you only care when the button
// is pressed and the release isn't needed.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button0.risingEdge()) {
Keyboard.println("B0 release");
}
if (button1.risingEdge()) {
Keyboard.println("B1 release");
}
if (button2.risingEdge()) {
Keyboard.println("B2 release");
}
if (button3.risingEdge()) {
Keyboard.println("B3 release");
}
if (button4.risingEdge()) {
Keyboard.println("B4 release");
}
if (button5.risingEdge()) {
Keyboard.println("B5 release");
}
if (button6.risingEdge()) {
Keyboard.println("B6 release");
}
if (button7.risingEdge()) {
Keyboard.println("B7 release");
}
if (button8.risingEdge()) {
Keyboard.println("B8 release");
}
if (button9.risingEdge()) {
Keyboard.println("B9 release");
}
}
 
Just looking at the last two messages I suggest you replace:
Code:
if (button0.fallingEdge()) {
Keyboard.println("B0 press");
}
with this:

Code:
if (button0.fallingEdge()) {
Keyboard.press(KEY_ESC);
}
And similarly with the release:

Code:
if (button0.risingEdge()) {
Keyboard.println("B0 release");
}
Becomes:
Code:
if (button0.risingEdge()) {
Keyboard.release(KEY_ESC);
}
 
This is what I'm getting

Arduino: 1.6.2 (Mac OS X), TD: 1.22, Board: "Teensy LC, Serial, 48 MHz, US English"

Buttons.pde: In function 'void loop()':
Buttons:68: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:71: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:74: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:77: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:80: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:83: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:86: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:89: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:92: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:95: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:105: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:108: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:111: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:114: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:117: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:120: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:123: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:126: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:129: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
Buttons:132: error: 'Keyboard' was not declared in this scope
To make a USB Keyboard, use the Tools > USB Type menu
'Keyboard' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
 
/* Buttons to USB Keyboard Example

You must select Keyboard from the "Tools > USB Type" menu

This example code is in the public domain.
*/

#include <Bounce.h>

// Create Bounce objects for each button. The Bounce object
// automatically deals with contact chatter or "bounce", and
// it makes detecting changes very simple.
Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
Bounce button2 = Bounce(2, 10); // which is appropriate for
Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
Bounce button4 = Bounce(4, 10);
Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
Bounce button6 = Bounce(6, 10); // to rapid touch, you can
Bounce button7 = Bounce(7, 10); // increase this time.
Bounce button8 = Bounce(8, 10);
Bounce button9 = Bounce(9, 10);

void setup() {
// Configure the pins for input mode with pullup resistors.
// The pushbuttons connect from each pin to ground. When
// the button is pressed, the pin reads LOW because the button
// shorts it to ground. When released, the pin reads HIGH
// because the pullup resistor connects to +5 volts inside
// the chip. LOW for "on", and HIGH for "off" may seem
// backwards, but using the on-chip pullup resistors is very
// convenient. The scheme is called "active low", and it's
// very commonly used in electronics... so much that the chip
// has built-in pullup resistors!
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP); // Teensy++ LED, may need 1k resistor pullup
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
}

void loop() {
// Update all the buttons. There should not be any long
// delays in loop(), so this runs repetitively at a rate
// faster than the buttons could be pressed and released.
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();

// Check each button for "falling" edge.
// Type a message on the Keyboard when each button presses
// Update the Joystick buttons only upon changes.
// falling = high (not pressed - voltage from pullup resistor)
// to low (pressed - button connects pin to ground)
if (button0.fallingEdge()) {
Keyboard.press(KEY_ESC);
}
if (button1.fallingEdge()) {
Keyboard.println("B1 press");
}
if (button2.fallingEdge()) {
Keyboard.println("B2 press");
}
if (button3.fallingEdge()) {
Keyboard.println("B3 press");
}
if (button4.fallingEdge()) {
Keyboard.println("B4 press");
}
if (button5.fallingEdge()) {
Keyboard.println("B5 press");
}
if (button6.fallingEdge()) {
Keyboard.println("B6 press");
}
if (button7.fallingEdge()) {
Keyboard.println("B7 press");
}
if (button8.fallingEdge()) {
Keyboard.println("B8 press");
}
if (button9.fallingEdge()) {
Keyboard.println("B9 press");
}

// Check each button for "rising" edge
// Type a message on the Keyboard when each button releases.
// For many types of projects, you only care when the button
// is pressed and the release isn't needed.
// rising = low (pressed - button connects pin to ground)
// to high (not pressed - voltage from pullup resistor)
if (button0.risingEdge()) {
Keyboard.release(KEY_ESC);
}
if (button1.risingEdge()) {
Keyboard.println("B1 release");
}
if (button2.risingEdge()) {
Keyboard.println("B2 release");
}
if (button3.risingEdge()) {
Keyboard.println("B3 release");
}
if (button4.risingEdge()) {
Keyboard.println("B4 release");
}
if (button5.risingEdge()) {
Keyboard.println("B5 release");
}
if (button6.risingEdge()) {
Keyboard.println("B6 release");
}
if (button7.risingEdge()) {
Keyboard.println("B7 release");
}
if (button8.risingEdge()) {
Keyboard.println("B8 release");
}
if (button9.risingEdge()) {
Keyboard.println("B9 release");
}
}
 
First, try running the example without any changes.

When it's been programmed onto your Teensy, your PC should see Teensy appear as a new keyboard. When you touch a wire or paperclip to GND and pin 0, that keyboard should type "B0 press". Run a word processor or text editor, or just open a new empty window in Arduino. Click the mouse there, so you'll be able to see the keystrokes. Remember, to your PC it's just another keyboard.

Hopefully this helps. I do want to emphasize that Teensy and Arduino are about DIY electronics. We can try to help you, but ultimately you've got to put some DIY effort into this. You can't expect to get every last step in perfect detail. You're going to need to figure some things out.

When you do need help, saying only "nothing happens" or "doesn't work" is useless. Screenshots or precise descriptions of what you actually did and what really happened are needed. If it's not working, something must be not right, but nobody can tell what's wrong it you don't show what you're actually trying.
 
when i put the script on the teensy the setup for a new keyboard popped up. it said to press the key to the right of the left shift key. it doesn't have a shift key any ideas what i should do?
 
Just close that window (the little red button in the upper corner). Your mac is simply trying to gain info about the keyboard layout. It'll default to a reasonable setting if you just close that window.

The fact that window appeared means Teensy did indeed identify itself as a keyboard to your mac. Things are working.

Did you continue on, to try actually touching a wire between GND and the other pins? Did it type keystrokes? Or did the appearance of an unfamiliar window without a clear solution cause the entire project to stall, until you get an answer here? The reality is DIY electronics involves a lot of little unexpected issues. To make progress on a project, you really need to strike a balance between taking too much risk vs just experimenting and learning as you go.
 
ok this is what is up. the teensy works. when i connect the ground to a pin it works. but when i connect that button to the teensy it doesn't work at all. but when i unplug the button and connect the ground with a wire it works. what could be the problem?
 
Teensy LC Keyboard Library

I have a Teensy 3.1 using it for Keyboard functions and works great. I also have a Teensy LC but it's rejecting the Keyboard statements how do I include the library for the Keyboard?

Thanks.
 
The same Keyboard code is supposed to be able to work on Teensy LC and 3.1.

You didn't post your code that produces the problem (see the "forum rule"), nor even the error message were "it's rejecting the Keyboard statements", so we simply don't have much info to figure out what the problem is, or how to solve it.
 
My bad, I was getting the exact same error that ochoppers133 was getting "'Keyboard' was not declared in this scope" I figured it out that I needed to select the proper settings on the Arduino IDE Tools drop down "USB Type:". I am new to the Teensy and have to use the IDE. I have always done my compiling and linking from CLI. SO now I am on to building a Makefile so I can avoid using the Arduino IDE. Any help there would be much appreciated.
 
Status
Not open for further replies.
Back
Top