📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 3 Day orders by 11am ET Friday December 13 📬❄️📦
0

found a keypad, need help programming it
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

found a keypad, need help programming it

by GAHorton on Fri Oct 31, 2008 2:46 pm

At my trusty electronics shop i found a keypad for only 2 bucks! and it seems like a perfect thing to throw on the arduino! here is pictures:
Image

The Back
Image

All wired up!
Image

Luckily i was able to find the specs of the keypad, there were a few things that were weird. I expected there to be a ground pin, there doesn't seem to be one. also the keypad only has 8 pins on the back, so the selection of numbers is done in an array fashion. Here is the Spec Sheet: http://lgrws01.grayhill.com/web/images/ ... ads_96.pdf

i want to know what would be the best way to go about programming this? I'v taken some classes in C and C++, and it seems like a switch statement would be a good way, but i'm not sure

share your thoughts!
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: found a keypad, need help programming it

by trialex on Fri Oct 31, 2008 7:34 pm

I'm assuming you've seen the keypad tutorial on arduino.cc?

http://www.arduino.cc/playground/Main/KeypadTutorial
trialex
 
Posts: 189
Joined: Mon Apr 03, 2006 6:25 pm

Re: found a keypad, need help programming it

by GAHorton on Sat Nov 01, 2008 1:36 pm

i wrote up some code using that lib, and it seems to crash a lot when i run the program.

This doesn't like the Serial.clear(); it won't compile with it.
Code: Select all | TOGGLE FULL SIZE
#include <keypad.h>

#define ROWS 4
#define COLS 4

keypad kpd = keypad(ROWS, COLS);

void setup()
{
  kpd.init();
  Serial.begin(9600);
  Serial.println("Ready...");
}

void loop()
{
    char key = kpd.get_key();

    if(key != '\0')
    {
        Serial.clear();
        Serial.print(key);
    }
}


when i remove the clear it compiles but goes off and prints "77777777777777777777777777777777777777777777777777777777" etc... and crashes, freezes the arduino compiler and i have to use xkill to save myself.

so yea...
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: found a keypad, need help programming it

by Amberwolf on Sat Nov 01, 2008 5:38 pm

.
Last edited by Amberwolf on Mon Nov 03, 2008 11:42 pm, edited 1 time in total.
Watch me build an electric bike from assorted junk!
Alternative Energy Sources for the Home from Recycled Junk
A subsidiary of Cybernetic Necromancers, Discorporated:
"We Bring Dead Things To Life"
Amberwolf
 
Posts: 310
Joined: Wed Oct 08, 2008 2:42 am
Location: Phoenix, AZ, USA

Re: found a keypad, need help programming it

by mtbf0 on Sat Nov 01, 2008 6:33 pm

i don't see any pullups, and it doesn't look like the avr's internal pullups are enabled in the code anywhere. you might add

Code: Select all | TOGGLE FULL SIZE
  digitalWrite (ROW0, HIGH);
  digitalWrite (ROW1, HIGH);
  digitalWrite (ROW2, HIGH);
  digitalWrite (ROW3, HIGH);


after kpd.init(). otherwise the inputs on the rows will be floating.

also, did you change the pin assignments in the library, because it uses the serial port, (digital 0-1), for keypad i/o.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: found a keypad, need help programming it

by GAHorton on Sat Nov 01, 2008 9:43 pm

i added the pullups and got this:
Code: Select all | TOGGLE FULL SIZE
 error: ‘ROW0’ was not declared in this scopeCouldn't determine program size:


I changed the pins, i'm not using 0 so i changed it so it goes 1-8:
Code: Select all | TOGGLE FULL SIZE
#include "keypad.h"
extern "C" {
   #include "WConstants.h"  //all things wiring / arduino
}



// --------- PIN MAPPINGS -------------------------------

// The pin mappings are matched to the pin numbers found on the Arduino. For example

// the first column from your keypad is connected to pin 6 on the Arduino and the

// second column is connected to pin 5. It's the same for the rows.

