Teensy 2.0 button input

Status
Not open for further replies.

Wombat

Member
Hi all,

I am very very new to the teensy scene, but i think for what i want teensy is perfect.

So i think i have a few simple questions :), what we need to do is to make a button that wil send a space bar command to the computer.


think of a industrial button paddestoel-noodknop.png in that button i wil place the teensy and with a usb cable connected to a computer.

if i push the button, teensy must send a keyboard space action to the computer, yes that's all;)

What besides the button the teensy and u usb cable do i need to make this work.
do i need a kfw.jpg if yes what colorcode. and how to connect.
on what Chanels on the teensy do i need to connect the button?
I have little programming skills so help on that, or examples will be nice.
and how do i prevent when the usb is inserted in the computer windows want to install a driver.

looking forward to your reactions.

Thanks in advance.
Greetings Kees Brugman.
 
First, if you've never used Arduino, start with this tutorial.

http://www.pjrc.com/teensy/tutorial.html

In part 3 you'll find info about connecting buttons and making them print to the serial monitor.

The same basic skills apply to sending keystrokes. After you've learned how to connect a button and verify it works using the serial monitor, use File > Examples > Teensy > USB_Keyboard > Buttons for an example that will guide you to sending keyboard presses instead of serial data.
 
Thanks Paul, that was very helpful, it solved the connection of the button.

do you have any idea about the code i have to use to make it work?
 
Sounds like you are creating a button to start a photo booth application. The code for a single button is fairly simple and pretty sure a sample is included with the Arduino installation. This is the code from a project I did with a Teensy 2.0 for 5 buttons. I used a key combination to reduce the chance of the actual keyboard duplicating the buttons but it should show you the basics. The KEY codes are either in a header file of in a help file, can't remember where I found them.

