OK, how about now, the password is now button 1,2 and 3 (pins 2,3,4)
// Project Awesome. Code Entry With Four Buttons
#define BTNCNT 9
#define ATTCNT 3
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {2,3,4}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[ATTCNT]; // Make states array
byte oldstate[ATTCNT]; // When button is hit, info added here
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT); // Initiate all pins
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt[y]
y++; // add 1 to y
time = millis(); // set time to millis()
}
}
}
if(attempt[0] == pass[0] && attempt[1] == pass[1] && attempt[2] == pass[2]){ // attempt equals pass run: Correct()
Correct();
}
if(attempt[0] != pass[0] && attempt[1] != pass[1] && attempt[2] != pass[2]){ // if attempt doesnt equal pass run timeout
TimeOut();
}
if ((millis() - time) > 5000 || y > 3 ){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
}
y=0;
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}
I feel like an idiot, i didnt see what you were talking about until now, i dint realise what you meant but i get it now, state should be [BTNCNT] not [ATTCNT]
Ok. it works, more or less, the only issue is that if you leave it too long it stops working, like over about 40 seconds. Also is there an easier way with having to write each == for the pass[x] and attempt[x]?
#define BTNCNT 9
#define ATTCNT 2
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {2,10}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[BTNCNT]; // Make states array
byte oldstate[BTNCNT]; // When button is hit, info added here
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT); // Initiate all pins
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt[y]
y++; // add 1 to y
time = millis(); // set time to millis()
}
}
}
if(attempt[0] == pass[0] && attempt[1] == pass[1]){ // attempt equals pass run: Correct()
Correct();
}
if(attempt[0] != pass[0] && attempt[1] != pass[1]){ // if attempt doesnt equal pass run timeout
TimeOut();
}
if ((millis() - time) > 5000 || y > 3 ){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
}
y=0;
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}
Should probably be just an else instead of an if. If you really want to go for an if statement you should use || instead of && here. Actually I would not use an if statement at all. Instead I would pass the state directly to a suitable function.
if (y >= ATTCNT) // they pressed the right number of buttons
{
boolean OK = true; // so far so good
// check each button
for (int i = 0; i < ATTCNT; i++)
if (attempt [i] != pass [i])
OK = false; // uh oh
if (OK)
Correct ();
else
TimeOut ();
}
And give y a better name. Do you like working with meaningless names? How about buttonPressCount?
ok, now look at it. i still have the issue that if its left too long the red light wont got out
#define BTNCNT 9
#define ATTCNT 3
int a = 0; //set a
int y = 0; // Set y
int x = 0; // Set x
int ledOne = 13; //Set Red LED
int ledTwo = 12; //Set Green LED
byte buttons[BTNCNT] = {2,3,4,5,6,7,8,9,10}; //Set buttons to array
byte pass[ATTCNT] = {2,10,3}; // Set password
byte attempt[ATTCNT]; // Set attempts array
byte state[BTNCNT]; // Make states array
byte oldstate[BTNCNT]; // When button is hit, info added here
int time = 0;
void setup(){
pinMode(ledOne, OUTPUT); // Initiate all pins
pinMode(ledTwo, OUTPUT);
for(x=0; x<BTNCNT; x++){
pinMode(buttons[x], INPUT);
}
digitalWrite(ledOne, HIGH);
}
void loop(){
for(x=0; x<BTNCNT; x++){
state[x] = digitalRead(buttons[x]); // Check states of each button
}
for(x=0; x<BTNCNT; x++){
if(oldstate[x] != state[x]){ //If these new states are different then the old ones
if(state[x] == HIGH)
{
attempt[y] = buttons[x]; //Set attempt[y]
y++; // add 1 to y
time = millis(); // set time to millis()
}
}
}
if(attempt[0] == pass[0] && attempt[1] == pass[1] && attempt[2] == pass[2]){ // attempt equals pass run: Correct()
Correct();
}
if(attempt[0] != pass[0] || attempt[1] != pass[1] || attempt[2] != pass[2]){ // if attempt doesnt equal pass run timeout
TimeOut();
}
if ((millis() - time) > 5000 || y > 3 ){
for(x=0;x<ATTCNT;x++){
attempt[x] = 0;
}
y=0;
}
for(x=0;x<BTNCNT;x++){
oldstate[x] = state[x];
}
}
void Correct(){
digitalWrite(ledOne, LOW);
digitalWrite(ledTwo, HIGH);
}
void TimeOut(){
digitalWrite(ledTwo, LOW);
digitalWrite(ledOne, HIGH);
}
With regard to the easier way: there is no need at all to store the previously entered keys. It is completely sufficient to store how many keys are already entered correctly. This in turn simplifies the logic a lot. Anyway I still recommend to clean up the code.
Anyway, im still new to arduino so i have much to learn. I will at some point refine it and simplify it, but for now i just want to know why if its left for more then about a minute the red light won't turn off and the green just flashes very very slightly...
Put some debugging displays in. That's what everyone does if they are puzzled. Do Serial.println ("at point A") or whatever, in various places, and then work out what is happening.
This code should be sanitized first. Also it should be broken into reasonable modules. Then it would become immediately obvious that for example debouncing is completely missing. But that's what I say since the start of this discussion.