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();
}
}
}
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.