// include the library #include // ============================================= // Adjust this block of code if you have another LCD (e.g. direct connected) #include #define I2C_ADDR 0x3f #define BACKLIGHT_PIN 3 #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE); // ============================================= // it's a 16x2 LCD so... int screenWidth = 16; int screenHeight = 2; int countMovement = 0; int stringStart = 0; int stringStop = 0; int scrollCursor = 0; void setup() { Serial.begin(9600); lcd.begin(screenWidth, screenHeight); } void loop() { lcd.clear(); lcd.setCursor(0, 1); lcd.print("Scroll-row0"); scroll(0, "The quick brown fox jumps over the lazy dog",200); delay(2000); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Scroll-row1"); scroll(1, "This line scrolls normal (200ms delay)", 200 ); delay(2000); lcd.clear(); lcd.setCursor(0, 0); lcd.print("Scroll-row1"); scroll(1, "This line scrolls fast (100ms delay)", 100 ); delay(2000); } // ========================================== void scroll(int row, String line, int speed) { countMovement = 0; stringStart = 0; stringStop = 0; scrollCursor = 0; // Serial.print(countMovement); Serial.print("-"); Serial.println(line.length() + 1); while ( ( countMovement ) < (line.length() + 2 ) ) { lcd.setCursor(scrollCursor, row); // Serial.print("Cursor: "); Serial.print(scrollCursor); Serial.print(" "); Serial.println(row); lcd.print(line.substring(stringStart, stringStop)); // Serial.println( line.substring(stringStart, stringStop) ); delay(speed); if (stringStart == 0 && scrollCursor > 0) { // Serial.println(scrollCursor); scrollCursor--; stringStop++; } else if (stringStart == stringStop) { stringStart = stringStop = 0; scrollCursor = screenWidth; } else if (stringStop == line.length() && scrollCursor == 0) { stringStart++; } else { stringStart++; stringStop++; } countMovement++; } }