Monday, February 22, 2016

Figured out the Keypad

Monday Feb. 22nd, 2016

I did not have a lot of time to work on the robot today, but I did figure out how to make the keypad work.
On the 'head' of the robot is a keypad and several LED's.



On the back of the head I found a board with all the connections on it tied to the keypad and the LED's.



There is some wiring on the back that runs to the microphone and the sonar sensor, but I will decipher those another day.  I pulled out the keypad board and set about testing with a meter to see what I could figure out about this board.



I was able to figure out that the keypad was a pretty simple grid of 4 rows and 4 columns tied together from little disc's on the back of each key on the rubber keypad cover.  When you press a button, then the little disc makes contact with the row and column of the key pressed and you get a closed contact.
I wired the rows and columns together on an Arduino board using pins 4,5,6,7 for the rows and 8,9,10,11 for the columns, using 8 pins for the keypad in total.



After making the connections, I fired up Visual Studio and created a quick program to test the keypad output based on the examples in the keypad library for Arduino.



The output worked perfectly!  I was able to trap the keypad presses the first time through.
The code to run a keypad is not very complicated, you set the row and column pins and build a small array of the keys and then create a keypad object that traps the keypress.  In the end of the program, I used a switch statement to see if certain keys were pressed and then light the LED on the Arduino. The code I used is shown below.

/*  Keypadtest for Hero Jr. Robot
 *
 *  Demonstrates the simplest use of the  keypad library.
 *
 *  The first step is to connect your keypad to the
 *  Arduino  using the pin numbers listed below in
 *  rowPins[] and colPins[]. If you want to use different
 *  pins then  you  can  change  the  numbers below to
 *  match your setup.
 *
 */
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // four columns
// Define the Keymap

char keys[ROWS][COLS] = {
  {'C','D','E','F'},
  {'8','9','A','B'},
  {'4','5','6','7'},
  {'0','1','2','3'}
};

// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 4, 5, 6, 7 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 8, 9, 10, 11 };

// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

#define ledpin 13

void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, LOW);
  Serial.begin(9600);
}

void loop()
{
  char key = kpd.getKey();
  if(key)  // Check for a valid key.
  {
    switch (key)
    {
      case '2':
        digitalWrite(ledpin, HIGH);
        break;
      case 'C':
        digitalWrite(ledpin, LOW);
        break;
      default:
        Serial.println("You Pressed Key: " );
        Serial.println(key);
    }
  }
}



No comments:

Post a Comment