Author Topic: Arduino code lock  (Read 14353 times)

0 Members and 1 Guest are viewing this topic.

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Arduino code lock
« on: May 28, 2017, 01:20:14 pm »
I am making a Button lock for school, I need it to send an output when one of two codes get pressed (for example: 1234 = on, 4321 = on, 4231 = off, 1432 = off) here's the setup: https://goo.gl/photos/WpGmVAVoiJkg5LjD6
button 1 = 8
button 2 = 9
button 3 = 10
button 4 = 11
red led = 4
green = 12
if you have any idea, please help me!
*Insert cool inspirational text here*
 

Offline CM800

  • Frequent Contributor
  • **
  • Posts: 882
  • Country: 00
Re: Arduino code lock
« Reply #1 on: May 28, 2017, 02:25:13 pm »
Simple really:

Allow each button to add a character to a string:

Button 1 = "1"
Button 2 = "2"
Button 3 = "3"
Button 4 = "4"

then read the string & check against known ones, e.g.:

if (enteredPassword == "1234" || enteredPassword == "4321")
{
      digitalWrite(Output, HIGH);
}
else
{
     digitalWrite(Output, LOW);
}
 
The following users thanked this post: lolimpol

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Arduino code lock
« Reply #2 on: May 28, 2017, 02:40:59 pm »
 

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Re: Arduino code lock
« Reply #3 on: May 28, 2017, 03:20:49 pm »
*Insert cool inspirational text here*
 

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Re: Arduino code lock
« Reply #4 on: May 28, 2017, 03:22:58 pm »
Simple really:

Allow each button to add a character to a string:

Button 1 = "1"
Button 2 = "2"
Button 3 = "3"
Button 4 = "4"

then read the string & check against known ones, e.g.:

if (enteredPassword == "1234" || enteredPassword == "4321")
{
      digitalWrite(Output, HIGH);
}
else
{
     digitalWrite(Output, LOW);
}
But how would I format that? (I am a complete noob :D)
« Last Edit: May 28, 2017, 03:33:02 pm by lolimpol »
*Insert cool inspirational text here*
 

Offline CM800

  • Frequent Contributor
  • **
  • Posts: 882
  • Country: 00
Re: Arduino code lock
« Reply #5 on: May 28, 2017, 05:38:59 pm »
Simple really:

Allow each button to add a character to a string:

Button 1 = "1"
Button 2 = "2"
Button 3 = "3"
Button 4 = "4"

then read the string & check against known ones, e.g.:

if (enteredPassword == "1234" || enteredPassword == "4321")
{
      digitalWrite(Output, HIGH);
}
else
{
     digitalWrite(Output, LOW);
}
But how would I format that? (I am a complete noob :D)

I've given you a good hint here!

...but I dare say that it's YOUR course, not mine.

The point of this is for YOU to learn.

Now, go on youtube, watch and follow along with an arduino tutorial series and LEARN

Play around etc...

:)
 

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Re: Arduino code lock
« Reply #6 on: May 28, 2017, 05:54:55 pm »
yes I understand, but i'm kind of under pressure, and i'm really really dumb...
I just download code and put it on an arduino. I really want to learn, but now is not the time!
*Insert cool inspirational text here*
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: Arduino code lock
« Reply #7 on: May 28, 2017, 07:52:55 pm »
Cheat on homework isn't a nice thing on Netherlands.........
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Arduino code lock
« Reply #8 on: May 28, 2017, 09:32:46 pm »
Simple really:

Allow each button to add a character to a string:

Button 1 = "1"
Button 2 = "2"
Button 3 = "3"
Button 4 = "4"

then read the string & check against known ones, e.g.:

if (enteredPassword == "1234" || enteredPassword == "4321")
{
      digitalWrite(Output, HIGH);
}
else
{
     digitalWrite(Output, LOW);
}
But how would I format that? (I am a complete noob :D)

