klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

teensy usb keypad

Wed Oct 25, 2017 2:47 pm

How do I setup an Arduino/Teensy USB keypad so that it works as a keypad/keyboard on a RPi? I have done a google on it there is one reference, it was saying that it was flacky at best. I thought that RPi was capable of using a PS2 keyboards?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
scruss
Posts: 2349
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: teensy usb keypad

Wed Oct 25, 2017 3:54 pm

What project were you planning to follow? Teensy boards of all flavours are used as USB keyboard controllers such as tmk_keyboard and QMK. There have to be some Planck and ErgoDox keyboard users who use 'em with Raspberry Pi.

The Raspberry Pi has no PS/2 keyboard capability built in. Hasu (the developer of tmk_keyboard) has a nice PS/2-USB converter if you need one.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: teensy usb keypad

Wed Oct 25, 2017 6:59 pm

Hello,
I was just trying to use a 4x4 keypad. I seen a game console for a online game that I thought would be fun to make, then changed my mind to 'would it be possible to make a "steam punk keyboard" for a Case Raspberry Pi'. A 4x4 pad is the best place to start. There are a lot of uses for a USB keypad if a RPi could pare them and such.

Thanks I'll try your link scruss

Ps. this is the code in the Teensy

Code: Select all

/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact [email protected]
||
|| @description
|| | Demonstrates the simplest use of the matrix Keypad library.
|| #
*/
#include <Keypad.h>

int led = 13;
int a = 0;
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
  pinMode(led, OUTPUT);  
  for (int a = 0; a <= 13; a++){
    digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(500);               // wait for a second
    digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    delay(500);    
  } 
}
  
void loop(){
  char key = keypad.getKey();
  
  if (key){
    Serial.println(key);
    digitalWrite(led, LOW); 
  } else {
    digitalWrite(led, HIGH);
  }
}
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
scruss
Posts: 2349
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: teensy usb keypad

Thu Oct 26, 2017 12:20 am

Looks like your code got a bit mangled: there's a bit of blink() in the middle of setup.

You should be able to use the Keypad Library for Arduino along with Teensy's USB Keyboard functions. I really should try this: I have a couple of Teensy's and some awesome prison-grade keypads
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: teensy usb keypad

Thu Oct 26, 2017 2:23 am

this is the re write:

Code: Select all

/* @file EventSerialKeypad.pde
 || @version 1.0
 || @author Alexander Brevig
 || @contact [email protected]
 ||
 || @description
 || | Demonstrates using the KeypadEvent.
 || #
 */
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13; 

boolean blink = false;
boolean ledPin_state;

void setup(){
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);              // Sets the digital pin as output.
    digitalWrite(ledPin, HIGH);           // Turn the LED on.
    ledPin_state = digitalRead(ledPin);   // Store initial LED state. HIGH when LED is on.
    keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}

void loop(){
    char key = keypad.getKey();

    if (key) {
        Serial.println(key);
    }
    if (blink){
        digitalWrite(ledPin,!digitalRead(ledPin));    // Change the ledPin from Hi2Lo or Lo2Hi.
        delay(100);
    }
}

// Taking care of some special events.
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
    case PRESSED:
        if (key == '#') {
            digitalWrite(ledPin,!digitalRead(ledPin));
            ledPin_state = digitalRead(ledPin);        // Remember LED state, lit or unlit.
        }
        break;

    case RELEASED:
        if (key == '*') {
            digitalWrite(ledPin,ledPin_state);    // Restore LED state from before it started blinking.
            blink = false;
        }
        break;

    case HOLD:
        if (key == '*') {
            blink = true;    // Blink the LED when holding the * key.
        }
        break;
    }
}
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
Gavinmc42
Posts: 3410
Joined: Wed Aug 28, 2013 3:31 am

Re: teensy usb keypad

Thu Oct 26, 2017 2:26 am

Arduino library and example code works.
I've made AT...32U4 based USB keyboards and if something is wrong it will be the Arduino code :oops:
Linux has no trouble with them and Pi's run Linux.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

User avatar
scruss
Posts: 2349
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: teensy usb keypad

Sat Oct 28, 2017 6:23 pm

klintkrossa wrote:
Thu Oct 26, 2017 2:23 am
this is the re write: …
I dug out a surplus phone (3×4) keypad and made a USB keyboard that worked fine on Raspberry Pi with no drivers. Here's my code:

Code: Select all

