#include <LiquidCrystal.h>

const byte  rs = 12;
const byte en = 11;
const byte d4 = 5;
const byte d5 = 4;
const byte d6 = 3;
const byte d7 = 2;

const byte ledPin = 13;

unsigned long myScrollTimer;
unsigned long myBlinkimer;
byte testCnt;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const byte  ButtonPin = 10;
byte ButtonVal = 0;
byte positionCounter;

void setup() {
  Serial.begin(115200);
  Serial.println("setup-Start");
  pinMode(ButtonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  lcd.begin(16, 2);   // set up the LCD's number of columns and rows
}


void loop() {
  ButtonVal = digitalRead(ButtonPin);

  if ( TimePeriodIsOver(myBlinkimer, 500) == true) {
    digitalWrite(ledPin, !digitalRead(ledPin) );
    //Serial.print(testCnt++);
    //Serial.println(" timer");
  }

  if (positionCounter == 0) {
    if (ButtonVal == LOW) {
      positionCounter = 30;
      lcd.setCursor(0, 0);     // set the cursor to column 0, line 0
      //                    10        20        30        40        50        60        70        80
      //           012345678901234567890123456789012345678901234567890123456789012345678901234567890
      //lcd.print("Really long message to display0123456789");
      //         1234567890
      //                  10        20        30        40        50        60        70        80
      //         012345678901234567890123456789012345678901234567890123456789012345678901234567890
      lcd.print("          Really long message to display");    // scroll "n" positions (string length) to the left to move it offscreen left:
    }
  }

  if (positionCounter > 0) {
    if ( TimePeriodIsOver(myScrollTimer, 250) == true) {
      positionCounter--;
      lcd.scrollDisplayLeft();
    }
  }

  if (positionCounter == 0) {
    lcd.clear();
  }
}


// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - startOfPeriod >= TimePeriod ) {
    // more time than TimePeriod has elapsed since last time if-condition was true
    startOfPeriod = currentMillis; // a new period starts right here so set new starttime
    return true;
  }
  else return false;            // actual TimePeriod is NOT yet over
}