Just watch out - you don't compare strings/arrays like that in C/C++/Arduino. They will be at different addresses in memory - use strcmp() or memcmp() instead...  ...unless of course you use the String class, as shown in https://www.arduino.cc/en/Tutorial/StringComparisonOperators
« Last Edit: May 28, 2017, 11:07:14 pm by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Arduino code lock
« Reply #9 on: May 28, 2017, 11:45:38 pm »
I really want to learn, but now is not the time!
Have you heard the expression "necessity is the mother of invention"? Being under pressure is exactly the time to learn!

Here's some working code I made just for you. This is only the second program I have written for Arduino, so it was a learning experience for me too.

Just one catch though - there's a deliberate mistake in it. If you can figure out what it is then you're good to go!

Code: [Select]
//  4 button combination lock for Arduino 
//  by Bruce Abbott       [http://www.bhabbott.net.nz]

#define button1  8       
#define button2  9
#define button3  10
#define button4  11   

#define KeyLed   3    // button press indicator  (long flash = 4th digit entered)
#define RedLed   4    // on when locked, off when unlocked   
#define GreenLed 12   // on when unlocked, off when locked

#define LOCKCODE1   "1234"
#define LOCKCODE2   "4321"

#define UNLOCKCODE1 "4231"
#define UNLOCKCODE2 "1432"

int bS1 = 0;         // current state of button
int lbS1 = 0;        // previous state of button

int bS2 = 0;         
int lbS2 = 0;

int bS3 = 0;         
int lbS3 = 0;

int bS4 = 0;         
int lbS4 = 0;

int counter = 0;       // number of buttons pressed in current sequence

int lockstate = 0;     // 0 = unlocked, 1 locked

char code[] = "    ";  // string to hold 4 digit code

