#include <Keypad.h>
const int ROW_NUM = 4;
const int COLUMN_NUM = 4;
char keys[ROW_NUM][COLUMN_NUM] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte pin_rows[ROW_NUM] = {34, 36, 38, 40};
byte pin_column[COLUMN_NUM] = {26, 28, 30, 32};
Keypad keypad = Keypad( makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
const String rand1 = "4478";
const String rand2 = "6940";
const String rand3 = "3312";
const String rand4 = "1675";
String ipass;
String ipass2;
String ipass3;
String ipass4;
int code_pos = 0;
void setup() {
Serial.begin(9600);
ipass.reserve(32);
ipass2.reserve(32);
ipass3.reserve(32);
ipass4.reserve(32);
}
void loop() {
if (code_pos == 0) {
char key = keypad.getKey();
if (key) {
Serial.print(key);
if (key == '*') {
ipass = "";
}
else if (key == '#') {
if (rand1 == ipass) {
Serial.println("rand1 is correct");
code_pos = 1;
}
else {
Serial.println("rand1 is incorrect");
}
ipass = "";
}
else {
ipass += key;
}
}
}
if (code_pos == 1) {
char key2 = keypad.getKey();
if (key2) {
Serial.print(key2);
if (key2 == '*') {
ipass2 = "";
}
else if (key2 == '#') {
if (rand2 == ipass2) {
Serial.println("rand2 is correct");
code_pos = "2";
}
else {
Serial.println("rand2 is incorrect");
}
ipass2 = "";
}
else {
ipass2 += key2;
}
}
}
if (code_pos = 2) {
char key3 = keypad.getKey();
if (key3) {
Serial.print(key3);
if (key3 == '*') {
ipass3 = "";
}
else if (key3 == '#') {
if (rand3 == ipass3) {
Serial.println("rand3 is correct");
code_pos = 3;
}
else {
Serial.println("rand3 is incorrect");
}
ipass3 = "";
}
else {
ipass3 += key3;
}
}
}
if (code_pos = 3) {
char key4 = keypad.getKey();
if (key4) {
Serial.print(key4);
if (key4 == '*') {
ipass4 = "";
}
else if (key4 == '#') {
if (rand4 == ipass4) {
Serial.println("rand4 is correct");
code_pos = 4;
}
else {
Serial.println("rand4 is incorrect");
}
ipass4 = "";
}
else {
ipass4 += key4;
}
}
}
}
Your post was MOVED to its current location as it is more suitable.
Do you have a question ?
this is supposed to be a 4-stage password that you enter in order but it skips around and i have zero clue how to fix it
my only guess is since its the same function over and over its randomly picking an else statement
problem solved. i neglected to notice that a couple of statements had only one = instead of ==
using an array and an index like code_pos would make your code shorter...