I have made a password program on an Arduino UNO R3 that uses buttons to input a password but I want it to loop after you get it wrong and make it lock if you fail 3 times - I think
int passwordCount = 0;
might be part of the locking but I don't know how it would work - also how to restart when you get it wrong
I want it to be able to count the fails and loop the program if you fail by flashing a red light (pin 12) and looping using loop() or a more efficient way
Please help me with this, here is the code!
bool pin1 = false; // Variables!
bool pin2 = false;
bool pin3 = false;
bool pin4 = false;
bool pin5 = false;
bool done = false;
int passwordCount = 0;
void setup() {
pinMode (2,INPUT); // Buttons are INPUT pins!
pinMode (3,INPUT);
pinMode (4,INPUT);
pinMode (5,INPUT);
pinMode (6,INPUT);
pinMode (7,OUTPUT); // LEDs are OUTPUT pins!
pinMode (8,OUTPUT);
pinMode (9,OUTPUT);
pinMode (10,OUTPUT);
pinMode (11,OUTPUT);
pinMode (12,OUTPUT);
digitalWrite(7,LOW); // Turns all LEDs off
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
void loop() {
if (passwordCount == 3) {
digitalWrite (12,HIGH);
loop();
}
if (done == true) {
pin1 = false;
pin2 = false;
pin3 = false;
pin4 = false;
pin5 = false;
digitalWrite(7,LOW); // Turns all LEDs off
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
}
done = false;
if (digitalRead(2) == HIGH) { // If button 1 is pressed it will light up
pin1 = true;
}
if (digitalRead(3) == HIGH) { // If button 2 is pressed it will light up
if (pin1 == true) {
pin2 = true;
}
}
if (digitalRead(4) == HIGH) { // If button 3 is pressed it will light up
if (pin2 == true) {
pin3 = true;
}
}
if (digitalRead(5) == HIGH) { // If button 4 is pressed it will light up
if (pin3 == true) {
pin4 = true;
}
}
if (digitalRead(6) == HIGH) { // If button 5 is pressed it will light up
if (pin4 == true) {
pin5 = true;
}
}
if (pin5 == true){
digitalWrite(7,HIGH); // Turns all LEDs on
digitalWrite(8,HIGH);
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11,HIGH);
delay(1000);
done = true;
}
}
}
Please help me by telling me the concept and what I should include.
well done posting code as a codesection in the very first post.
This forum is to help people learn. Not to post ready to use code.
If you want somebody to write ready to use code you can ask in the gigs & collaboration-sub-forum but be prepared to pay for it.
Even for payed programming your description is not sufficient.
Please explain in normal words if somebody watches you using the ready the functioning password-input
what does this person see?
describe it very detailed with each single buttonpress and each thing that should happen when a single button-press is done.
describe entering the right password
describe what shall happen if the user presses the wrong button
exampel
first attempt to enter the password:
first button-press is correct, second button-press is correct third button-press is wrong
what shall happen?
Whenever you are waiting for an answer beeing posted here in the forum. You should start read this tutorial
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
The program will depend on how the buttons are connected to the microcontroller so you should also add a hand-drawn wiring-schematic.
loop is themost outer loop.
That does what its name says looping.
If you call loop() inside loop() you will make your program crash over time.
each time you call a function including loop some RAM is used for "jumping" back
And as function loop() does loop anyway each new run of loop will again call loop and use soem RAM
after a limited number of doing this all RAM is in use and then your prgram will crash or do weird things.
There are mainly two ways of using your time:
a: quick guessings with stumpbling from error to error and needing time to fix the errors still on a low knowledgde-level about programming
b: taking time to really learn programming and then writing down code with much less errors and the ability to find errors much faster
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
You can't reset RAM. You just have to avoid calling loop inside loop
If pressing the reset-button is a good solution for you - do it. (But I guess it would be a bad solution)
void loop() {
if (passwordCount == 3) {
digitalWrite (12,HIGH);
~~loop(); ~~ <===== don't do this!!!
there are millions of ways to make your functionality work without calling loop inside loop