Hope you guys help me..
every time i input the expected output is correct but theirs an extra... i'm thinking of clearing Serial.Available every time a condition was done for this problem but i dont know how. i tried using Serial.flush() but its not working.
example of that extra output is when i input "abcd"(which is the correct passwrord) and verified that its correct the extra out is driven even if i only input 1 byte this how it looks like in serial monitor:
First inputed byte :
a
Matching
a
First Byte Correct
Second inputed byte :
b
Matching
b
Secont Byte Correct
Third inputed byte :
c
Matching
c
Third Byte Correct
Forth input byted :
d
Matching
d
Forth Byte Correct
CORRECT PASSWORD
First inputed byte :
Matching
a
First Byte Wrong
The BOLD words is the extra output even w/o Serial.read() it runs
below is my code you can try it~
please need help ![]()
char inputpw[4];
char correct[4] = {'a','b','c','d'}; //User-Defined Password
int bytes=0;
int correct_byte=0;
int tenge=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available()>0)
{
if(bytes==0 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("First inputed byte :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);
if(inputpw[bytes]==correct[bytes])
{
Serial.println("First Byte Correct ");
Serial.println();
bytes++;
correct_byte++;
}
else
{
Serial.println("First Byte Wrong");
Serial.println();
bytes++;
tenge=1;
}
}
else if(bytes==1 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("Second inputed byte :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);
if(inputpw[bytes]==correct[bytes])
{
Serial.println("Secont Byte Correct");
Serial.println();
bytes++;
correct_byte++;
}
else
{
Serial.println("Second Byte Wrong");
Serial.println();
bytes++;
tenge=1;
}
}
else if(bytes==2 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("Third inputed byte :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);
if(inputpw[bytes]==correct[bytes])
{
Serial.println("Third Byte Correct");
Serial.println();
bytes++;
correct_byte++;
}
else
{
Serial.println("Third Byte Wrong");
Serial.println();
bytes++;
tenge=1;
}
}
else if(bytes==3 && Serial.available()>0)
{
inputpw[bytes]=Serial.read();
Serial.println("Forth input byted :");
Serial.println(inputpw[bytes]);
Serial.println("Matching");
Serial.println(correct[bytes]);
if(inputpw[bytes]==correct[bytes])
{
Serial.println("Forth Byte Correct");
bytes++;
correct_byte++;
}
else
{
Serial.println("Forth Byte Wrong");
bytes++;
tenge=1;
}
}
if(correct_byte==4)
{
Serial.println("CORRECT PASSWORD");
Serial.println();
/* delay(500);
digitalWrite(lock, HIGH);
delay(3000);
digitalWrite(lock,LOW);*/
bytes=0;
correct_byte=0;
}
else if(tenge==1 && bytes==4)
{
Serial.println("WRONG PASSWORD");
Serial.println();
bytes=0;
tenge=0;
}
}
}