Reputation: 9
first, I am new to this website and arduino so i'm sure this is just a stupid mistake.
I'm working on making my arduino play hangman. It's incomplete, so currently all it does is receive serial input and make underscores and spaces on a 16x2 LCD screen.
my code looks like this.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String content = "";
int loopcount=0;
int loopend;
char character;
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0,1);
while(!Serial.available()){}
while(Serial.available()) {
character = Serial.read();
delay(5);
content.concat(character);
}
loopend=content.length();
while(!loopend==loopcount){
if(content.charAt(loopcount)==' '){
lcd.print(' ');
}else{
lcd.print('_');
}
loopcount++;
}
}
void loop(){
}
the problem is, the while loop for either printing ' ' or '_' only runs once. I get no error messages and the first character works fine. Thanks in advance!
Upvotes: 0
Views: 1140
Reputation: 123
First, let's start with the actual error in your code. Looking at your loop
while(!loopend==loopcount){
if (content.charAt(loopcount)==' '){
lcd.print(' ');
} else {
lcd.print('_');
}
loopcount++;
}
The problem is the order of operations of the ! operator. It might look like your the loop should end as soon as loopend is equal to loopcount. But for that to work you would have had to write it as !(loopend == loopcount)
or as loopend != loopcount
. As it is it first negates loopend, then compares it to loopcount. This makes the condition evaluate to true as soon as loopcount is greater than zero, regardless of the value of loopend. So to fix it just which to either of those alternatives (although using != is cleaner imo).
However, to avoid making such errors in the future, there are some design changes you can make. First there is no reason for your loop iteration variables (loopcount and loopend) to be scoped outside of the setup function. You can declare and initialize them within that function, and that way they won't have global scope. Second using a for loop instead of a while loop will reduce the chance of making an error, as this is exactly the scenario for loops were intended for.
So then the loop code would become
int len = content.length();
for (int i = 0; i < len; i++) {
if(content.charAt(i)==' '){
lcd.print(' ');
} else{
lcd.print('_');
}
}
Finally, you should really indent your code. I am not sure if your code is properly indented within your dev environment and it just got messed up in this question. But it makes errors harder to see if you have to work to see where code blocks begin and end.
Upvotes: 2
Reputation: 145
I don't know if this helps
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String content = "";
int loopcount=0;
int loopend;
char character;
void setup(){
Serial.begin(9600);
lcd.begin(16, 2);
lcd.setCursor(0,1);
}
void loop(){
// while(!Serial.available()){}
while(Serial.available()) {
character = Serial.read();
delay(5);
content.concat(character);
loopend=content.length();
}
while(loopcount < loopend){
if(content.charAt(loopcount)==' '){
Serial.println(' ');
}else{
Serial.print('_');
Serial.print(" ");
loopcount++;}
}
delay(5);
}
Upvotes: 0