Author Topic: I want to make a small numpad  (Read 4378 times)

0 Members and 1 Guest are viewing this topic.

Offline bavman

  • Thread Starter
  • Posts: 529
I want to make a small numpad
« on: Sun, 03 February 2013, 23:12:36 »
Since I got a couple extra teensy controllers I want to make a little numpad sort of device that can be reprogrammed for some mmo games.
I just to bread board a simple 3x3 layout for now, but I don't know how what to connect to the controller since I'm not really savvy at this stuff.

I drew a 3x3 matrix. As I understand it, each column (at a time) receives a high signal which travels through the switch if its depressed and is read by which row its in. I hope I did this right, if someone could take a look at it and tell me which column/rows get soldered on which teensy 2.0 pins. The layout is here: http://www.pjrc.com/teensy/pinout.html

My guess is that the columns where the high signal is sent is attached to the PWM pins and the rows are attached to any other pin. What do I do with the 5V rail and ground or do I even need them?


Offline alaricljs

  • I be WOT'ing all day...
  • ** Moderator Emeritus
  • Posts: 3715
  • Location: NE US
Re: I want to make a small numpad
« Reply #1 on: Mon, 04 February 2013, 07:42:18 »
In the case of the Phantom, the PWM pins were reserved for LEDs so that variable brightness could be done in firmware.

The easiest way to decide what pins to deal with is to check out an established firmware and see what pins have been used. A quick browse through hasu's code shows all of  port B and port D are commonly used, and parts  of C, F and E.
Filco w/ Imsto thick PBT
Ducky 1087XM PCB+Plate, w/ Matias "Quiet Click" spring-swapped w/ XM Greens

Offline bavman

  • Thread Starter
  • Posts: 529
Re: I want to make a small numpad
« Reply #2 on: Tue, 05 February 2013, 16:36:28 »
What about since I only have a small amount of keys I just assign each key its own pin? It would make programing a little easier too.

Each key would be connected to the 5 volt rail and the output would go to an individual pin to be read

Offline samwisekoi

  • MAWG since 1997
  • * Administrator
  • Posts: 2480
  • Location: Mt. View, California
  • Sorry, moving houses. Be back ASAP.
    • Tweet samwisekoi
Re: I want to make a small numpad
« Reply #3 on: Tue, 05 February 2013, 17:02:52 »
What about since I only have a small amount of keys I just assign each key its own pin? It would make programing a little easier too.

Each key would be connected to the 5 volt rail and the output would go to an individual pin to be read

Well, really what you have then is a 1x9 matrix.  One column and nine rows.  That would let you use just one SIDE of the Teensy.  And all of the existing FW source.

 - Ron | samwisekoi
I like keyboards and case modding.  Everything about a computer should be silent -- except the KEYBOARD!

'85 IBM F-122/Soarer Keyboard |  Leopold FC200 TKL (Browns) + GH36 Keypad (Browns/Greens) | GH-122 (Whites/Greens) with Nuclear Data Green keycaps in a Unicomp case

Offline kmiller8

  • Banned
  •  Post Reporting Timeout
  • Posts: 1589
  • Who is that kmiller8 guy?
Re: I want to make a small numpad
« Reply #4 on: Tue, 05 February 2013, 18:33:03 »
Here, check out this resource, Read the main article, and Click on the links from A-L to learn more than you'll ever need to know about reading a matrix :)

http://www.openmusiclabs.com/learning/digital/input-matrix-scanning/

Offline bavman

  • Thread Starter
  • Posts: 529
Re: I want to make a small numpad
« Reply #5 on: Tue, 05 February 2013, 20:02:43 »
Thanks for that link kmiller, I was looking for something really simple like that, that spells everything out

Offline bpiphany

  • Posts: 1033
  • Location: Stockholm, Sweden
  • bpiph is a special type of crazy. //mkawa
Re: I want to make a small numpad
« Reply #6 on: Sat, 09 February 2013, 08:08:24 »
I think the keyboard example on pjrc's page is pretty much what you want to use. They have a small number of switches connected directly to ground, each one. You don't even need any diodes for that. Only 9 IO-pins =) You may even be able to use interrupts to tell when a  key has been pressed. No scanning whatsoever =D Drop that matrix...
http://pjrc.com/teensy/usb_keyboard.html
« Last Edit: Sat, 09 February 2013, 08:10:36 by bpiphany »

Offline bavman

  • Thread Starter
  • Posts: 529
Re: I want to make a small numpad
« Reply #7 on: Sun, 10 February 2013, 15:08:35 »
Thanks I'll try that as well.

I ordered a small breadboard so I can prototype my idea only to find out the switches won't fit on the board because of that center plastic hump, and I think the spacing might be off too.

Offline bavman

  • Thread Starter
  • Posts: 529
Re: I want to make a small numpad
« Reply #8 on: Sun, 10 February 2013, 16:02:22 »
I just soldered some jumper cables on the switches and attached everything. Flashed usb_keyboard.hex from the page you linked, bpiphany, and it worked. The only thing I can't figure out is that every 8 seconds it inserts a space. I also just remembered that I don't know c++, its been a very long time since I used it. Can i write the code in teensyduino since it seems really dumbed down or do I really need c++?

Offline bpiphany

  • Posts: 1033
  • Location: Stockholm, Sweden
  • bpiph is a special type of crazy. //mkawa
Re: I want to make a small numpad
« Reply #9 on: Sun, 10 February 2013, 16:10:54 »
"An idle timeout is also implemented which send a spacebar keystroke after 8 seconds of inactivity. "

=D You need to learn some c. Don't bother with the ++.

Edit: if you cut these parts out you should get rid of the spaces

