#include <SD.h>
#include <LiquidCrystal_I2C.h>
#define CS_PIN 10
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Buffer for screen content, two lines 16 chars (+1 for zero termination)
char buffer[2][17];
void setup() {
if(!SD.begin(10)) while(1);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("wokwi.txt");
delay(2000);
lcd.clear();
// Fill the buffers with spaces
memset(buffer, ' ', sizeof(buffer));
// Terminate the buffers (C expects a NULL character at the end of every string, to know how long it is)
buffer[0][16] = 0;
buffer[1][16] = 0;
}
void loop() {
File text = SD.open("wokwi.txt"); // open the file for reading
if(text) {
while(text.available()) { //execute while file is available
char letter = text.read(); //read next character from file
// Replace new-line with a space
if(letter == '\r') letter = ' ';
// Only render characters that are possible to put on the screen
if(letter >= ' ' && letter < 128) {
// Move first buffer one step to the left
memmove(&buffer[0][0], &buffer[0][1], 15);
// Add the first character of the second buffer to the end of the first buffer
buffer[0][15] = buffer[1][0];
// Move the second buffer one step to the left
memmove(&buffer[1][0], &buffer[1][1], 15);
// Add the new character at the end of the second buffer
buffer[1][15] = letter;
// Print the first buffer to the first LCD line
lcd.setCursor(0, 0);
lcd.print(buffer[0]);
// Print the second buffer to the second LCD line
lcd.setCursor(0, 1);
lcd.print(buffer[1]);
// Delay
delay(300);
}
}
text.close(); //close file
}
lcd.clear();
}