Thanks for your help. I figured it out differently. Although, it is not very efficient if you have many choices.
I found a code designed for a secret password lock and modified it like this:
#include "Keypad.h"
#define led10 10
#define led12 12
#define led13 13
const byte ROWS = 4; // four rows
const byte COLS = 4; // three columns
char keys[ROWS][COLS] =
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[ROWS] = {9,8,7,6}; //Rows 0 to 3
byte colPins[COLS]= {5,4,3,2}; //Columns 0 to 3
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
char A1CODE[2] = {'A','1'}; // selection option 1
char B1CODE[2] = {'B','1'}; // selection option 2
char attempt[2] = {0,0};
int z=0;
void setup()
{
pinMode(led10, OUTPUT); // sets the digital pin 10 as output;
pinMode(led12, OUTPUT); // sets the digital pin 12 as output;
pinMode(led13, OUTPUT); // sets the digital pin 13 as output;
Serial.begin(9600);
Serial.println("Enter your selection and press #:");
}
void a1CODE() // do this if the correct KEY is entered
{
Serial.println(" Thank you...");
digitalWrite(led13, HIGH);
digitalWrite(led12, LOW);
delay(5000);
digitalWrite(led13, LOW);
Serial.println("Enter your selection and press #:");
}
void b1CODE() // do this if the correct KEY is entered
{
Serial.println(" Thank you...");
digitalWrite(led10, HIGH);
digitalWrite(led12, LOW);
delay(5000);
digitalWrite(led10, LOW);
Serial.println("Enter your selection and press #:");
}
void incorrectCODE() // do this if an incorrect KEY is entered
{
Serial.println("Invalid selection");
digitalWrite(led12, HIGH);
digitalWrite(led13, LOW);
delay(2000);
digitalWrite(led12, LOW);
Serial.println("Enter your selection and press #:");
}
void checkKEY()
{
int correct=0;
int i;
for ( i = 0; i <= 2 ; i++ )
{
if (attempt[i]==A1CODE[i])
{
correct++;
}
}
if (correct==2)
{
a1CODE();
}
{
if (attempt[i]==B1CODE[i])
{
correct++;
}
}
if (correct==2)
{
b1CODE();
}
else
{
incorrectCODE();
}
for (int zz=0; zz <= 2; zz++) // clear previous key input
{
attempt[zz]=0;
}
}
void readKeypad()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
switch(key)
{
case '*':
z=0;
break;
case '#':
delay(100); // added debounce
checkKEY();
break;
default:
attempt[z]=key;
z++;
}
}
}
void loop()
{
readKeypad();
}
and it works perfectly fine. Other than the lengthy code if I had, let's say 40 options, do you foresee any other issues?