Advertisement
NittyGritty

LCD_Scroll.ino

Apr 9th, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. // include the library
  2. #include <LCD.h>
  3.  
  4. // =============================================
  5. // Adjust this block of code if you have another LCD (e.g. direct connected)
  6. #include <LiquidCrystal_I2C.h>
  7.  
  8. #define I2C_ADDR    0x3f
  9. #define BACKLIGHT_PIN     3
  10. #define En_pin  2
  11. #define Rw_pin  1
  12. #define Rs_pin  0
  13. #define D4_pin  4
  14. #define D5_pin  5
  15. #define D6_pin  6
  16. #define D7_pin  7
  17.  
  18. LiquidCrystal_I2C    lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE);
  19. // =============================================
  20.  
  21. // it's a 16x2 LCD so...
  22. int screenWidth = 16;
  23. int screenHeight = 2;
  24.  
  25. int countMovement = 0;
  26. int stringStart = 0;
  27. int stringStop = 0;
  28. int scrollCursor = 0;
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.   lcd.begin(screenWidth, screenHeight);
  33. }
  34.  
  35. void loop() {
  36.  
  37.   lcd.clear();
  38.   lcd.setCursor(0, 1);
  39.   lcd.print("Scroll-row0");
  40.   scroll(0, "The quick brown fox jumps over the lazy dog",200);
  41.   delay(2000);
  42.  
  43.   lcd.clear();
  44.   lcd.setCursor(0, 0);
  45.   lcd.print("Scroll-row1");
  46.   scroll(1, "This line scrolls normal (200ms delay)", 200 );
  47.   delay(2000);
  48.  
  49.  
  50.   lcd.clear();
  51.   lcd.setCursor(0, 0);
  52.   lcd.print("Scroll-row1");
  53.   scroll(1, "This line scrolls fast (100ms delay)", 100 );
  54.   delay(2000);
  55.  
  56. }
  57.  
  58. // ==========================================
  59. void scroll(int row, String line, int speed) {
  60.   countMovement = 0;
  61.   stringStart = 0;
  62.   stringStop = 0;
  63.   scrollCursor = 0;
  64.  
  65.   // Serial.print(countMovement); Serial.print("-"); Serial.println(line.length() + 1);
  66.   while ( ( countMovement ) < (line.length() + 2 )  )
  67.   {
  68.     lcd.setCursor(scrollCursor, row);
  69.     // Serial.print("Cursor: "); Serial.print(scrollCursor); Serial.print(" "); Serial.println(row);
  70.     lcd.print(line.substring(stringStart, stringStop));
  71.     // Serial.println( line.substring(stringStart, stringStop) );
  72.     delay(speed);
  73.  
  74.     if (stringStart == 0 && scrollCursor > 0) {
  75.       // Serial.println(scrollCursor);
  76.       scrollCursor--;
  77.       stringStop++;
  78.     } else if (stringStart == stringStop) {
  79.       stringStart = stringStop = 0;
  80.       scrollCursor = screenWidth;
  81.     } else if (stringStop == line.length() && scrollCursor == 0) {
  82.       stringStart++;
  83.     } else {
  84.       stringStart++;
  85.       stringStop++;
  86.     }
  87.     countMovement++;
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement