The problem is that once I print() none of my other functions will work. Instead they print their "control characters" to the LCD display. It's as if I'm "stuck" in "string" mode. To be thorough, I also tried looping through a null terminated string and sending the BYTES individually, (and replacing sizeof()) but no matter what I do, I'm stuck in text mode after writing to the display. Any suggestions?
I wanted to be thorough, and troubleshoot this at the lowest possible level, so I used this code snippet:
void setup() {
beginSerial(9600);
// this part works
Serial.print('P', BYTE); // move cursor
Serial.print(20); // x=20
Serial.print(20); // y=20
// this part works
Serial.print('S', BYTE); // begin string
Serial.print(4); // length of string
Serial.print('T', BYTE);
Serial.print('E', BYTE);
Serial.print('S', BYTE);
Serial.print('T', BYTE);
// this part is displayed onscreen instead of moving the cursor
Serial.print('P', BYTE); // move cursor
Serial.print(40); // x=40
Serial.print(40); // y=40
// this part works
Serial.print('S', BYTE); // begin string
Serial.print(4); // length of string
Serial.print('T', BYTE);
Serial.print('E', BYTE);
Serial.print('S', BYTE);
Serial.print('T', BYTE);
}
The end result looks something like, " TESTP4040S4TEST" on the LCD display. As you can see, the first "move cursor" operation worked fine, but once I issue any text commands, the LCD never recovers.
etc. Sending BYTE is actually worse because it won't even send the entire string, ie.
Serial.print('S', BYTE);
Serial.print(7, BYTE);
Serial.print("Testing");
That looks correct, it should be working. Are there any other commands before the S command? Strip your program down to nothing, just the S command. Try substituting other values for 7 (even though 7 is the correct value). Are you monitoring serial communications in the Arduino IDE? Maybe you could get it written to a file and do a hex dump? I've barely used the Serial object before, but I'm pretty certain you're using it correctly in the last bit of code you posted.
[EDIT] I was totally unaware that we had access to the entire standard C library from arduino. That is awesome. The code now works perfectly. I added a strcat to append a null terminator to printed text. This is to prevent buffer overflows.
I got it working. When I was passing char[] to the print() function it was being passed as a pointer, so sizeof() returned 1, apparently causing all sorts of chaos. I tried to find an elegant solution, but a friend of mine who is comfortable with C suggested that I use null-terminated strings, or pass the length as a parameter, so I created an overloaded function like so:
String literals are nul-terminated anyway. In fact, strcat(str,"\0") should be a no-op. You'd be adding a nul byte to a memory location that already contains a nul byte, and you're calling strcat with a zero-length string which does nothing. C string functions have no way to differentiate an empty string literal "" from a string literal with a nul as its first byte "\0more string here".
Your code now is exactly the same as your code before. You shouldn't even need "Junk\0", since the string literal "Junk" already has a nul byte at the end.
Actually, it doesn't. The strcat function doesn't know how long the string is, it'll seek from the beginning of the string until it finds a nul byte. And replacing a nul byte with a nul byte, well.. doesn't do much, does it? It's best to either use string literals (which are nul-terminated) or nul terminate your strings as they are input.