Code: [Select]
// Configure timer 0 to generate a timer overflow interrupt every
// 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
// This demonstrates how to use interrupts to implement a simple
// inactivity timeout.
TCCR0A = 0x00;
TCCR0B = 0x05;
TIMSK0 = (1<<TOIE0);
Code: [Select]
// if any keypresses were detected, reset the idle counter
if (reset_idle) {
// variables shared with interrupt routines must be
// accessed carefully so the interrupt routine doesn't
// try to use the variable in the middle of our access
cli();
idle_count = 0;
sei();
}
Code: [Select]
// This interrupt routine is run approx 61 times per second.
// A very simple inactivity timeout is implemented, where we
// will send a space character.
ISR(TIMER0_OVF_vect)
{
idle_count++;
if (idle_count > 61 * 8) {
idle_count = 0;
usb_keyboard_press(KEY_SPACE, 0);
}
}

As well as anything involving "reset_idle"
« Last Edit: Sun, 10 February 2013, 16:17:01 by bpiphany »

Offline bavman

  • Thread Starter
  • Posts: 529
Re: I want to make a small numpad
« Reply #10 on: Sun, 10 February 2013, 17:52:44 »
You know that makes a lot of sense since I basically understood nothing when I opened the file  :))

But I did finally get it to work by just modifying the while statement:
Code: [Select]
while (1) {
// read all port B and port D pins
b = PINB;
d = PIND;
// check if any pins are low, but were high previously
mask = 1;
/*reset_idle = 0;*/
for (i=0; i<8; i++) {
if (((b & mask) == 0) && (b_prev & mask) != 0) {
//usb_keyboard_press(KEY_B, KEY_SHIFT);
if (i == 0)
                                        usb_keyboard_press(KEY_1, 0);
                                if (i == 1)
                                    usb_keyboard_press(KEY_2, 0);
                                if (i == 2)
                                    usb_keyboard_press(KEY_3, 0);
                                if (i == 3)
                                    usb_keyboard_press(KEY_4, 0);
                                if (i == 7)
                                    usb_keyboard_press(KEY_6, 0);
                                if (i == 4)
                                    usb_keyboard_press(KEY_7, 0);
                                if (i == 5)
                                    usb_keyboard_press(KEY_8, 0);
                                if (i == 6)
                                    usb_keyboard_press(KEY_9, 0);                                           
/*reset_idle = 1;*/
}
if (((d & mask) == 0) && (d_prev & mask) != 0) {
//usb_keyboard_press(KEY_D, KEY_SHIFT);
                            if (i == 1)
                                usb_keyboard_press(KEY_5,0);
/*reset_idle = 1;*/
}
mask = mask << 1;
}
// if any keypresses were detected, reset the idle counter
/*if (reset_idle) {
// variables shared with interrupt routines must be
// accessed carefully so the interrupt routine doesn't
// try to use the variable in the middle of our access
cli();
idle_count = 0;
sei();
}*/
// now the current pins will be the previous, and
// wait a short delay so we're not highly sensitive
// to mechanical "bounce".
b_prev = b;
d_prev = d;
_delay_ms(2);
}

I can't really write it from scratch as I'd mess it up. So I guess its a good start. I just need to find a metal plate now

Offline salmo

  • Posts: 120
  • Location: Memphis, TN, USA
    • messmore.org
Re: I want to make a small numpad
« Reply #11 on: Sun, 10 February 2013, 18:09:24 »
I've been thinking about doing the same as well, and wandered into this thread at the perfect time.

I'm not trying to output USB, but want to make a nice keypad for my arduino project, and this helped me grok what I was doing a lot better so I'm not just stuck copying someone's work and not fully getting it.

Offline bavman

  • Thread Starter
  • Posts: 529
Re: I want to make a small numpad
« Reply #12 on: Sun, 10 February 2013, 22:23:31 »
It's even easier if you use the Adruino software. It may not be as powerful as C (i assume), but it works for my purpose and the code is super simple, almost to a child's level which is what I needed :)

Code: [Select]
#include <Bounce.h>

Bounce button0 = Bounce(0, 10);
Bounce button1 = Bounce(1, 10);
Bounce button2 = Bounce(2, 10);
Bounce button3 = Bounce(3, 10);
Bounce button4 = Bounce(6, 10);
Bounce button5 = Bounce(4, 10);
Bounce button6 = Bounce(13, 10);
Bounce button7 = Bounce(14, 10);
Bounce button8 = Bounce(15, 10);

void setup()
{
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP); 
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
  pinMode(15, INPUT_PULLUP); 
}

void loop()
{
  button0.update();
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  button6.update();
  button7.update();
  button8.update();
 
  if (button0.fallingEdge()){
    Keyboard.print("1");
  }
  if (button1.fallingEdge()){
    Keyboard.print("2");
  }
  if (button2.fallingEdge()){
    Keyboard.print("3");
  }
  if (button3.fallingEdge()){
    Keyboard.print("4");
  }
  if (button4.fallingEdge()){
    Keyboard.print("5");
  }
  if (button5.fallingEdge()){
    Keyboard.print("6");
  }
  if (button6.fallingEdge()){
    Keyboard.print("7");
  }
  if (button7.fallingEdge()){
    Keyboard.print("8");
  }
  if (button8.fallingEdge()){
    Keyboard.print("9");
  }
}

Actually I've moved away from keyboard.print("") to keyboard.set_key1(KEY_1) then keyboard.send_now() for example. I set the keys to F13 to F21 since they aren't used and I can just program them to whatever I want whether its in game or through something like AHK. I'm not sure if thats the most efficient way to do things or not.
« Last Edit: Sun, 10 February 2013, 23:47:19 by bavman »