Keypad library, array and ascii

Hi all,

I'm using this keypad library:
http://playground.arduino.cc/Main/KeypadTutorial

It is used in a keycode alarm system:

  • pressed keys are to be placed in an array
  • input array is compared to passcode array

This library gives me the ascii value of the pressed key but what I want is the character to be placed into the input array.

For example:

  • pressing button 1 returns 49
  • pressing button 2 returns 50
  • pressing button # returns 35

(complete program code at the end)

My arrays

//PASSCODE AND INPUT================================================
//Array that holds the correct passcode and array that holds input
int arrCode[] = {3, 3, 3, 1, 5};
int arrInput[] = {0, 0, 0, 0, 0};

From this library you can use:

//get key input
  char key = keypad.getKey();

So then I want to put the pressed key into the array arrInput for comparison

//place the pressed key into arrInput at index indArray
    arrInput[indArray] = key;
    indArray++;

Now when I trace the contents of the array I see that instead of numbers, the ascii values are stored:

//TRACE:
    int i;
    for (i = 0; i < 5; i++) {
      Serial.print(arrInput[i]);
    }
    Serial.println(" ");
    //END TRACE

How do I handle this nicely ?
Yeah I could change my passcode array to:

//PASSCODE AND INPUT================================================
//Array that holds the correct passcode and array that holds input
//code is 55536
int arrCode[] = {53, 53, 53, 51, 54};
int arrInput[] = {0, 0, 0, 0, 0};

And this works. But it's not very convenient in programming

My complete code is this:

//include libraries
#include <Key.h>
#include <Keypad.h>

//Setup keypad

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', '#'}
};

