Hello guys. I'm using an "if" code inside "while" loop. But it doesn't work. Codes:
int test = 0;
int but1 = 13;
int but1Val;
void setup() {
Serial.begin(9600);
pinMode(but1, INPUT);
}
void loop() {
but1Val = digitalRead(but1);
while(test != 10){
if(but1Val == 1){
test++;
Serial.println(but1Val);
}
}
Serial.println("Out Of While");
}
I always in while loop. When i press the button, button value never changes. Although i press the button, serial monitor always writes 0. Where is the problem? What can i do? Thanks..
but1Val = digitalRead(but1); // This sets but1Val to equal what is read at this time
while(test != 10){
if(but1Val == 1){ // now if but1Val was set to zero this will NEVER become true and so test will NEVER count
test++;
Serial.println(but1Val);
}
}
you might consider moving 1 line of code
while(test != 10){
but1Val = digitalRead(but1); // moved this line to here
if(but1Val == 1){
test++;
Serial.println(but1Val);
}
}
while(test != 10){
if(digitalRead(but1) == 1){// combined read & test to here
test++;
Serial.println(test); // and changed this line
}
}
test = 0; // reset for next time thru
This will go 1 2 3 4 5 6 7 8 9 10 pretty quick I think