void setup(){
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(KeyLed, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(GreenLed, OUTPUT);
  Serial.begin(9600);
//  digitalWrite(GreenLed,HIGH);
}

void loop(){

    bS1 = digitalRead(button1);
    bS2 = digitalRead(button2);
    bS3 = digitalRead(button3);
    bS4 = digitalRead(button4);

    if (bS1 != lbS1) {
       if ((bS1 == HIGH) && (bS2 == LOW) && (bS3 == LOW) && (bS4 == LOW)) {
          code[counter]='1';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
       }
       lbS1 = bS1;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

    if (bS2 != lbS2) {
       if ((bS1 == LOW) && (bS2 == HIGH) && (bS3 == LOW) && (bS4 == LOW)) {
          code[counter]='2';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
          }
       lbS2 = bS2;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

    if (bS3 != lbS3) {
       if ((bS1 == LOW) && (bS2 == LOW) && (bS3 == HIGH) && (bS4 == LOW)) {
          code[counter]='3';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
       }
       lbS3 = bS3;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

    if (bS4 != lbS4) {
       if ((bS1 == LOW) && (bS2 == LOW) && (bS3 == LOW) && (bS4 == HIGH)) {
          code[counter]='4';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
       }
       lbS4 = bS4;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

   if (counter == 4) {
      if (!strcmp(code,UNLOCKCODE1) || !strcmp(code,UNLOCKCODE2)) {
         lockstate = 0;
         digitalWrite(GreenLed,HIGH);
         digitalWrite(RedLed,LOW);
      }
      if (!strcmp(code,LOCKCODE1) || !strcmp(code,LOCKCODE2)) {
         lockstate = 1;
         digitalWrite(GreenLed,LOW);
         digitalWrite(RedLed,HIGH);
      }
      counter = 0;                  // reset digit counter, ready to enter new code
      strcpy(code,"    ");          // reset code string
      digitalWrite(KeyLed,HIGH);
      delay(400);                   // long flash = 4 digits entered
      digitalWrite(KeyLed,LOW);
   }
    // end main loop \
}


« Last Edit: May 28, 2017, 11:48:07 pm by Bruce Abbott »
 
The following users thanked this post: lolimpol

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Arduino code lock
« Reply #10 on: May 28, 2017, 11:51:27 pm »
What is supposed to happen if you enter 1234 after entering 1 (or some other buttons)?

Presumably, the lock is meant to open, rather than remaining perma-locked... ;)
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Arduino code lock
« Reply #11 on: May 29, 2017, 12:33:53 am »
What is supposed to happen if you enter 1234 after entering 1 (or some other buttons)?
Once you start, you have to keep going until exactly 4 digits have been entered. That's why I added an LED to show when the complete code has been received (as well as when each digit is pressed). I could have used the green LED for this, but I presumed that red means on and green means off.

If lolimpol wanted it to work some other way then he should have told us...


 
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: Arduino code lock
« Reply #12 on: May 29, 2017, 12:47:49 am »
Sorry. Wasn't meant as a criticism of your code (though I can see how you read it that way), but rather a specification clarification question.
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Arduino code lock
« Reply #13 on: May 29, 2017, 02:01:19 am »
Once you get it working with 1,2,3,4 as the combination, see if you can you have 1,1,1,1 or 1,2,2,1 as the valid combination.

Unless you you think really really hard about how your code processes the button presses it is quite hard to get this to work cleanly.
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline janekm

  • Supporter
  • ****
  • Posts: 515
  • Country: gb
Re: Arduino code lock
« Reply #14 on: May 29, 2017, 02:36:38 am »
As an extra credit question, why shouldn't you use strcmp in this case (assuming it's a lock on something worth protecting)?   ;D
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Arduino code lock
« Reply #15 on: May 29, 2017, 05:35:02 am »
As an extra credit question, why shouldn't you use strcmp in this case (assuming it's a lock on something worth protecting)?   ;D

You can get by with just two bytes of state if you really want to secure it. At least you can then truly verify/audit all the code (if it is really worth protecting)... ;D
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Re: Arduino code lock
« Reply #16 on: May 29, 2017, 06:34:42 am »
I really want to learn, but now is not the time!
Have you heard the expression "necessity is the mother of invention"? Being under pressure is exactly the time to learn!

Here's some working code I made just for you. This is only the second program I have written for Arduino, so it was a learning experience for me too.

Just one catch though - there's a deliberate mistake in it. If you can figure out what it is then you're good to go!

Code: [Select]
//  4 button combination lock for Arduino 
//  by Bruce Abbott       [http://www.bhabbott.net.nz]

#define button1  8       
#define button2  9
#define button3  10
#define button4  11   

#define KeyLed   3    // button press indicator  (long flash = 4th digit entered)
#define RedLed   4    // on when locked, off when unlocked   
#define GreenLed 12   // on when unlocked, off when locked

#define LOCKCODE1   "1234"
#define LOCKCODE2   "4321"

#define UNLOCKCODE1 "4231"
#define UNLOCKCODE2 "1432"

int bS1 = 0;         // current state of button
int lbS1 = 0;        // previous state of button

int bS2 = 0;         
int lbS2 = 0;

int bS3 = 0;         
int lbS3 = 0;

int bS4 = 0;         
int lbS4 = 0;

int counter = 0;       // number of buttons pressed in current sequence

int lockstate = 0;     // 0 = unlocked, 1 locked

char code[] = "    ";  // string to hold 4 digit code

void setup(){
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(KeyLed, OUTPUT);
  pinMode(RedLed, OUTPUT);
  pinMode(GreenLed, OUTPUT);
  Serial.begin(9600);
//  digitalWrite(GreenLed,HIGH);
}

void loop(){

    bS1 = digitalRead(button1);
    bS2 = digitalRead(button2);
    bS3 = digitalRead(button3);
    bS4 = digitalRead(button4);

    if (bS1 != lbS1) {
       if ((bS1 == HIGH) && (bS2 == LOW) && (bS3 == LOW) && (bS4 == LOW)) {
          code[counter]='1';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
       }
       lbS1 = bS1;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

    if (bS2 != lbS2) {
       if ((bS1 == LOW) && (bS2 == HIGH) && (bS3 == LOW) && (bS4 == LOW)) {
          code[counter]='2';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
          }
       lbS2 = bS2;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

    if (bS3 != lbS3) {
       if ((bS1 == LOW) && (bS2 == LOW) && (bS3 == HIGH) && (bS4 == LOW)) {
          code[counter]='3';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
       }
       lbS3 = bS3;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

    if (bS4 != lbS4) {
       if ((bS1 == LOW) && (bS2 == LOW) && (bS3 == LOW) && (bS4 == HIGH)) {
          code[counter]='4';
          counter = counter + 1;
          digitalWrite(KeyLed,HIGH);
          Serial.println(code);
       }
       lbS4 = bS4;
       delay(100);
       digitalWrite(KeyLed,LOW);
    }

   if (counter == 4) {
      if (!strcmp(code,UNLOCKCODE1) || !strcmp(code,UNLOCKCODE2)) {
         lockstate = 0;
         digitalWrite(GreenLed,HIGH);
         digitalWrite(RedLed,LOW);
      }
      if (!strcmp(code,LOCKCODE1) || !strcmp(code,LOCKCODE2)) {
         lockstate = 1;
         digitalWrite(GreenLed,LOW);
         digitalWrite(RedLed,HIGH);
      }
      counter = 0;                  // reset digit counter, ready to enter new code
      strcpy(code,"    ");          // reset code string
      digitalWrite(KeyLed,HIGH);
      delay(400);                   // long flash = 4 digits entered
      digitalWrite(KeyLed,LOW);
   }
    // end main loop \
}
Perfect! I will do my absolute best to find it!
edit: I think I found it,
Code: [Select]
      if (!strcmp(code,LOCKCODE1) || !strcmp(code,LOCKCODE2)) {
         lockstate = 1;
         digitalWrite(GreenLed,LOW);
         digitalWrite(RedLed,HIGH);
      }
      counter = 0;                  // reset digit counter, ready to enter new code
      strcpy(code,"    ");          // reset code string
      digitalWrite(KeyLed,HIGH);
      delay(400);                   // long flash = 4 digits entered
      digitalWrite(KeyLed,LOW);
   }} //<-- one more closing bracket? that's it I think
    // end main loop \
}
« Last Edit: May 29, 2017, 06:39:23 am by lolimpol »
*Insert cool inspirational text here*
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Arduino code lock
« Reply #17 on: May 29, 2017, 09:44:19 am »
You could do something like this, but it would be a little too slick for homework... somebody would know you are pulling their leg.

Code: [Select]
const int code1 = 1*(5*5*5) + 2*(5*5) + 3*5 + 4; // Code of 1234
const int code2 = 4*(5*5*5) + 3*(5*5) + 2*5 + 1; // Code of 4321

int readButtons(void) {
   static int buttons;
   int buttonsLast;

   buttonsLast = buttons;
   buttons = 0;
   if(digitalRead(btn1Pin)) buttons |= 1;
   if(digitalRead(btn2Pin)) buttons |= 2;
   if(digitalRead(btn3Pin)) buttons |= 4;
   if(digitalRead(btn4Pin)) buttons |= 8;

   if(buttonsLast != 0)
     return 0;

   switch(buttons) {
case 1:  return 1;
case 2:  return 2;
case 4:  return 3;
case 8:  return 4;
        default: return 0;
   }
}

void loop() {
   static unsigned int state = 0;
   static int timeout = 0;
   int buttons;

   buttons = readButtons(); // Returns 0 (no button) or 1 through 4
   if(buttons == 0) {
     if(timeout == timeout_max)
       state = 0;
     else
       timeout++;
   } else {
     timeout = 0;
     state = state * 5 + buttons;
   }

   if (state == code1 || state == code2) {
     digitalWrite(errorPin, LOW);
     digitalWrite(unlockPin,HIGH);
   } else if( state > 5*5*5) {
     digitalWrite(errorPin, HIGH);
     digitalWrite(unlockPin,LOW);
     state = 5*5*5*5;
   } else {
     digitalWrite(errorPin, LOW);
     digitalWrite(unlockPin,LOW);
   }
   delay(100); // pause for 1/10th of a second
}

You would need to add your own setup() and set the pin constants and so on
« Last Edit: May 29, 2017, 10:47:21 am by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline CM800

  • Frequent Contributor
  • **
  • Posts: 882
  • Country: 00
Re: Arduino code lock
« Reply #18 on: May 29, 2017, 09:58:07 am »
You could do something like this, but it would be a little too slick for homework... somebody would know you are pulling their leg.

I'd agree with that, haha.


What did you mean about the problem with comparing strings that I shown earlier??

I've never had a problem with it!

 :scared:
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: Arduino code lock
« Reply #19 on: May 29, 2017, 10:14:06 am »
You could do something like this, but it would be a little too slick for homework... somebody would know you are pulling their leg.

I'd agree with that, haha.


What did you mean about the problem with comparing strings that I shown earlier??

I've never had a problem with it!

 :scared:

Just the usual issue with beginners comparing points for equivilence, not comparing the contents that they point to.

Code: [Select]
#include <stdio.h>

char test[5] = "abcd";

int main(int argc, char *argv[]) {

   if(test == "abcd")
     printf("Matched '=='\n");
   else
     printf("Did not match '=='\n");

   if(strcmp(test,"abcd")==0)
     printf("Matched 'strcmp()'\n");
   else
     printf("Did not match 'strcmp()'\n");

   return 0;
}

gives

Code: [Select]
C:\Users\Hamster\Desktop\string>strtest
Did not match '=='
Matched 'strcmp()'

C:\Users\Hamster\Desktop\string>
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Re: Arduino code lock
« Reply #20 on: May 29, 2017, 05:35:09 pm »
well, the code isn't the homework... I could have made a drawing basically, but I decided to be creative.
*Insert cool inspirational text here*
 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: Arduino code lock
« Reply #21 on: May 29, 2017, 06:24:28 pm »
edit: I think I found it,
Code: [Select]
      if (!strcmp(code,LOCKCODE1) || !strcmp(code,LOCKCODE2)) {
         lockstate = 1;
         digitalWrite(GreenLed,LOW);
         digitalWrite(RedLed,HIGH);
      }
      counter = 0;                  // reset digit counter, ready to enter new code
      strcpy(code,"    ");          // reset code string
      digitalWrite(KeyLed,HIGH);
      delay(400);                   // long flash = 4 digits entered
      digitalWrite(KeyLed,LOW);
   }} //<-- one more closing bracket? that's it I think
    // end main loop \
}
Congratulations! Now for extra points, explain why the closing bracket on the last line isn't doing the job.

