Serial monitor as input

Help needed:
The code compile but there are several issus:
Everything keeps scrolling down in the serial monitor, is there any way tto have everything displayed so that it looks 'static' and will just change when the operator enters something?
It doe not compare correctly the code and the operator inputs.
</>
/*
The idea of this sketch is to chose a 3 character solution code entered as code1,code2 and code3.
We would then take a serial input from the computer keyboard through the serial monitor and compare the operator entry to the solution code.
If the entry matches the solution display 'code accepted, you may proceed' if not display 'Intruder alert'
*/

int i=1;
char code1= 'A';
char code2= 'B';
char code3= 'C';
char input[]={1,2,3};

void setup() {
Serial.begin(9600); //establishes communication over the serial cable

Serial.println("Type the correct code");

}

void loop() {
if(Serial.available()>0){
for(i=1;i<4;i++)
{ input = Serial.read();

  • }*

  • }*

  • Serial.print("your first input is");*

  • Serial.println(input[1]);*

  • Serial.print ("your second input is");*

  • Serial.println(input[2]);*

  • Serial.print ("your third input is");*

  • Serial.println(input[3]);*

  • if ((input[1]==code1)&&(input[2]==code2)&&(input[3]==code3))*

  • {*

  • Serial.println("Code accepted. You may proceed.");*

  • }*

  • else*

  • {*

  • }*
    }
    </>

Please follow the advice on posting code in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and using code tags when posting code here

Firstly, you should enclose the code using the </> in the edit menu so that it displays properly. Certain combinations of characters will put the display into italics or turn in emoticons, which means your code cannot be read properly. You should edit the original post to do this.

Understand that loop() will run hundreds of times a second, so anything that is just in loop() will, for example, print every time through loop().

This means that you should only be doing stuff if there is a character on the serial port to process.

loop()
{
  if (serial.available())
  {
   // do your stuff here
  }
}

Additionally, don't expect that all your serial input will be available at once, which means your for() loop is flawed. Rather, keep track of which character you are at and when you have all of them the process the code.

int curIndex = 0;

loop()
{
  if (serial.available())
  {
    input[curIndex++] = Serial.read();

    if (curIndex == 3)
    {
       curIndex = 0;   // rest for next time
       // do whatever with input[]
    }
  }
}

And finally a note on style. just as input is an array, code could be as well and you can then check the code in a for() loop.

bool match = true;

for (int i = 0; i<3; i++)
  match = match && (input[i] == code[i]);

I would also declare a constant value for the code size in characters and use that throughout the code rather than the magic number '3'.

Thank you very much marco_c for the advice. I see many good things here....I will work on it now!

</>
/*
The idea of this sketch is to chose a 3 character solution code entered as code1,code2 and code3.
We would then take a serial input from the computer keyboard through the serial monitor and compare the operator entry to the solution code.
If the entry matches the solution display 'code accepted, you may proceed' if not display 'Intruder alert'
*/

int curIndex=1;

char input[]={1,2,3};
char code[]={1,2,3};
bool match = true;
char code1= 'A';
char code2= 'B';
char code3= 'C';

void setup()
{
Serial.begin(9600); //establishes communication over the serial cable

Serial.println("Type the correct code");

}

void loop()
{
if(Serial.available())
{

input[curIndex++] = Serial.read();

if (curIndex == 4)
{
curIndex = 1; // rest for next time
// do whatever with input[]
}

for (int i = 1; i<4; i++)
{
match = match & (input == code*);*
* if(match==true)*
* {*

* Serial.println("Code accepted. You may proceed.");*
* }*
* else*
* {*
* Serial.println("Code rejected. ALERT.");*
* }*
}
}
}
</>
I think that the problem comes from the declaration of the variables and solutions for the password ABC. What I did is faulty but I do not see how to fix it yet

No.

"</> in the edit menu" not just </> typed literally into your post. If you are confused, go to the forum instructions. A link was provided to you for that purpose, in reply #1.

Your matching code should be in the if statement.
And you need to set match to turn just before the loop.
And what are code1, code2 and code3 doing?