In my Engineering class at school, we have a project where we build a new invention and it has to include some type of coding. My project is a knife block that locks the knives in place and unlocks when the correct passcode in put into a 4 x 4 keypad. What happens is an electromagnetic pull solenoid is in a notch in the knife and it is pulled in when the correct code is put in, then goes back out after a few seconds.
Right now with the code we have, the arduino recognizes the passcode and pulls in the solenoid when “1234” is entered into the keypad. However, it won’t reset, as a loop should, to allow the passcode to be put in again to unlock the knives again. I have tried putting in different things, but what I have currently just makes the solenoid go in again after I press “#”, “1”, “2”, “3”, or “4” and then stops.
Does anyone know of any way I can make the code reset and go back to the top of the code after each time the correct passcode has been entered and the solenoids have been pulled, or makes it so that every 4 digits entered is read as a different try?
#include <Keypad.h>
int answer[5];
int myPins[] = {'1', '2', '3', '4', '\0'};
int myPins2[] = {'5', '6', '7', '8', '\0'};
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1','2','3','A'}, //digits in row 1
{'4','5','6','B'}, //digits in row 2
{'7','8','9','C'}, //digits in row 3
{'*','0','#','D'} //digits in row 4
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad myKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
char keypressed = myKeypad.getKey();
for (int increment=0; increment<5; increment++) {
keypressed=0;
while(keypressed==0) {
keypressed = myKeypad.getKey();
}
Serial.println(keypressed);
answer[increment]=keypressed;
if (answer[0]==myPins[0] && answer[1]==myPins[1] && answer[2]==myPins[2] && answer[3]==myPins[3])
{
digitalWrite(13, HIGH);
delay(4000);
digitalWrite(13, LOW);
}
else
{
}
}
}
KnifeBeLocked-code.ino (1.28 KB)