byte rowPins[ROWS] = {12, 11, 10, 9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

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

//PIN SETUP ==========================================

//PIR sensor
//#define pinPir 52    //the digital pin connected to the PIR sensor's output
////piezo buzzer
//#define piezoPin 48
//
#define ledGreen 4
#define ledRed 5


//PIR SENSOR VARS ===================================================
//the time we give the sensor to calibrate (30-60 secs for ADA-189)
int calibrationTime = 3;

//ARMED VAR =========================================================
//to indicate if system is armed or not : 0= not armed 1= armed
boolean blnArmed = true;

//PASSCODE AND INPUT================================================
//Array that holds the correct passcode and array that holds input
//code is 55536
int arrCode[] = {53, 53, 53, 51, 54};
int arrInput[] = {0, 0, 0, 0, 0};

// Index to determine where the input from the button should go into the array
int indArray = 0;

/////////////////////////////
//SETUP
void setup() {

  Serial.begin(9600);

  //setup pir sensor
  //  pinMode(pinPir, INPUT);
  //  digitalWrite(pinPir, LOW);

  //setup leds
  pinMode(ledGreen, OUTPUT);
  pinMode(ledRed, OUTPUT);
  digitalWrite(ledRed, HIGH);
  digitalWrite(ledGreen, HIGH);

  //setup armed mode
  fncArm();

  //give the sensor some time to calibrate
  //  Serial.print("calibrating sensor ");
  //  for (int i = 0; i < calibrationTime; i++) {
  //    Serial.println(calibrationTime - i);
  //    delay(1000);
  //  }
  //  Serial.println(" done");
  //  Serial.println("SENSOR ACTIVE");
  //  delay(50);
}

////////////////////////////
//LOOP
void loop() {

  //get key input
  char key = keypad.getKey();

  //if "*" key is pressed the system wil be armed
  if ( key == '*' && key != NO_KEY) {
    fncArm();
  }

  //if the system is armed it should place the input into the array arrInput
  //so that the input can be compared to the passcode
  if (blnArmed && key != NO_KEY) {

    //when the array index reaches 5 arrinput is set to 0,0,0,0,0
    if ( indArray >= 5) {
      fncClearArrInput();
    }

    //place the pressed key into arrInput at index indArray
    arrInput[indArray] = key;
    indArray++;

    //TRACE:
    int i;
    for (i = 0; i < 5; i++) {
      Serial.print(arrInput[i]);
    }
    Serial.println(" ");
    //END TRACE

    //compare arrInput to arrCode. If arrInput matches arrCode the system will be disarmed
    if ( arrCode[0] == arrInput[0] && arrCode[1] == arrInput[1] && arrCode[2] == arrInput[2] && arrCode[3] == arrInput[3] && arrCode[4] == arrInput[4] ) {
      fncDisarm();
    }

  }

  if (blnArmed) {
    digitalWrite(ledRed, HIGH);
    digitalWrite(ledGreen, LOW);
  } else {
    digitalWrite(ledRed, LOW);
    digitalWrite(ledGreen, HIGH);
  }

  //  if (digitalRead(pinPir) == HIGH) {
  //    tone(piezoPin, 1000);
  //  }
  //
  //  if (digitalRead(pinPir) == LOW) {
  //    noTone(piezoPin);
  //  }

} //END LOOP


//FUNCTIONS

//function to arm system
void fncArm() {
  //when the system is armed arrInput should bet set to 0,0,0,0,0
  fncClearArrInput();
  //set bArmed to true
  blnArmed = true;
  //turn on red led
  blnArmed = true;
}

//function to disarm system
void fncDisarm() {
  //set bArmed to true
  blnArmed = false;
}

//function to clear the arrInput
void fncClearArrInput() {
  indArray = 0;
  int i;
  for (i = 0; i < 5; i = i + 1) {
    arrInput[i] = 0;
  }
}
int arrCode[] = {53, 53, 53, 51, 54};
int arrInput[] = {0, 0, 0, 0, 0};

Why not make both of these arrays of chars instead of ints ? In any case using ints wastes memory and the values will never be larger than 255 anyway.

Thanks for your reply,

it seemed to work... but now I'm having problems comparing the arrays.

Hope you can help me out om this one too :slight_smile:

Thanks in advance

When I use:

//Array that holds the correct passcode and array that holds input
//code is 55536
char arrCode[] = {5, 5, 5, 3, 6};
char arrInput[] = {0, 0, 0, 0, 0};

The compare function below does not work

When I use

//Array that holds the correct passcode and array that holds input
//code is 55536
int arrCode[] = {53, 53, 53, 51, 54};
int arrInput[] = {0, 0, 0, 0, 0};

The compare function below does work

The compare function:

    //compare arrInput to arrCode. If arrInput matches arrCode the system will be disarmed

    if ( arrCode[0] == arrInput[0] && arrCode[1] == arrInput[1] && arrCode[2] == arrInput[2] && arrCode[3] == arrInput[3] && arrCode[4] == arrInput[4] ) {
      fncDisarm();
    }

oooh !

I should have used:

//PASSCODE AND INPUT================================================
//Array that holds the correct passcode and array that holds input
//code is 55536
char arrCode[] = {'5', '5', '5', '3', '6'};
char arrInput[] = {0, 0, 0, 0, 0};

this now works with:

    //compare arrInput to arrCode. If arrInput matches arrCode the system will be disarmed
    if ( arrCode[0] == arrInput[0] && arrCode[1] == arrInput[1] && arrCode[2] == arrInput[2] && arrCode[3] == arrInput[3] && arrCode[4] == arrInput[4] ) {
      fncDisarm();
    }

I should have used:

Well spotted. I was about to point that out when I noticed your next post.

The comparison looks clumsy. Have a look at the memcmp() function memcmp()

Yeah I'm a bit clumsy still :slight_smile:

Thanks for your help UKHeliBob, saved me some time figuring things out

I replaced :

//compare arrInput to arrCode. If arrInput matches arrCode the system will be disarmed
    
if ( arrCode[0] == arrInput[0] && arrCode[1] == arrInput[1] && arrCode[2] == arrInput[2] && arrCode[3] == arrInput[3] && arrCode[4] == arrInput[4] ) {
      fncDisarm();
    }

With this:

//compare arrInput to arrCode. If arrInput matches arrCode the system will be disarmed

if (memcmp (arrCode, arrInput,  sizeof (arrCode)) == 0){
     fncDisarm();
    }