Should be simple just to make an array that defines in which order the buttons should be pressed.
const byte PINCODE_LENGTH = 4;
const byte PINCODE[PINCODE_LEGTH] = {A0, A2, A1, A3};
const unsigned int PINCODE_TIMEOUT = 1000;
usigned long lastPincodePress = 0;
byte pincodeIndex = 0;
void loop()
{
//Check if the pincode entering has timed out..
unsigned long currentMilis = millis();
if (currentMillis - lastPincodePress > PINCODE_TIMEOUT) pincodeIndex = 0;
//Read next pincode button
if (readButton(PINCODE[pincodeInbex])) {
lastPinodePress = currentMillis;
pincodeIndex++;
if (pincodeIndex == PINCODE_LENGTH) {
pincodeIndx = 0;
//Successfully received the pincode!
}
}
}
bool readButton(byte btnPin) {
byte hit=255, count=0;
for (byte i = 0; i < PINODE_LENGTH; i++) if (digitalRead(PINCODE[i]) == HIGH) {
hit = i;
count++;
}
retrun (hit = btnPin) && (count == 1);
}
This will read the buttons in the sequence defined by "PINCODE". If no button is pressed for 1 second (1000 milliseconds), the pincode typing is reset to first button. Pseudocode and deliberate typos included for free - hope OP will think, not just copy! ![]()