Pages: [1]   Go Down
Author Topic: Password Protected Coin Bank  (Read 812 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As the title explains, I am creating a password protected coin bank using pushbuttons as the hardware/software communication. There are a total of 7 buttons, 1-4 being for the password, 5 for setting the password, 6 for entering it, and 7 for resetting to the main menu. I used LED's to verify (see very bottom of code) to verify if I entered the password correctly. The problem is, if I enter the first character correctly while entering the last 3 incorrectly... all the LEDs light up.

Before I put down the code I am using I need to know...

Do pushbuttons act like clocks?
OR
If you push a button, it will keep that signal tied HIGH forever?


int ledPin1 = 9;
int ledPin2 = 10;
int ledPin3 = 11;
int ledPin4 = 12;
int inputPin1 = 1;  // Passcode Slot 1
int inputPin2 = 2;  // Passcode Slot 2
int inputPin3 = 3;  // Passcode Slot 3
int inputPin4 = 4;  // Passcode slot 4
int inputPin5 = 5;  // Set Password
int inputPin6 = 6;  // Enter
int inputPin7 = 7;  // Reset
int inputPin8 = 8;
int a,b,c,d,e,f,g,h,i,j,k;

void setup()  {
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    pinMode(inputPin1, INPUT);
    pinMode(inputPin2, INPUT);
    pinMode(inputPin3, INPUT);
    pinMode(inputPin4, INPUT);
    pinMode(inputPin5, INPUT);
    pinMode(inputPin6, INPUT);
    pinMode(inputPin7, INPUT);
}

void loop()  {

     if (digitalRead(inputPin5) == LOW)  {        // Setting the Password
    a=0;
    b=0;
    c=0;
    d=0;
    i=0;                                       // 'i' will control whether or not to exit loop
    while (i < 1)
    {
      j=0;
      while (j < 1)                            // First passcode input
      {
        if (digitalRead(inputPin1) == LOW)  {
          a = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          a = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          a = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          a = 4;
          j = j + 1;
        }
      }
      
      j=0;
      while (j < 1)                            // Second passcode input
      {
        if (digitalRead(inputPin1) == LOW)  {
          b = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          b = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          b = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          b = 4;
          j = j + 1;
        }
      }
      
      j=0;
      while (j < 1)                            // Third passcode input
      {
        if (digitalRead(inputPin1) == LOW)  {
          c = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          c = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          c = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          c = 4;
          j = j + 1;
        }
      }
      
       j=0;
      while (j < 1)                            // Fourth passcode input
      {
        if (digitalRead(inputPin1) == LOW)  {
          d = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          d = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          d = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          d = 4;
          j = j + 1;
        }
      }
      i = i + 1;                                   // End setting the Password
    }
  }
                                              
    if (digitalRead(inputPin6) == LOW)  {          // Entering the password
      e=0;
      f=0;
      g=0;
      h=0;
      i=0;
      while (i < 1)
      {
        j=0;
        while (j < 1)                              // Entering the first Character
        {
        if (digitalRead(inputPin1) == LOW)  {
          e = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          e = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          e = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          e = 4;
          j = j + 1;
        }
      }
        
        
        j=0;
        while (j < 1)                              // Entering the second Character
        {
        if (digitalRead(inputPin1) == LOW)  {
          f = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          f = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          f = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          f = 4;
          j = j + 1;
        }
      }
      
        j=0;
        while (j < 1)                              // Entering the third Character
        {
        if (digitalRead(inputPin1) == LOW)  {
          g = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          g = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          g = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          g = 4;
          j = j + 1;
        }
      }
      
        j=0;
        while (j < 1)                              // Entering the fourth Character
        {
        if (digitalRead(inputPin1) == LOW)  {
          h = 1;
          j = j + 1;
        }
        if (digitalRead(inputPin2) == LOW)  {
          h = 2;
          j = j + 1;
        }
        if (digitalRead(inputPin3) == LOW)  {
          h = 3;
          j = j + 1;
        }
        if (digitalRead(inputPin4) == LOW)  {
          h = 4;
          j = j + 1;
        }
      }
      delay(5000);                      //verification of code
      if (a==e)  {      
      digitalWrite(ledPin1, HIGH);                                      
      }
      if (b==f)  {      
      digitalWrite(ledPin2, HIGH);                                      
      }
      if (c==g)  {      
      digitalWrite(ledPin3, HIGH);                                    
      }
      if (d==h)  {      
      digitalWrite(ledPin4, HIGH);                                    
      }
      
      
      else  {
      i = i + 1;                              //Return to loop.... maybe
      }
      }
    }
}


I used LED's to verify if the entered password is correct or not, but what ends up happening is if I get the first password character correct of the four, all the LED's light up. My suspicion is that when you push the buttons once, it will remain in that state forever. smiley-sad
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
My suspicion is that when you push the buttons once, it will remain in that state forever.
No.

Your code just screams for some functions, though. If the "set password" switch is pressed, you should call a setPassword() function that returns only when the "ok" or "cancel" switches are pressed.

If the "enter password" switch is pressed, you should call a enterPassword()  function that returns only when the "ok" or "cancel" switches are pressed.

Put the open ({) and close (}) curly braces on separate lines, and properly indent all the code in a block.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25854
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this
http://www.arduino.cc/playground/Code/Button
help?

(If you're posting code, can you please use the # (code) icon on the posting editor's toolbar)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
No.

Your code just screams for some functions, though. If the "set password" switch is pressed, you should call a setPassword() function that returns only when the "ok" or "cancel" switches are pressed.

If the "enter password" switch is pressed, you should call a enterPassword()  function that returns only when the "ok" or "cancel" switches are pressed.

Put the open ({) and close (}) curly braces on separate lines, and properly indent all the code in a block.

The equivalent of subroutines in assembly?

Quote
Does this
http://www.arduino.cc/playground/Code/Button
help?

(If you're posting code, can you please use the # (code) icon on the posting editor's toolbar)

Yes the link you posted gave me a lot of information.
Sorry I didn't know there was a code function in the reply.

Thanks for the insights btw! I debugged the sucker last night and figured out the problem. I inserted a delay (buffer) inbetween the  inputs.... The microcontroller was reading 1 button push as all the inputs.. which in turn made me think that these buttons were kept tied.


Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48554
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The equivalent of subroutines in assembly?
Yes.
Logged

Pages: [1]   Go Up
Jump to: