#define PASSWORD_SIZE 3
enum State {
LOCKED,
UNLOCKED
};
const char* const stateText[] = {"Locked", "Unlocked"};
State state = LOCKED;
State lastState = LOCKED;
const char myPassword[PASSWORD_SIZE] = {'a', 'b', 'c'};
byte userPassword[PASSWORD_SIZE] = {NULL};
bool gotPassword = false;
int index = 0;
void setup()
{
Serial.begin(9600);
Serial.println(F("Enter Password to Start..."));
}
void loop()
{
if (state == LOCKED)
{
if (Serial.available())
{
userPassword[index++] = Serial.read();
if (index >= PASSWORD_SIZE)
{
gotPassword = true;
index = 0;
}
}
if (gotPassword)
{
bool matched = true;
for (int i = 0; i < PASSWORD_SIZE; i++)
{
if (userPassword[i] != myPassword[i])
{
matched = false;
}
}
if (matched)
{
Serial.println(F("ok"));
state = UNLOCKED;
}
else
{
Serial.println("Bad Password");
memset(userPassword, NULL, PASSWORD_SIZE);
}
gotPassword = false;
}
}
else if(state == UNLOCKED)
{
//
// do your unlocked stuff here
//
if (Serial.available())
{
Serial.read();
state = LOCKED;
}
}
if(state != lastState)
{
Serial.println(stateText[state]);
if(state == LOCKED)
{
Serial.println(F("Enter Password to Start..."));
}
else if (state == UNLOCKED)
{
// do state change to UNLOCKED here
}
}
lastState = state;
}
Now this code actually works but could I not just plug in LED’s anywhere on the breadbored ? I only want 2 LED’s on the board a red an a green to either show locked or unlocked ??? any helps ???would appreciate it highly
well it works amazing like if I type in abc it says unlocked but anything else it will say locked an it is a one time thing soo yea but even when it is unlocked the led light is not cutting on so what am I doing wrong an where should I input the digitalWrite(13,HIGH); at ???
Actually if we were to try and copy your code and compile it, it wouldn't work for us. See how your code turns into italics halfway through? And the 'open-bracket i close-bracket' symbols (that BulldogLowell pointed out) are missing.
This is why there are code tags. If you edit your original post, you can add them in now. And the code will work again.
it is correct because it runs without any error message also like I even changed it again from what there talking about I have fixed it I just need to know where to insert where I cut my led on that's all ?? any help
if(state != lastState) // < THIS IS THE START OF A STATE CHANGE DETECTION BLOCK OF CODE
{
Serial.println(stateText[state]);
if(state == LOCKED)
{
Serial.println(F("Enter Password to Start..."));
// <<<<<<<< CONSIDER TURNING ON YOUR "LOCKED" LED HERE
}
else if (state == UNLOCKED)
{
// do state change to UNLOCKED here
// <<<<<<<< CONSIDER TURNING ON YOUR "UN-LOCKED" LED HERE
}
}
lastState = state;
consider turning your locked led off when your unlocked led is illuminated and vice-versa!
TRY SOMETHING AND POST It wether it works or not!!!