I'd like you to help me.
I have four buttons and an 8 number pin and 3 output leds. If i press the buttons in the right way, the program grants the access. If i press them in wrong way, the access denied. This is okay. but i'd like to check the pressed buttons right after pressing, not after all the 8 numbers inputted.
I have another problem, i'd like to restart the button listening after 2 seconds without button press.
Here is the code:
const int greenled = 6;
const int redled = 7;
const int ackled = 11;
const int b1 = 9;
const int b2 = 2;
const int b3 = 3;
const int b4 = 4;
int timer_ackled = 400;
int pwcount;
byte combination[] = "12314241";
long userInput[9];
int buttonstate1 = 0;
int buttonstate2 = 0;
int buttonstate3 = 0;
int buttonstate4 = 0;
void setup() {
pinMode(greenled, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(b1, INPUT);
pinMode(b2, INPUT);
pinMode(b3, INPUT);
pinMode(b4, INPUT);
Serial.begin(9600);
}
void loop(){
buttonstate1 = digitalRead(b1);
buttonstate2 = digitalRead(b2);
buttonstate3 = digitalRead(b3);
buttonstate4 = digitalRead(b4);
if (buttonstate1 == HIGH){
userInput[pwcount] = '1';
pwcount++;
delay(300);
Serial.print('1');
digitalWrite(ackled, HIGH);
delay(timer_ackled);
digitalWrite(ackled, LOW);
}
if (buttonstate2 == HIGH){
userInput[pwcount] = '2';
pwcount++;
delay(300);
Serial.print('2');
digitalWrite(ackled, HIGH);
delay(timer_ackled);
digitalWrite(ackled, LOW);
}
if (buttonstate3 == HIGH){
userInput[pwcount] = '3';
pwcount++;
delay(300);
Serial.print('3');
digitalWrite(ackled, HIGH);
delay(timer_ackled);
digitalWrite(ackled, LOW);
}
if (buttonstate4 == HIGH){
userInput[pwcount] = '4';
pwcount++;
delay(300);
Serial.print('4');
digitalWrite(ackled, HIGH);
delay(timer_ackled);
digitalWrite(ackled, LOW);
}
for(long n = 0; n <=8; n++){
if (userInput[pwcount] == combination[n] && pwcount >=8){
Serial.println(" access granted");
digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
delay(10000);
digitalWrite(greenled, LOW);
pwcount = 0;
}
else {
if(userInput[n] != combination[n] && pwcount >=8){
Serial.println(" access denied");
digitalWrite(greenled, LOW);
digitalWrite(redled, HIGH);
delay(3000);
digitalWrite(redled, LOW);
pwcount = 0;
n = 0;
}
}
}
}
layerke:
I have another problem, i'd like to restart the button listening after 2 seconds without button press.
For this part, just store the current millis() when any button is pressed, then every time through the loop see if the difference between current millis() and lastButtonPressedMillis is > 2 seconds. If so, reset your state machine.
arduinodlb:
For this part, just store the current millis() when any button is pressed, then every time through the loop see if the difference between current millis() and lastButtonPressedMillis is > 2 seconds. If so, reset your state machine.
I never used this thing and don't know how to implement it or where.. Can you please explain the method to do this?
layerke:
I have four buttons and an 8 number pin and 3 output leds. If i press the buttons in the right way, the program grants the access. If i press them in wrong way, the access denied. This is okay. but i'd like to check the pressed buttons right after pressing, not after all the 8 numbers inputted.
A cleaner way is to check every button against the current index, instead of using a for loop.
But, this will do for a first pass for now, change your loop to something like (untested):
byte valid = 1; // assume all is good. Then check the sequence
for(byte n = 0; n < pwcount; n++){
if (userInput[n] != combination[n]){
valid = 0;
break;
}
}
if (valid == 1 && pwcount == 8)
{
// do access granted
}
else if (valid == 0)
{
// bad code. Reset some stuff??
}
Thank you very much, this is the way i wanted this code. I tried to put in a counter, but if i pressed the first button it was okay, and the second button always gave me an access denied. This code is perfect, i made a little change in start over section, like this: