Button press sequence

hello!

I am trying to make a door lock using 4 buttons but i want it to reset if a wrong button is pressed
eg if code is 1234 and one presses 13 then it should reset
meading that f one press 413423...1234 then the lock will open since the sequence was pressed right !

well, you need to have 4 buttons.
probably an UNO or any of the variants

write some code, probably use 4 LED's to show that you are getting closer to the correct sequence.

it should be simple enough.
once you get stuck come back, read the sticky note on each forum that says how to use this forum.
read #7 and then use what it tells you about how to post your code.

i have my buttons and my uno all are set.
i made a code that works with a password but it waits for all the code to be pressed for it to see if
it a correct one. making one that will check every button press till you get the correct sequence it more tricky!

here is that code:

const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;


const int greenled = 6;
const int RedLed = 8;

const int Thelock = 11;


int code = 0;
int codesize = 6;    // Number of the Code Digits
int keypress = 0;     //keypress counter
int locked = 0;
int unlock_code = 2230;   // the code

void setup()
{

  Serial.begin(9600);

  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

  pinMode(Thelock, OUTPUT);
  pinMode(RedLed, OUTPUT);
}

void loop() {

  if (locked == 0)
  {
    if (digitalRead(button1) == HIGH) //Button 1
    {

      code = (code + 1) * 2;
      keypress = keypress + 1;
      delay(350);
    }

    if (digitalRead(button2) == HIGH) //Button 2
    {
      code = (code + 1) * 3;
      keypress = keypress + 1;
      delay(350);

    }

    if (digitalRead(button3) == HIGH)
    {
      code = (code + 1) * 4;
      keypress = keypress + 1;
      delay(350);
    }



    if (digitalRead(button4) == HIGH)//Button 4
    {
      code = (code + 1) * 5;
      keypress = keypress + 1;
      delay(350);

    }


    if (keypress == codesize)
    {
      if (unlock_code == code)
      { locked = 1;
        digitalWrite(Thelock, HIGH);

        delay(150);
        digitalWrite(Thelock, LOW);
        code = 0;
        locked = 0;
        keypress = 0;
      }
      else
      {
        digitalWrite(RedLed, HIGH);
        delay(50);
        digitalWrite(RedLed, LOW);
        code = 0;
        locked = 0;
        keypress = 0;
        delay(250);
      }
    }
  }
}