Code:
/* Buttons to USB Keyboard

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

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

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
}
void SendKey(int key) {

    Keyboard.set_modifier(MODIFIERKEY_CTRL);
    Keyboard.send_now();

    Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT);
    Keyboard.send_now();

    Keyboard.set_key1(key);
    Keyboard.send_now();

    Keyboard.set_modifier(0);
    Keyboard.set_key1(0);
    Keyboard.send_now();  
}  

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

  // Check each button for "falling" edge.
  // Type a message on the Keyboard when each button presses
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  if (button0.fallingEdge()) {
      SendKey(KEY_F1);
  }
  if (button1.fallingEdge()) {
      SendKey(KEY_F2);
  }
  if (button2.fallingEdge()) {
      SendKey(KEY_F3);
  }
  if (button3.fallingEdge()) {
      SendKey(KEY_F4);
  }
  if (button4.fallingEdge()) {
      SendKey(KEY_F5);
  }
}
 
Hi all,

I found a nice video on youtube, the awesome button so i tough let’s try that to begin with.
https://www.youtube.com/watch?v=eYRa3ClXP-w

So I can also see what’s happening when I push the button.

Here is the code
Code:
const byte NUMBER_OF_WORDS = 27;
char* names[NUMBER_OF_WORDS] = {
"neat", "cool", "Wicked", "incredible", "exellent", "crack", "exceptional", "fantastic",
"fabulous", "fine", "first-rate", "marvelous", "super", "terrific", "tremendous", "wonderful",
"brilliant", "superb", "keen", "laudable", "worthy", "rad", "sweet", "fresh", "dope", "fly", "nifty"
};
void setup(){                
  Serial.begin(9600);
  randomSeed(analogRead(0));
  pinMode(0, INPUT_PULLUP);
  delay(5000);
}
void loop(){                
  if (digitalRead(0) == LOW) 
  {
  Keyboard.print(names[random(0,NUMBER_OF_WORDS)]);
  Keyboard.print(" ");
  delay(500);
  }
}

And here are the errors, as far as I can see I made no mistake.
Can someone tell me what is wrong.

C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
};
^
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
C:\Users\Kees Brugman\Documents\Arduino\Awesome_button\Awesome_button.ino:6:1: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
 
Last edited:
change "char* names[NUMBER_OF_WORDS]" to "const char* names[NUMBER_OF_WORDS]" there is an explanation here: http://arduino.stackexchange.com/questions/13429/deprecated-conversion-from-string-constant-to-char

Its a warning at the moment to say that code has moved on, but should still work, but not work in future coding environment updates. Sorry, have not read the in-depth reason, but strings are constants and char are not, i believe.

Thanks Mortonkopf that solved the error problem.

I managed to create a hex file and in teensy -> File opend that file it loaded and rebooted.
But the button does not work.
IMAG1175.jpg

What are the exact steps after the code verify.



For the board in Arduino i have Teensy 2.0 and USB Type: "Keboard + Mouse + Joystick"
 
I just tried the above code (with the const change) and it worked fine. What programme are you using to read the keyboard message, i just used a simple text editor.

The steps are to put the above code into a new sketch in the arduino IDE, save it, hit to upload button, go to the text editor, and then press the button. I just shorted pin zero to gnd and it happily printed words
 
Last edited:
I just tried the above code (with the const change) and it worked fine. What programme are you using to read the keyboard message, i just used a simple text editor.

The steps are to put the above code into a new sketch in the arduino IDE, save it, hit to upload button, go to the text editor, and then press the button. I just shorted pin zero to gnd and it happily printed words

I have teensy open and arduino 1.6.6 paste and save the code and hit upload this is the error.

Teensy did not respond to a USB-based request to automatically reboot.
Please press the PROGRAM MODE BUTTON on your Teensy to upload your sketch.

so i press the program mode button on the teensy board. it is programming but the button is not working after that.

Another try result error.

Teensy Loader is currently busy with another operation (p). Please try again in a few seconds, or restart Teensy Loader.
i use notepad to test
 
Last edited:
instead of using the button, try connecting pin zero with ground very briefly, and see if this works. What application are you trying to read the keyboard with? If you open the serial monitor it should print into the top line
 
Yes i got it working :) it's not clear why, i loaded the standard blinking from the library and no problem at all.
Back to my button code i still have errors but its working.

I striped the code to get what i want, just the space input.

Code:
void setup() {
  pinMode(0, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  if(digitalRead(0)==LOW){
    Keyboard.print(" ");
     delay(5000);
  }
}

And it works wel, i have only one problem left, it must work always even if the program is started but the window not active.
Is it possible to activate the window of a started program first and then send the space command.
 
probably using the micro manage functions to control alt or command shift whatever. Paul has some examples on the keyboard page: https://www.pjrc.com/teensy/td_keyboard.html
I would have thought that if you know the windows or mac keystroke to make an open window active, then you can use the micro manage functions to send those keystrokes.
 
Yes i got it working :) it's not clear why, i loaded the standard blinking from the library and no problem at all.
Back to my button code i still have errors but its working.


And it works wel, i have only one problem left, it must work always even if the program is started but the window not active.
Is it possible to activate the window of a started program first and then send the space command.

Not from a keyboard command, there are ways to do it from a Windows application. It would require a resident windows app to detect your space key, look for the specific app you would want to set focus to, tell Windows to set focus to the app's window then send it the space key. All possible with the Windows API but somewhat complex.
 
Not from a keyboard command, there are ways to do it from a Windows application. It would require a resident windows app to detect your space key, look for the specific app you would want to set focus to, tell Windows to set focus to the app's window then send it the space key. All possible with the Windows API but somewhat complex.
there you go. Sounds reasonable given hacking potential.
 
Haven't used it myself, but as far as I can tell, AutoIt should do the trick. Instead of Keyboard.print(" "); one could send a keyboard shortcut to trigger an AutoIt script.
Ben
 
Sketch modification

Sounds like you are creating a button to start a photo booth application. The code for a single button is fairly simple and pretty sure a sample is included with the Arduino installation. This is the code from a project I did with a Teensy 2.0 for 5 buttons. I used a key combination to reduce the chance of the actual keyboard duplicating the buttons but it should show you the basics. The KEY codes are either in a header file of in a help file, can't remember where I found them.

Code:
/* Buttons to USB Keyboard

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

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

void setup() {
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
}
void SendKey(int key) {

    Keyboard.set_modifier(MODIFIERKEY_CTRL);
    Keyboard.send_now();

    Keyboard.set_modifier(MODIFIERKEY_CTRL | MODIFIERKEY_ALT);
    Keyboard.send_now();

    Keyboard.set_key1(key);
    Keyboard.send_now();

    Keyboard.set_modifier(0);
    Keyboard.set_key1(0);
    Keyboard.send_now();  
}  

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

  // Check each button for "falling" edge.
  // Type a message on the Keyboard when each button presses
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  if (button0.fallingEdge()) {
      SendKey(KEY_F1);
  }
  if (button1.fallingEdge()) {
      SendKey(KEY_F2);
  }
  if (button2.fallingEdge()) {
      SendKey(KEY_F3);
  }
  if (button3.fallingEdge()) {
      SendKey(KEY_F4);
  }
  if (button4.fallingEdge()) {
      SendKey(KEY_F5);
  }
}

Hi,
What changes to the code needs to be made if I want only 1 keyboard output
example: SendKey(F4)
Somewhere in the MODIFIERKEY Section I guess?
Any help will be appreciated.
 
Status
Not open for further replies.
Back
Top