When I run this code, my pin and pinCheck arrays are not being filled with the results of keypad.getKey(). If I print the value at each index, the result is blank. As far as I can tell, I am either not writing a char to the array, I am misreading the array when it comes time to print to the serial monitor, or .getKey() only works in the main or loop functions.
#include <Key.h>
#include <Keypad.h>
void armDisarm(void);
boolean array_cmp(char, char, int, int);
bool isArmed = 0;
char pin[3];
char pinCheck[3];
const byte ROWS = 4; // number of rows
const byte COLS = 4; // number of columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'#','0','*','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // row pinouts of the keypad R1 = D8, R2 = D7, R3 = D6, R4 = D5
byte colPins[COLS] = {5, 4, 3, 2}; // column pinouts of the keypad C1 = D4, C2 = D3, C3 = D2
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (isArmed == 1)
{
Serial.println("SYSTEM ARMED! ENTER PIN TO DISARM!");
}
else
{
Serial.println("SYSTEM DISARMED! ENTER PIN TO ARM!");
}
armDisarm();
}
void armDisarm(void)
{
while(true)
{
if (isArmed == 0)
{
for (int i = 0; i < 4;)
{
if (keypad.getKey() == NO_KEY)
{
continue;
}
pin[i] = keypad.getKey();
Serial.println(pin[i]);
i++;
}
isArmed = 1;
return;
}
else if (isArmed == 1)
{
for (int j = 0; j < 4;)
{
if (keypad.getKey() == NO_KEY)
{
continue;
}
pinCheck[j] = keypad.getKey();
Serial.println(pin[j]);
j++;
}
for (int k = 0; k < 4; k++)
{
if(pinCheck[k] != pin[k])
{
isSame = 0;
break;
}
else
{
isSame = 1;
}
}
if (array_cmp(pin, pinCheck, sizeof(pin), sizeof(pinCheck)) == 1)
{
Serial.println("same pin"); //used for bebugging
isArmed = 0;
return;
}
else
{
Serial.println("INCORRECT PIN! CLAYMORE ROOMBA DEPLOYED!");
}
}
}
}
boolean array_cmp(char a[sizeof(pin)], char b[sizeof(pinCheck)], int len_a, int len_b)
{
int n;
// if their lengths are different, return false
if (len_a != len_b)
{
return false;
}
// test each element to be the same. if not, return false
for (n = 0 ;n < len_a; n++)
{
if (a[n]!= b[n])
{
return false;
}
}
//ok, if we have not returned yet, they are equal :)
return true;
}