// These are the numbers you would change to match your keypad wiring. Even though

// they are shown in order you can set them to any valid (random) pin number.



#define COL0   7

#define COL1   6

#define COL2   5

#define COL3   8

#define ROW0   4

#define ROW1   3

#define ROW2   2

#define ROW3   1



// Note: The column pins will be set as outputs and the row pins will be set as inputs.



// Default row and col pin counts. Don't change these here. You can set them in your

// code when instantiating a new keypad object. See the example project.

int num_rows = 4;

int num_cols = 3;



static int col_select;   // Keeps track of which column to activate/de-activate



// 4x4 keypad key mapping. You shouldn't need to touch this unless you have

// a keypad with different (or more) keys than those listed.

const char keymap[4][5] = {   "123A",

            "456B",

            "789C",

            "*0#D"  };



int row[4] = {ROW0, ROW1, ROW2, ROW3};

int col[4] = {COL0, COL1, COL2, COL3};

//--------------------------------------------------------



// get_key() takes control of the data lines on each call just in case

// another part of the user's code needs to share control. This allows

// them to be multiplexed with, for example, the data lines of an LCD.

char keypad::get_key()

{

   static int temp_key;

   int key = '\0';



   digitalWrite(col[col_select],LOW);         // Activate the current column.



   for(int r=0;r<num_rows;r++)            // Scan all the rows for a key press.

   {

      pinMode(row[r],INPUT);            // Take control of data lines

      if(digitalRead(row[r]) == LOW)         // Is a key STILL being pressed?

      {

         temp_key = keymap[r][col_select];   // Remember which key was pressed.

         return '\0';            // Prevents repeating when a key is held down.

      }

   }



   if( (key == '\0') && (temp_key != '\0') )      // Key was pressed and then released

   {

      key = temp_key;

      temp_key = '\0';            // Get ready for next keypress

      return key;

   }



   digitalWrite(col[col_select],HIGH);         // De-activate the current column.



   col_select++;                  // Move to the next column.

   if(col_select == num_cols)

      col_select=0;



   return key;

}



// Default - Setup up the keypad in accordance with keymap above.

void keypad::init ()

{

   for (int c=0; c<num_cols; c++)

   {

      pinMode(col[c],OUTPUT);            // Set column pins as outputs for writing

      digitalWrite(col[c],HIGH);         // Make all columns inactive to start with.

   }

   col_select = 0;                  // Start with the first column.

}



keypad::keypad()

{

   // Default constructor

}



// Constructor for setting your keypad matrix size

keypad::keypad(int r, int c)

{

   num_rows = r;

   num_cols = c;

}


i tried to compile the .cpp to get a .h and got lots of errors:
Code: Select all | TOGGLE FULL SIZE
keypad.cpp:50:56: error: WConstants.h: No such file or directory
keypad.cpp: In member function ‘char keypad::get_key()’:
keypad.cpp:97: error: ‘LOW’ was not declared in this scope
keypad.cpp:97: error: ‘digitalWrite’ was not declared in this scope
keypad.cpp:101: error: ‘INPUT’ was not declared in this scope
keypad.cpp:101: error: ‘pinMode’ was not declared in this scope
keypad.cpp:102: error: ‘digitalRead’ was not declared in this scope
keypad.cpp:116: error: ‘HIGH’ was not declared in this scope
keypad.cpp: In member function ‘void keypad::init()’:
keypad.cpp:130: error: ‘OUTPUT’ was not declared in this scope
keypad.cpp:130: error: ‘pinMode’ was not declared in this scope
keypad.cpp:131: error: ‘HIGH’ was not declared in this scope
keypad.cpp:131: error: ‘digitalWrite’ was not declared in this scope


-------------------------------------------------------------------------------
Are any of the rows shorted to any of the columns when you are *not* pressing a key?


i'm not sure what you mean by this.
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: found a keypad, need help programming it

by Amberwolf on Sat Nov 01, 2008 10:53 pm

