Reputation: 93
This program is meant to write text from the serial port to an Arduino's LCD, and on the serial monitor. However, it prints out a long string of random numbers instead. The characters need to be stored in an array, so the text can be moved around the LCD without reading the serial port again (I plan on putting this in later). What am I doing wrong?
Here is the code:
// These are the pins our LCD uses.
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup()
{
Serial.begin(9600); //Set the serial monitor.
lcd.begin(16, 2); //Set the LCD
}
char ch;
int index = 0;
char line1[17]; //17 is the max length of characters the LCD can display
char line2[17];
void loop() {
lcd.setCursor(0,0); //Set the cursor to the first line.
if( Serial.available() ) { //If the serial port is receiving something it will begin
index=0; //The index starts at 0
do {
char ch = Serial.read(); //ch is set to the input from the serial port.
if( ch == '.' ) { //There will be a period at the end of the input, to end the loop.
line1[index]= '\0'; //Null character ends loop.
index = 17; //index = 17 to end the do while.
}
else
{
line1[index] = ch; //print the character
lcd.print('line1[index]'); //write out the character.
Serial.print('line1[index]');
index ++; //1 is added to the index, and it loops again
}
} while (index != '17'); //Do while index does not = 17
}
}
Upvotes: 0
Views: 296
Reputation:
You are wrapping a lot of things in single quotes which shouldn't be:
lcd.print('line1[index]'); //write out the character.
Serial.print('line1[index]');
These should both be line1[index]
(without the quotes), since you want the result of that expression.
} while (index != '17'); //Do while index does not = 17
This should be 17
, not '17'
.
As to the ÿ
-- that's character 255 (which will also show up as character -1
), and indicates that Serial.read()
is running out of data and returning -1
. Keep in mind that the device on the other end of the serial link will not always write a whole line of data at once -- it will, in most cases, trickle in one character at a time.
Upvotes: 2