Code: Select all
/* @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @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);
}
}
Code: Select all
/* @file EventSerialKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @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;
}
}
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);
}
}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 keypadCode: Select all
/* @file EventSerialKeypad.ino
|| @version 1.0
|| @author Alexander Brevig
|| @ Add on author Klint-K
|| @contact alexanderbrevig@gmail.com
|| @contact klint-k@******.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;
}
}
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 IDEklintkrossa wrote: ↑Mon Oct 30, 2017 5:52 pmIs 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?