Crystal ball LCD message toggle 2nd line

Greetings,
I have a 16x2 LED 8Ball that works fine but found some who wanted to use it need help understanding its use. I included an IR proximity switch with works fine when the user swipes their hand over it and then the displays the random answer.

What I am attempting to do is toggle a timed (~1sec) post of two messages on the second line. I attempted to get this to scroll but found the "ghosting" of the previous characters to be annoying even when running slowly.

My acute sense to the obvious seems lacking on how to get this to work. Any help or direction is appreciated. The code is induced below.

///Karnak Crystal ball  answers you questions.
// Several sketches looked over to come up with this one.
// apologies to origional authors.
//
///
 #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int buttonPin = 6;
int lastButtonState;

#define NUM_ANSWERS 14
const char* answerTable[NUM_ANSWERS] = {
  "Yes",
  "No",
  "Not Today",
  "Never",
  "Ask me tomorrow",
  "No One Knows",
  "Absolutely",
  "Possible",
  "Not a chance",
  "Certainly",
  "Hell No",
  "When Pigs Fly",
  "U already know",
  "Really?",

};

void setup() {
  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print("Ask Karnak");
  lcd.setCursor(0, 1);
  lcd.print("Ask a Question"); ///First second line message to be toggled 
  delay (2000);
  lcd.setCursor(0, 1);
  lcd.print("Swipe your hand");  //Second second line message to be toggled

  delay(1000);
  //lcd.clear();

  //pinMode( buttonPin, INPUT_PULLUP);

  // Try to get a random start by using the noise of the analog input(s)
  long seed = 12345678L + analogRead(0) + analogRead(1) + analogRead(2);
  randomSeed(seed);

  lastButtonState = digitalRead(buttonPin);
}

void loop() {
  int read = digitalRead(buttonPin);

  if (read != lastButtonState) {
    lastButtonState = read;

    if (read == LOW)  // low when button is pressed
    {

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Karnak says:");
      lcd.setCursor(1, 1);
      //lcd.setCursor( 0,1);

      int randomIndex = random(0, NUM_ANSWERS);
      lcd.print(answerTable[randomIndex]);

      delay(4000);
      lcd.clear();
    }
  }
}

You need to define buttoninput i setup.

Maybe call something like this completely untested snippet:

void lcdToggleLine2(char * str1, char * str2){ 
// toggle lines on the LCD
  static unsigned long lastLcdMillis = 0; // remember last toggle time
  unsigned long currentMillis = millis(); 
  static int firstValFlag = 0; // remember which line we're showing
  if (currentMillis - lastLcdMillis> 1000) // see if it's time to swap
  {
    lastLcdMillis = currentMillis;  // record time of swap
    lcd.setCursor(0, 1);
    if(firstValFlag == true){
      lcd.print(str1);
    } else {
      lcd.print(str1);
    }
    lcd.print("                "); // clear remainder of line
    firstValFlag = !firstValFlag;  // swap for next time
  }
}

If you want more control on when the swaps happen, move the declarations for lastLcdMillis and firstValFlag out to be globals, and control them.

You must overwrite all 16 characters in a line to erase previous characters.

Ahhhh... Ill try that. Meantime I figured out a solution for this one. See updated sketch below.

Ok I figured this out and did a few improvements. See new sketch attached.
Thanks for the replies!

///Karnak Crystal ball  answers you questions.
// Several sketches looked over to come up with this one.
// apologies to origional authors.
//
///
 #include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

const int buttonPin = 6;
int lastButtonState;

#define NUM_ANSWERS 15
const char* answerTable[NUM_ANSWERS] = {
  "Yes",
  "No",
  "Not Today",
  "Never",
  "Ask me tomorrow",
  "No One Knows",
  "Absolutely",
  "Possible",
  "Not a chance",
  "Certainly",
  "Hell No",
  "When Pigs Fly",
  "U already know",
  "Really?",
  "UR minds foggy"
};

void setup() {
  lcd.begin();
  lcd.setCursor(0, 0);
  lcd.print("Karnak Knows All");
  ////////////////////////////////////////////////////////
  for (int i=0; i<3; i++) ///Repeat Count
  {
  ///////////////////////////////////////////////////
  lcd.setCursor(0, 1);
  lcd.print(" Ask a Question"); ///First line message to be toggled 
  delay (3000);
  lcd.setCursor(0, 1);
  lcd.print(" Swipe Over Box");  //Second  line message to be toggled
  delay(3000);
  
  }
   lcd.setCursor (0,1);
  lcd.print(" Ask Now & Swipe"); 
  

  //pinMode( buttonPin, INPUT_PULLUP);

  // Try to get a random start by using the noise of the analog input(s)
  long seed = 12345678L + analogRead(0) + analogRead(1) + analogRead(2);
  randomSeed(seed);

  lastButtonState = digitalRead(buttonPin);
}

void loop() {
  int read = digitalRead(buttonPin);

  if (read != lastButtonState) {
    lastButtonState = read;

    if (read == LOW)  // low when button is pressed
    {

      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Karnak Says:");
      lcd.setCursor(1, 1);
     
      int randomIndex = random(0, NUM_ANSWERS);
      lcd.print(answerTable[randomIndex]);

      delay(4000);
      lcd.setCursor (0,1);
  lcd.print("New Question?  ");
  delay (2000);
  lcd.setCursor (0,1);
  lcd.print(" Ask and Swipe ");
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.