Incorporating a 4 digit password to a 4x4 keypad, saying Correct to Serial monitor if correct or Incorrect if the password is not right

void setup() {
// put your setup code here, to run once:
DDRD=0xff;
DDRB=0x00;
PORTB=0xff;
Serial.begin(115200);
}
String key = "";

String board() {
key="";

PORTD=0b11111011;
//Serial.println(PORTB,BIN);
if(PINB==0b111110)
{Serial.println("D");key="D";}
if(PINB==0b111101)
{Serial.println("C");key="C";}
if(PINB==0b111011)
{Serial.println("B");key="B";}
if(PINB==0b110111)
{Serial.println("A");key="A";}
while(PINB !=0b111111);

PORTD=0b11110111;
//Serial.println(PINB,BIN);
if(PINB==0b111110)
{Serial.println("#");key="#";}
if(PINB==0b111101)
{Serial.println("9");key="9";}
if(PINB==0b111011)
{Serial.println("6");key="6";}
if(PINB==0b110111)
{Serial.println("3");key="3";}
while(PINB !=0b111111);

PORTD=0b11101111;
//Serial.println(PINB,BIN);
if(PINB==0b111110)
{Serial.println("0");key="0";}
if(PINB==0b111101)
{Serial.println("8");key="8";}
if(PINB==0b111011)
{Serial.println("5");key="5";}
if(PINB==0b110111)
{Serial.println("2");key="2";}
while(PINB !=0b111111);

PORTD=0b11011111;
//Serial.println(PINB,BIN);
if(PINB==0b111110)
{Serial.println("");key="";}
if(PINB==0b111101)
{Serial.println("7");key="7";}
if(PINB==0b111011)
{Serial.println("4");key="4";}
if(PINB==0b110111)
{Serial.println("1");key="1";}
while(PINB !=0b111111);
///delay(500);
return key;
}

void password(String key){

if(key == "#")
{Serial.println("Enter Password");
String passkey="";
for(int i=0;i<4;i++)
{
while(PINB==0b111111);
String key1=board();
passkey+=key1;
delay(10);

}
Serial.println(passkey);
key="";

}
}

//////////////////////////////////////////////////
This is the .h library

#include "board.h"

void loop(){
String key=board();
password(key);
key="";
}

Code tags please! Read the forum guide in the sticky post!

Please also post 'board.h' in code tags.

The problem writing code in this manner is it makes it very hard to spot errors and doesn’t really give any benefit over the simplier high level C type stuff .

Why don't you use the Keypad library? Your code would be much more readable and much easier to debug.

Except as an academic assignment, which is probably what it is.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.