Data type for Boolean logic

I have a while loop that I can't figure out how to exit from correctly, and I think it's because of a data type conflict. When I use my lcd screen to print species, it shows the correct number based on what key I've pressed on the keypad. However, pressing - for example - 1 or 2 does not cause the while loop to exit correctly. Any insight on why would be appreciated.

char species = 0;

void loop() {

while (species == 0) { lcd.setCursor(0,0); lcd.print("Select Species "); char species = kpd.getKey(); unsigned long currentMillis = millis(); delay(1000); lcd.clear(); lcd.print(species); delay(1000); /* if (currentMillis - previousMillis >= interval & trip == 0) { lcd.setCursor(0,1); lcd.print("1 Oyster "); trip = 1; previousMillis = currentMillis; }

if (currentMillis - previousMillis >= interval & trip == 1) { lcd.setCursor(0,1); lcd.print("2 Shiitake "); trip = 0; previousMillis = currentMillis; } */ }


Hello there!

Is this all of your code? It would help to see the whole thing, and posting code using code tags would be even better.

That being said, check out your if statement conditions. The timing part seems okay, but checking whether trip is 1 or 0 seems odd. Take a look and let me know.

Did you mean: if (currentMillis - previousMillis >= interval && trip == 1) ?

You are re-defining "species" in the while(). So the code is doing exactly what you are telling it to do.


Yes, you're correct. Thanks for pointing that out


I was using the trip variable as a way of switching back and forth in the messages sent to the LCD screen. Sure, I could print the first statement, delay, second statement, delay, etc. However I wanted to keep the loop cycling fast to read for keypad input. The delays I do have in the code posted above were to try and help me in debugging.


I figured out the problem. When defining species I needed to use int instead of char, and then change

char species = kpd.getKey();


species = kpd.getKey();

When defining species I needed to use int instead of char, and then change

The second part was necessary. The getKey() method returns a char, so the first part was NOT, and was, in fact, the wrong thing to do.