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'
*/
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'.
</>
/*
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'
*/
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
"</> 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.