my code below has three nested if statements.
but the if statement is only met when both state1 and state2 are equal to 4.
even when condition for printing "a","b" or "c" is met it does not print.
it is only printing "close" and "d" when their conditions are met.
please help.
const int sensor1=A0;
int count=0;
int check=0;
int state1=0;
int state2=0;
int anaval=0;
void setup() {
pinMode(sensor1,INPUT);
Serial.begin (9600);
}
void loop () {
calibrate ();
if (state1==state2){
if (count==1){
if (state1==1){
Serial.println("A");
count=0;
}
else if (state1==2){
Serial.println("B");
count=0;
}
else if (state1==3){
Serial.println("C");
count=0;
}
else if (state1==4){
Serial.println("D");
count=0;
}
}
else if (count==0){
if (state1==4){
Serial.println("close");
count=1;
}
}
}
}
void calibrate () {
anaval=analogRead(sensor1);
while (millis() <=500){
check= analogRead(sensor1);
}
if (anaval>=0 && anaval<=225){
state1=4;
}
else if (anaval>=226 && anaval<=510){
state1=1;
}
else if (anaval>=511 && anaval<=755){
state1=2;
}
else if (anaval>=756 && anaval<=1023){
state1=3;
}
//check
if (check>=0 && check<=225){
state2=4;
}
else if (check>=226 && check<=510){
state2=1;
}
else if (check>=511 && check<=755){
state2=2;
}
else if (check>=756 && check<=1023){
state2=3;
}
}