I tried to read your code but found it too verbose so here are a few pointers about cleaning it up (which will help you in shorter term and us in this forum!)

  • Press CTRL-T in the IDE to get all the indentation correct - currently your IF statements are all over the place making it difficult to follow the logic
  • Write some methods to carry out verbose logic, such as setting the LEDs on or off; you can probably use a void method with a parameter or two to do this. Plenty of examples to choose from. That way you can see the woods (your core logic) for the trees (all the other guff in your main loop, obscuring what we really need to work with). This also lets you test out each method and once you're sure it's working collapse it and don't look at it again!
  • Use Serial.println("Your string") to print something followed by a new line; this gets rid of all those "\n" characters you are printing which obscures everything else
  • Use COMMENTS!!! A few lines of comments goes a long way to helping your future self in understanding your code (in 3 months' time for example) as well as us all right now. Pretty code is code you want to read, follow and debug.

I hope you take this in the constructive manner it is intended, we all had to start somewhere in our coding but if you get into good habits early on your will thank yourself that you did so - believe me, I speak from long coding experience.

So tidy up that code by editing your post, and ideally add a few comments in first and replace it, then we can inspect it without issues :cold_sweat:

Try googling "state machine". Or check out sparkfun.com, I think that is where I read a good tutorial about state machines.

I think that is the perfect solution to your problem.

Gareth

The dirty way is to make a flag for every step in the pin sequence. If the number is correct, move to step 2. If step 2 is wrong, start over again. Etc.

dorvakta:
The dirty way is to make a flag for every step in the pin sequence. If the number is correct, move to step 2. If step 2 is wrong, start over again. Etc.

And that is the exactly wrong way to make a combination lock!


OK, fair enough, you did say the "dirty way". Perhaps you might care then to explain why it is wrong, and save me the trouble. :grinning:

I see that you've cleaned up that code you posted (but best not to PM users here, it was pure chance I saw I had a message).

Problems I can see is that you always wait until the user has pushed the number of digits in the code - what happens if the user realises he's made an error after button push #2? What if he never pushes button #3?

On a side note, for this sort of application a button matrix is ideal as it has 0 - 9 plus hash and star which you can use to allow the user to start over or enter what he's done already. Just like a bank cash machine with CLEAR and ENTER buttons.

That way you can wait until the user presses (the equivalent of) ENTER or CLEAR and take the required logic at that time. It's what they were built for!

And, true to form, I have a YouTube video #13 dedicated to this topic! (URL in the footer of this message).

If you decide a matrix is not for you try and work out (on paper) what the sequence of events should/could be for a user when he presses some / all / too few / too many of your buttons. Then post back with your stumbling blocks!

code lock i am making has 4 buttons... and all they have to do is to write the correct sequence
every wrong move reset the sequence till its pressed wight.

the previous code works quite ok. but have a new one but seems the " count = count + 1;"
dont work. can't see why.

int key = 0;
int Solved = 0;
int count = 2;
char code[8] = "ttabcd";  //first 2 chars are space holders only
int keyCounter[6] = {0, 0, 0, 0} ; // Set key flag

void setup() {

  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(13, OUTPUT);


  for (int i = 2; i <= 6; i++)
  {
    pinMode(i, INPUT);   // for D2 and up
  }
}

void loop()
{

  if (Solved == 0)
  {
    while (count != 6)
    {
      for (int i = 2; i <= 6; i++)
      {
        key = digitalRead(i);
        delay(40);

        if (key == LOW && keyCounter[i] == 1) {
          keyCounter[i] = 0;
          break;
        }
        if (key == HIGH && keyCounter[i] == 0)
        {
          digitalWrite(8, HIGH);
          delay(20);
          digitalWrite(8, LOW);
          delay(330);


          switch (i)
          {
            case 1:
              if (count == 2 && code[i] == 'a')
              {
                count = count + 1;

              }

              else
              {
                count = 2;
              }
              keyCounter[i] = 1;
              break;

            case 2:
              if (count == 3 && code[i] == 'c')
              {
                count = count + 1;

              }
              else
              {
                count = 2;
              }
              keyCounter[i] = 1;
              break;
            case 3:
              if (count == 4 && code[i] == 'c')
              {
                count = count + 1;

              }
              else
              {
                count = 2;
              }
              keyCounter[i] = 1;
              break;

            case 4:
              if (count == 5 && code[i] == 'b')
              {
                count = count + 1;

              }
              else
              {
                count = 2;
              }
              keyCounter[i] = 1;
              break;


          }
        }
      }
    }
  }
  if (count == 6)
  {
    digitalWrite(9, HIGH);
    count = 2;
    delay(1000);
    digitalWrite(9, LOW);
    Solved = 0;

  }

  else
  {

    Solved = 0;

  }
}

byronas:
every wrong move reset the sequence till its pressed wight.

And as I pointed out earlier - somewhat cryptically (of course :grinning: ) - that actually makes it easier to crack!

its not for a high security thing. its for a game.

Well, that's alright then. :grinning:

hi do u have a sketch?

Paul__B:
And as I pointed out earlier - somewhat cryptically (of course :grinning: ) - that actually makes it easier to crack!

Just so long as you don't tell the person entering the code that the sequence has been reset.

That method is exactly what is used on most intruder alarm panels, you just keep pressing numbers until the correct sequence is added, but you never know if you enter one not in the correct sequence. You could, of course, put a limit on how many key presses are allowed, and a "locked out" timer before you can try again. Each time you get locked-out, the time increases exponentially.

Of course, if you give a "uh-huh" if a digit is wrong, you will know you have to start over, so you remember what you entered last time, and try another digit where you got it wrong. Now that would be easy to crack.