.
Last edited by Amberwolf on Mon Nov 03, 2008 11:42 pm, edited 1 time in total.
Watch me build an electric bike from assorted junk!
Alternative Energy Sources for the Home from Recycled Junk
A subsidiary of Cybernetic Necromancers, Discorporated:
"We Bring Dead Things To Life"
Amberwolf
 
Posts: 310
Joined: Wed Oct 08, 2008 2:42 am
Location: Phoenix, AZ, USA

Re: found a keypad, need help programming it

by mtbf0 on Sat Nov 01, 2008 11:35 pm

GAHorton wrote:i added the pullups and got this:
Code: Select all | TOGGLE FULL SIZE
 error: ‘ROW0’ was not declared in this scopeCouldn't determine program size:


yow! stupid of me as usual. try sticking those lines in kpd.init, though i guess the right way to do it would be

Code: Select all | TOGGLE FULL SIZE
void keypad::init ()
{
   for (int c=0; c<num_cols; c++)
   {
      pinMode(col[c],OUTPUT);            // Set column pins as outputs for writing
      digitalWrite(col[c],HIGH);         // Make all columns inactive to start with.
   }
   for (int r=0; r<num_rows; r++)
   {
      digitalWrite(row[r],HIGH);         // turn on pullups for all rows
   }
   col_select = 0;                  // Start with the first column.
}
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: found a keypad, need help programming it

by GAHorton on Sun Nov 02, 2008 1:37 pm

I'm still having trouble compiling the .cpp file, it seems not to be finding the header files, even when i put the .cpp in the same folder as the header.

Code: Select all | TOGGLE FULL SIZE
keypad.cpp:48:39: error: WConstants.h: No such file or directory
keypad.cpp: In member function ‘char keypad::get_key()’:
keypad.cpp:97: error: ‘LOW’ was not declared in this scope
keypad.cpp:97: error: ‘digitalWrite’ was not declared in this scope
keypad.cpp:101: error: ‘INPUT’ was not declared in this scope
keypad.cpp:101: error: ‘pinMode’ was not declared in this scope
keypad.cpp:102: error: ‘digitalRead’ was not declared in this scope
keypad.cpp:116: error: ‘HIGH’ was not declared in this scope
keypad.cpp: In member function ‘void keypad::init()’:
keypad.cpp:130: error: ‘OUTPUT’ was not declared in this scope
keypad.cpp:130: error: ‘pinMode’ was not declared in this scope
keypad.cpp:131: error: ‘HIGH’ was not declared in this scope
keypad.cpp:131: error: ‘digitalWrite’ was not declared in this scope
keypad.cpp:135: error: ‘HIGH’ was not declared in this scope
keypad.cpp:135: error: ‘digitalWrite’ was not declared in this scope
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: found a keypad, need help programming it

by mtbf0 on Sun Nov 02, 2008 3:41 pm

i don't know. have you got everything tucked into the right place, arduino-001x/hardware/libraries/kpd, or whatever? where, the x in that outer most directory is whatever digit is takes to match your version of arduino.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: found a keypad, need help programming it

by GAHorton on Mon Nov 03, 2008 3:40 am

anyone know?
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: found a keypad, need help programming it

by mtbf0 on Mon Nov 03, 2008 4:25 am

where do you have keypad.cpp?
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: found a keypad, need help programming it

by GAHorton on Mon Nov 03, 2008 2:30 pm

in arduino-0012/hardware/libraries/keypad/

the WConstants.h file is in /hardware/cores/arduino/
GAHorton
 
Posts: 90
Joined: Tue Feb 19, 2008 4:30 am

Re: found a keypad, need help programming it

by mtbf0 on Mon Nov 03, 2008 5:17 pm

make sure there's not a copy of the library in the directory with your sketch.

i had a project i was trying to get to run on both an arduino and a minipov and wound up with a copies of the program with both .c and .pde extentions and started getting the most bizarre error reports.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Please be positive and constructive with your questions and comments.