How to Run the Main Loop for Your Keypad Entry System Arduino Project - dummies

How to Run the Main Loop for Your Keypad Entry System Arduino Project

By Brock Craft

You will need to run a main loop for your keypad entry system Arduino project. There are only a few things the code does to operate your entry system. The main loop operates as follows:

  1. Listen for a key.

  2. If a key has been pressed, send it to the LED display.

  3. Increment the number of key presses by one.

  4. Enter it into the code buffer in the next available position.

  5. Determine whether four digits have been entered.

    If so, check whether the code is valid.

  6. If the code is valid, open the door and reset everything for the next time around.

The main thing you need to do is check whether a key has been pressed. The first statement creates a local char variable called key to store the character that is sent back from a function named getKey(). Remember the keyMap? That’s where this number ultimately comes from.

The char value from key is also assigned to the codeBuffer[] array, which you use to test whether the code is valid. It is added to the position stored by keypressCount, which is incremented upward every time a new character is detected. The “zero-th” character is the first keypress detected. So codeBuffer[0] contains the first key pressed on the keypad, codeBuffer[1] contains the second key pressed, and so on.

The next conditional if statement executes only when a key has been pressed, at which point Steps 2 through 6 from the preceding list are processed. If a key hasn’t been pressed, the program just waits for that to happen. When it does, the sendCommand function executes:

sendCommand(keypressCount+1, key);

The sendCommand takes two parameters: which seven-segment module number to change, and what to change it to. You use keypressCount not only to keep track of the number of times keys have been pressed so far, but also to tell which seven-segment digit to light up.

However, keypressCount starts at zero because the code[] the buffer[] char arrays start their numbering at zero, and you want to store those values in the right place. But the Max chip starts numbering digits from 1. Therefore, to use keypressCount to light up the correct seven-segment digit, you have to add one (+1) to its value.

The next statement implements that handy debugging feature. If you have set the DEBUG boolean to true, the code will print out all of your variables to the serial port.

Next, you increment the keypressCount by one and then test to see if four digits have been pressed. If so, it’s showtime. The delay(500) statement gives you a half-second to see the last access code digit entered because the display will change depending on whether the right access code was entered. This test is done with a little bit of native C language code:

if (memcmp(codeBuffer, code,4)==0) {

The function memcmp() compares two items in memory and takes as its parameters the two items and their expected lengths. In this case, you are comparing the codeBuffer array and the code stored at the beginning of the program. Both have a length of four bytes.

If they are exactly the same, the memcmp() function returns a zero and that is exactly what you are looking for in the conditional statement. If they are the same (and DEBUG is true), “MATCH!” is printed to the serial port and the unlock() function is executed. If the memory comparison fails, then the function does not return a “0,” meaning the wrong code was entered.

In either case, you want to clear the display, ready for the next time around. So you call the clearDisplay() function. Another native C language instruction does a little memory housekeeping:

memset(codeBuffer, 0, 4);

This clears out the codeBuffer explicitly by setting it to 0. Its length is 4, which is the second parameter. Some might argue that this instruction is not necessary, but it’s good practice to explicitly manage memory rather than leave things to chance.

Lastly, because this was the fourth keypress and there are only four digits to evaluate, the keypressCount needs to be reset to zero so you are ready for the next iteration.