So i am having problems with the "times" variable, it is not working how it is supposed to, here is my code:
"String num;
int times;
int state;
int attempts;
void setup(){
Serial.begin(9600);
digitalWrite(13, LOW);
int state = 0;
int times = 0;
num = "0";
int attempts = 0;
Serial.println("enter your password");
}
void loop(){
Serial.println(times);
int in = analogRead(A0);
if(in > 3){
if(in < 10){
num = num + "1";
int times = times + 1;
}
}
if(in > 510){
if(in < 514){
num = num + "2";
int times = times + 1;
}
}
if(in > 999){
if(in < 1004){
num = num + "3";
int times = times + 1;
}
}
if(in > 1020){
if(in < 1024){
num = num + "4";
int times = times + 1;
}
}
while(analogRead(A0) > 0){
delay(10);
}
while(times >= 4){
if(num.charAt(1) == 1){
if(num.charAt(2) == 2){
if(num.charAt(3) == 3){
if(num.charAt(4) == 4){
num = "0";
int state = 1;
int times = 0;
Serial.print("unlocked");
}
else{
Serial.println("incorrect password");
int attempts = attempts + 1;
num = "0";
int times = 0;
}
}
else{
Serial.println("incorrect password");
int attempts = attempts + 1;
num = "0";
int times = 0;
}
}
else{
Serial.println("incorrect password");
int attempts = attempts + 1;
num = "0";
int times = 0;
}
}
else{
Serial.println("incorrect password");
int attempts = attempts + 1;
num = "0";
int times = 0;
}
}
delay(10);
}"
I just put the brackets in the code to signify that it is the end of my code, there not actually there. In the code it runs everything but when i am sending the integer "times" through serial it always sends a 0 when it is supposed to send an increase in number by one every time the input pulses. Please notify me of any errors in my code or bugs that relate to this, that would be helpful.
Since time was declared global, every time you access it with "int time" you are creating a new variable. Drop all the extra "int" declarations.
Instead of
int time = 0; // except the first time in the global section
use
time = 0;
Same applies to attempts, too.
Same applies to state.
I just put the brackets in the code to signify that it is the end of my code, there not actually there.
If you place your code inside code tags, you will not need to put sxtra characters in, and it will be a lot wasier for everyone to read.
In your Arduino IDE:
Select Tools->Auto Format
Do not proceed until Auto Format works without errors.
Click in the editor window
Press CTRL+A to select all text in the editor window
Press CTRL+C to copy the text
In the forum Editor:
Click on the # icon above the window
Press CTRL-V to paste the text.