keypad password = output

Hey all, New to the forums, but have been using arduinos for a while now. I am looking for a sketch that allows me to use my 4 row 3 column (adafruit) keypad and a password. I have both libraries and have been playing with them to try to learn and tweek them to fit my project.

I want it to say "if 1234# is pressed, digitalWrite pin 13 low".

anybody have a sketch that does this?

I cannot find any good (ENGLISH) tutorials that explain line by line the code for keypad or password sketches. HELP!

anybody have a sketch that does this?

Why don't you want to learn how to write code?

If you want someone to write it for you post in Gigs and collaboration and be prepared to pay.

I should specify what I am looking for. I am looking for one of two things. If somebody has code thats close to what I am looking for, I would like to see it so I can go through it and learn (I've found that this is the easiest way for me to learn what i have learned so far). All of the examples I have found deal only with the keypad. Or I would like to see if anybody knows of any tutorials on the Password library and the keypad library. Ideally somebody explaining the sketch line by line as these types of videos are what sucked me into this world. I DO want to learn the code. This is why I am asking for something close or a line by line tutorial of these two library's.

No idea about the password library; I think you can do without it.

Are you familiar with the "updated serial input basics" thread? If not, find it and read it. It shows how you can fill a character array with received bytes from the serial port. Instead of reading from the serial port, you read the keypad. If the key does not equal nokey, you store the character in the array. When the user presses #, you store '\0' after which you can use strcmp to compare the entered text against a 1234 and take action based on that.

Cost some amount of money to have someone do it for you. Costs even more money to have someone hold your hand through it.

If you search for a state machine, you might get something like what you're trying to implement.

I think that a statemachine will complicate life of the OP at this stage.

INTP:
Cost some amount of money to have someone do it for you. Costs even more money to have someone hold your hand through it.

A bit of mental exercise does not hurt me :slight_smile:

#include <Keypad.h>

#define KP_TIMEOUT 5000
#define UNLOCKDURATION 10000

const byte lockPin = 13;

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

// Define the keymaps.  The blank spot (lower left) is the space character.
char numberKeys[ROWS][COLS] =
{
  { '1', '2', '3' },
  { '4', '5', '6' },
  { '7', '8', '9' },
  { ' ', '0', '#' }
};

Keypad kp( makeKeymap(numberKeys), rowPins, colPins, sizeof(rowPins), sizeof(colPins) );


void setup()
{
  pinMode(lockPin, OUTPUT);
  Serial.begin(115200);
}

void loop()
{
  // the state of the lock
  static bool unlockState = false;

  // the unlock start time
  static unsigned long unlockStartTime = 0;

  // pointer to password
  char *pwd;

  // read a keystroke
  char ch = kp.getKey();
  //char ch = 0;
  //if (Serial.available() > 0)
  //{
  //  ch = Serial.read();
  //}

  // if not unlocked
  if (unlockState == false)
  {
    // add to user entry
    pwd = addToPwd(ch);

    // if pwd does not equal NULL, the password is complete
    if (pwd != NULL)
    {
      // compare
      if (strcmp(pwd, "1234") == 0)
      {
        Serial.println("Valid password");
        // indicate unlocked
        unlockState = true;
        // set the unlock start time
        unlockStartTime = millis();
        Serial.println("Unlock timeout 'timer' started");
      }
      else
      {
        Serial.println("Invalid password");
      }
    }
  }
  // when user pressed any key while unlocked
  else if (ch != NO_KEY)
  {
    Serial.println("User locked");
    // indicate locked
    unlockState = false;
  }

  // check if unlock 'timer' started if unlock lasted more than N seconds
  if (unlockStartTime != 0 && (millis() - unlockStartTime >= UNLOCKDURATION))
  {
    Serial.println("Unlock timeout");
    // reset the unlock start time
    unlockStartTime = 0;
    // set the state to locked
    unlockState = false;
  }

  if (unlockState == true)
  {
    // unlock
    digitalWrite(lockPin, LOW);
  }
  else
  {
    // lock
    digitalWrite(lockPin, HIGH);
  }
}

/*
  add received character to password
  in:
    received character (NO_KEY wiill be ignored)
  returns:
    NULL if entry not complete, else pointer to entered password
*/
char *addToPwd(char ch)
{
  // variable to store 4 character password and terminating nul character
  static char password[5];
  // index in the password array; where to store the next received character
  static byte index = 0;

  // start time of first keypress
  static unsigned long startTime = 0;

  // if timeout 'timer' not started, start it
  if (ch != NO_KEY && startTime == 0)
  {
    Serial.println("Keypad timeout 'timer' started");
    startTime = millis();
  }

  // check N second keypad timeout
  if (startTime != 0 && (millis() - startTime >= KP_TIMEOUT))
  {
    Serial.println("Keypad timeout");
    // reset startTime
    startTime = 0;
    // reset index
    index = 0;
    // indicate password not complete
    return NULL;
  }

  // handle the keypress
  switch (ch)
  {
    // no key pressed
    case NO_KEY:
      break;
    // user canceled entry
    case '*':
      Serial.println("User canceled");
      // reset startTime
      startTime = 0;
      // reset index
      index = 0;
      // indicate password not complete
      return NULL;
    // user indicate entry complete
    case '#':
      Serial.println("Password complete");
      // add string terminator
      password[index] = '\0';
      // reset the index for the next time that we enter a password
      index = 0;
      // reset the startTime
      startTime = 0;
      // return the (pointer to the) password
      return password;
    default:
      // do not allow more than N characters (prevents buffer overflow and leaves space for the string terminator)
      if (index >= sizeof(password) - 1)
      {
        Serial.println("Buffer overflow prevented");
        return NULL;
      }

      // store the character
      password[index++] = ch;
      Serial.println("Character added");
      break;
  }

  // entry not complete yet, return NULL pointer
  return NULL;
}

You can remove the Serial.print statements; they are there for debugging.

Analyse it and learn from it.