/*
  Teensy 4×3 keypad USB keyboard
  scruss - 2017-10
  - based on @file HelloKeypad.pde, part of Keypad library
  - with notes from Sparkfun part COM-08653 datasheet
   http://cdn.sparkfun.com/datasheets/Components/General/SparkfunCOM-08653_Datasheet.pdf
  - modified for Teensy via
   “Using USB Keyboard” <https://www.pjrc.com/teensy/td_keyboard.html>
   
   for writeup, see http://scruss.com/blog/2017/10/28/teensy-usb-keypad/
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};

// from Sparkfun part COM-08653 datasheet
// Stock Keypad example was quite incorrect.
byte rowPins[ROWS] = {5, 6, 7, 8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins,
                        ROWS, COLS );

void setup() {
  // Serial.begin(9600);
}

void loop() {
  char key = keypad.getKey();

  if (key) {
    Keyboard.print(key);
  }
}
Writeup and wiring is here: Teensy USB Keypad.

It should be really easy to modify this for a 4×4 button pad. You'll need 8 data lines, and the column and row pins are contiguous, so something like this might work:

Code: Select all

byte rowPins[ROWS] = {1,2,3,4}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5,6,7,8}; //connect to the column pinouts of the keypad
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: teensy usb keypad

Mon Oct 30, 2017 5:52 pm

I have it wired up and the LEDs work once I rearranged the pin sequence properly.
I am running headless so I vnc in to the RPi and open up a text editor there is no input from the key pad.
The question is:
Is there some driver for the RPi to make it read the Teensy in a key pad type of use? or is there some code that I need to add to the teensy?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: teensy usb keypad

Tue Oct 31, 2017 7:40 am

I have it working two problems with the Teensy code. First was that the baud rate was not to be set. second was that the key did not know what to do. here is the code that is in it now. to see if it will work. I plan to use it for other projects later on. like getting information on setting a ardreno from an RPi.

Code: Select all

/* @file EventSerialKeypad.ino
 || @version 1.0
 || @author Alexander Brevig
 || @ Add on author Klint-K
 || @contact [email protected]
 || @contact [email protected]******.net
 ||
 || @description
 || | Demonstrates using the KeypadEvent.
 || | For use with FallOut4
 || #
 */
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'}
};

byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8, 9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte ledPin = 13; 

boolean blink = false;
boolean ledPin_state;

void setup(){
    //Serial.begin(9600);
    pinMode(ledPin, OUTPUT);              // Sets the digital pin as output.
    digitalWrite(ledPin, HIGH);           // Turn the LED on.
    ledPin_state = digitalRead(ledPin);   // Store initial LED state. HIGH when LED is on.
    keypad.addEventListener(keypadEvent); // Add an event listener for this keypad
}

void loop(){
    char key = keypad.getKey();

    if (key) {
        Serial.println(key);
    }
    if (blink){
        digitalWrite(ledPin,!digitalRead(ledPin));    // Change the ledPin from Hi2Lo or Lo2Hi.
        delay(100);
    }
}

// Taking care of some special events.
void keypadEvent(KeypadEvent key){
    switch (keypad.getState()){
    case PRESSED:
        if (key == '#') {
            digitalWrite(ledPin,!digitalRead(ledPin));
            ledPin_state = digitalRead(ledPin);        // Remember LED state, lit or unlit.
            Keyboard.print("~");
        }
        else if(key == '*'){
          Keyboard.print("");
          break;
        }
        /* 
         * mini nukes 000E6B2E  
         * Ice cold Nuka Cola Quantum  178b25
         * Missile   000CABA3
         * Fusion Cell   00245D69
         */
        else if(key == 'A'){
          Keyboard.print("player.additem 000E6B2E "); //mini nukes
          break;
        }
        else if(key == 'B'){
          Keyboard.print("player.additem 178b25 "); //Ice cold Nuka Cola Quantum
          break;
        }
        else if(key == 'C'){
          Keyboard.print("player.additem 000CABA3 "); //Missile
          break;
        }
        else if(key == 'D'){
          Keyboard.print("player.additem 00245D69 "); //Fusion Cell
          break;
        }
        else {
          Keyboard.print(key);
          break;
        }
        break;

    case RELEASED:
        if (key == '*') {
            digitalWrite(ledPin,ledPin_state);    // Restore LED state from before it started blinking.
            blink = false;
            Keyboard.println("");
            break;
        }
        
        break;

    case HOLD:
        if (key == '*') {
            blink = true;    // Blink the LED when holding the * key.
            break;
        }
        
        break;
    }
}
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

User avatar
scruss
Posts: 2349
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: teensy usb keypad

Tue Oct 31, 2017 4:38 pm

klintkrossa wrote:
Mon Oct 30, 2017 5:52 pm
Is there some driver for the RPi to make it read the Teensy in a key pad type of use? or is there some code that I need to add to the teensy?
You need to program your Teensy in "USB Keyboard" mode from the Tools -> USB Type menu in Arduino. Description: Teensyduino: Using USB Keyboard with Teensy on the Arduino IDE

There's no baud rate to set, as it becomes a USB HID keyboard.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

Return to “Other projects”