/* This program reads strings from the serial console, and displays them on the LCD. The message received will be scrolled unless/until the button is pressed to clear the display. At that point only the display of a new messages will reset the output. */ // include the library code: #include // initialize the library with the numbers of the interface pins LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // The clear-button #define CLEAR 7 // The two lines of text. char one[128]; char two[128]; // The input-buffer for text from the serial-console char input[128]; byte index = 0; // The scroll-offset for the first line of text. int offset = 0; void setup() { // set up the number of columns and rows on the LCD lcd.begin(16, 2); // The button we read for clearing the display. pinMode(CLEAR, INPUT); Serial.begin(9600); Serial.write("Power On"); strcpy( one, "This is the default message." ); strcpy( two, "by Steve" ); } void loop() { // If we're clearing if ( digitalRead(CLEAR) == LOW ) { // Turn off the display lcd.noDisplay(); // empty our display-buffer memset( one, '\0', sizeof(one)); return; } // If we have serial input available .. while (Serial.available() > 0) { // And we've not yet filled our input buffer. if (index < sizeof(input)) { // Read & store the next character input[index] = Serial.read(); index++; input[index] = '\0'; } } // // If we have a string with "#" as the last character we can update our message // if ( strstr( input, "#" ) > 0 ) { Serial.print( "Read from serial : '"); Serial.print( input ); Serial.print( "'\n" ); // Copy this text to the first-line. strcpy( one, input); // Replace the "#" with a null-terminator one[strlen(one) - 1] = '\0'; // Replace any non-printable characters with a space. for( int i = 0 ; i < strlen( one ); i++ ) if ( !isprint( one[i] ) ) one[i] = ' '; // Add spaces to the end, so it scrolls off the display neatly. for ( int i = 0; i < 16; i++ ) strcat( one, " " ); // // Empty our input buffer, and reset our stcroll-state. // memset( input, '\0', sizeof(input) ); index = 0; offset = 0; } // If our text is empty - then do nothing if ( strlen(one) < 1 ) return; // Otherwise restore the display lcd.display(); // Move to the top-left. lcd.setCursor(0, 0); // Draw 16 characters from the start of the first string. for ( int i = 0; i < 16; i++ ) { int c = i + offset; if ( c < strlen( one ) ) lcd.write(one[c]); } // Show the second inline. lcd.setCursor(0, 1); lcd.print( two ); // Handle scrolling the first line offset += 1; if ( (offset + 16) > strlen(one) ) offset = 0; delay(500); }