Hint

 
 

Offline lolimpolTopic starter

  • Contributor
  • Posts: 42
  • Country: nl
  • What level of irony are you on right now?
Re: Arduino code lock
« Reply #22 on: May 29, 2017, 06:37:06 pm »
edit: I think I found it,
Code: [Select]
      if (!strcmp(code,LOCKCODE1) || !strcmp(code,LOCKCODE2)) {
         lockstate = 1;
         digitalWrite(GreenLed,LOW);
         digitalWrite(RedLed,HIGH);
      }
      counter = 0;                  // reset digit counter, ready to enter new code
      strcpy(code,"    ");          // reset code string
      digitalWrite(KeyLed,HIGH);
      delay(400);                   // long flash = 4 digits entered
      digitalWrite(KeyLed,LOW);
   }} //<-- one more closing bracket? that's it I think
    // end main loop \
}
Congratulations! Now for extra points, explain why the closing bracket on the last line isn't doing the job.

Hint
because there's a ` before the text?
(the code didn't work...)
*Insert cool inspirational text here*
 

Offline stj

  • Super Contributor
  • ***
  • Posts: 2155
  • Country: gb
Re: Arduino code lock
« Reply #23 on: May 29, 2017, 06:50:40 pm »
so much posts over such a simple task.

you setup a 4byte fifo buffer,
each time you read a keypress you shift the buffer left 1 position and put the keypress in the rightmost place.
then you call a compare routine to see if it matches the target string.


that way, you could have 145343222341234 and it would work.
if you dont do it that way, you either need a [clear] button to start the entry, or you need a timeout function to clear the buffer if nothing is entered for a few seconds.
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5026
  • Country: ro
  • .
Re: Arduino code lock
« Reply #24 on: May 29, 2017, 06:59:31 pm »
Use an array of 4 bytes / char , initialize them with 0 at the start.
Every time a key is pressed ,  move the four bytes to the left by one  ... ex value[0] = value[1]; value[1] = value[2]; value[2] = value[3];
put the read button in the last position ... value [3] = button;
optionally if the button is "C" for clear or reset , set all the 4 values in the array to 0
After every key is pressed or when a special key is pressed (an enter for example) ... do  value[0] * 1000 + value[1] * 100 + value[2] * 10 + value[3] ; and you have a variable that is a number and can be stored in 2 bytes (unsigned int or something like that).
For reverse , you simply do value[3] * 1000 + value[2]*100 + value[1] * 10 + value[0] and compare it with another pre-stored variable.

You can compare that variable with a number you store in memory in the microcontroller and if they match then you unlock the thing.

This adds more cpu cycles at every key press (as you have to shift those 4 bytes every time to put the last key press in the 4th spot in the array) but saves memory since you don't have to store the password inside as a 4 character string, you can save it as a 2 byte constant number  , and you also don't have to compare two strings .. you compare two numbers.

If you want to reduce memory usage even further, you can use a single 2 byte variable and use 4 bits for every key press .. 4 bits give you ability to store 0..15 in them , so you can easily store 0..9 in 4 bits.
Every time a button is pressed, you shift the value to the left by 4 bits and you add your button (0..9) to the end of the unsigned 2 byte variable.
After every change or after user presses reset , you compare the 2 byte variable to another pre-stored variable in memory.

Example ... let's say the code is   1639   ... 1 in binary as 0001  , 6 is 0110 , 3 is 0011 and 9 is 1001 so  you will have to compare whatever is entered against 00010110 00111001  or 5689 in decimal (stored as unsigned integer 2 bytes.

So  now yuo have a unsigned integer variable that's initially 0.
Code: [Select]
User presses  "1"  ... value = value << 4 + 1  === >      0 in binary is 0000 0000 0000 0000  and shifted to left by 4 bits is 0000 0000 0000 0000  and then you add 1 :  0000 0000 0000 0001  (1)
User presses  "6" ...  value = value << 4 + 6  === >      1 in binary is 0000 0000 0000 0001  and shifted to left by 4 bits is 0000 0000 0001 0000  and then you add 6 :  0000 0000 0001 0110 (22)
User presses  "3" ...  value = value << 4 + 3  === >     22 in binary is 0000 0000 0001 0110  and shifted to left by 4 bits is 0000 0001 0110 0000  and then you add 3 :  0000 0001 0110 0011 (355)
User presses  "9" ...  value = value << 4 + 9  === >    355 in binary is 0000 0001 0110 0011  and shifted to left by 4 bits is 0001 0110 0011 0000  and then you add 9 :  0001 0110 0011 1001 (5689)
After every digit (or when user presses ENTER) you compare the value with the value you originally stored (5689) and you can see then when user eventually presses 1639 in sequence, the value computed will be 5689 and will match with the pre-stored value.
Shifting and adding is done super fast, just a couple of cycles or something like that.. faster than moving bytes in arrays and since the variable can only store 4x4 bits at any time, it works perfectly for 4 digit codes. If you want longer pin codes, you'd have to resort to 4 byte variables (which would be able to store 8 digits) and you'd probably have to use a CLEAR or reset button to reset the variable to 0 if user makes a mistake.
With the above code that just keeps in memory the last 4 digits, if user makes a mistake he can simply start from the beginning knowing that only the last 4 button presses are memorized (for example if user pushes 8211639 and then ENTER, it would be a correct pin since only the last four numbers were memorized.

IF you want the code to work the other way around (for example if you want 9361 to also work, you just store a second value in memory (9361 using 4 bits for every digit) : 1001 0011 0110 0001  or 37,729 in decimal (unsigned 2 bytes)
and now every time a button is pushed, you shift the previously memorized value to the right by 4 bits and you store the button in the first 4 bits :

reverse_value = reverse_value >> 4;
reverse_value = reverse_value + (button << 12)  - shift [0..9] to the left 12 bits and then add value to it ..

« Last Edit: May 29, 2017, 07:10:05 pm by mariush »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf