So, I decided to try to make a basic password circuit (image included), with 4 buttons and 2 LEDs: a green one that's supposed to turn on if the passcode was entered correctly and a red one that's supposed to turn on if the passcode wasn't entered correctly. You get the idea. I am pretty new to this stuff, and I decided to use the Passcode library (Arduino Playground - Password Library).
Here is the code I have so far (probably lots of mistakes, could you please tell me what I need to do?):
#include <Password.h>
const int key1 = 7;
const int key2 = 6;
const int key3 = 5;
const int key4 = 4;
int key1State;
int key2State;
int key3State;
int key4State;
const int greenLed = 3;
const int redLed = 2;
Password password = Password("1234");
void setup(){
Serial.begin(9600);
pinMode(key1, INPUT);
pinMode(key2, INPUT);
pinMode(key3, INPUT);
pinMode(key4, INPUT);
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
}
void loop(){
key1State = digitalRead(key1);
if (key1State == HIGH) {
password.append('1');
}
key2State = digitalRead(key2);
if (key2State == HIGH) {
password.append('2');
}
key3State = digitalRead(key3);
if (key3State == HIGH) {
password.append('3');
}
key4State = digitalRead(key4);
if (key4State == HIGH) {
password.append('4');
}
Serial.println(password.evaluate()?"true":"false");
password.reset();
}
I have included an image of the circuit and what I want to know is:
-Do I need the Passcode library to do this? Is there a better way?
-What have I done wrong?
-How to I get the LEDs to turn on when I want them to?
Also, Windows plays the sound for when a USB device is unplugged every time I press the buttons. Does it have anything to do with the circuit?
Thank you for your time,
TheChorizo