Help with setup?

#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

Moderator edit: CODE TAGS.

could I not just plug in LED’s anywhere on the breadbored ?

could I not just plug in LED’s anywhere on the breadbored breadboard ?

Not any where, see example below:

LED.png

Use
digitalWrite(13, LOW);
OR
digitalWrite(13, HIGH);
to control this LED
.

this code actually works

I bet it doesn’t. Please edit your post, select the code and click on the code icon (</>) top left above the editor.

if (userPassword != myPassword)

should be

if (userPassword[i] != myPassword[i])

how on earth did I know that?

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 works lol I just need to know where to insert the digitalWrite High at in the code to understand when it is unlocked a light comes on

it works lol

The code, as displayed in your original post, does not work for the reasons explained above.

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

HINT:

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;

That would be a good place to turn on and off a LED, I'm guessing.

it is correct because it runs without any error message

Try copying the code in your original post, pasting it into the IDE and compiling it then tell me that it is correct.

still correct an ahh bulldog i have tried ti there an its not working lol so im confused

Alright, alright, you beat me down.

I’ve fixed your first post.

Now, try it again.

thee is not a better spot to do what you want:

BIGGER HINT:

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!!!