#include <LiquidCrystal_I2C.h>
const unsigned long shutoffDelay = 10000; // 30 seconds shutoff delay
const int NUM_ARRAYS = 51;
const int STRING_SIZE = 60; // max chars for 3 lines
const int BTN_PIN = 2;
const int I2C_ADDR = 0x27;
const int NUM_COLS = 20;
const int NUM_ROWS = 4;
const char afStr0[] PROGMEM = "Lorem ipsum dolor; sit amet,; adipiscing elit ...\0";
const char afStr1[] PROGMEM = "You are capable of; amazing things.\0";
const char afStr2[] PROGMEM = "You are enough; just as you; are.\0";
const char afStr3[] PROGMEM = "You make a positive; impact on others.\0";
const char afStr4[] PROGMEM = "You deserve love and; happiness.\0";
const char afStr5[] PROGMEM = "Your potential is; limitless.\0";
const char afStr6[] PROGMEM = "You are a beacon of; light in the world.\0";
const char afStr7[] PROGMEM = "You are resilient; and strong.\0";
const char afStr8[] PROGMEM = "Your journey is; important.\0";
const char afStr9[] PROGMEM = "You attract success; and abundance.\0";
const char afStr10[] PROGMEM = "You are a unique and; valuable person.\0";
const char afStr11[] PROGMEM = "Your efforts are not; in vain.\0";
const char afStr12[] PROGMEM = "You radiate; confidence.\0";
const char afStr13[] PROGMEM = "You are surrounded; by positive; energy.\0";
const char afStr14[] PROGMEM = "Your possibilities; are endless.\0";
const char afStr15[] PROGMEM = "You are a magnet; for miracles.\0";
const char afStr16[] PROGMEM = "You choose; happiness.\0";
const char afStr17[] PROGMEM = "You are a source; of inspiration.\0";
const char afStr18[] PROGMEM = "Your heart is open; to love and; kindness.\0";
const char afStr19[] PROGMEM = "You are a problem; solver.\0";
const char afStr20[] PROGMEM = "You are worthy of; success and joy.\0";
const char afStr21[] PROGMEM = "You believe in your; dreams.\0";
const char afStr22[] PROGMEM = "You are making; a difference.\0";
const char afStr23[] PROGMEM = "You are filled; with wisdom.\0";
const char afStr24[] PROGMEM = "You trust the; process of life.\0";
const char afStr25[] PROGMEM = "You let go of what; you can't control.\0";
const char afStr26[] PROGMEM = "You are loved and; appreciated.\0";
const char afStr27[] PROGMEM = "You attract; positive people.\0";
const char afStr28[] PROGMEM = "You are a; masterpiece.\0";
const char afStr29[] PROGMEM = "You are creating a; life you love.\0";
const char afStr30[] PROGMEM = "You are a gift; to the world.\0";
const char afStr31[] PROGMEM = "You are amazing!\0";
const char afStr32[] PROGMEM = "Believe in yourself!\0";
const char afStr33[] PROGMEM = "You've got this!\0";
const char afStr34[] PROGMEM = "You are capable of; great things.\0";
const char afStr35[] PROGMEM = "Your potential is; limitless.\0";
const char afStr36[] PROGMEM = "You are a positive; force in the world.\0";
const char afStr37[] PROGMEM = "You are resilient; and strong.\0";
const char afStr38[] PROGMEM = "Your efforts; are making a; difference.\0";
const char afStr39[] PROGMEM = "You radiate positive; energy.\0";
const char afStr40[] PROGMEM = "You are surrounded; by love and; support.\0";
const char afStr41[] PROGMEM = "You are a constant; learner and grow; from challenges.\0";
const char afStr42[] PROGMEM = "You attract success; with your; positive attitude.\0";
const char afStr43[] PROGMEM = "You inspire others; with your actions.\0";
const char afStr44[] PROGMEM = "You are in control; of your destiny.\0";
const char afStr45[] PROGMEM = "You are deserving of; all good things; in life.\0";
const char afStr46[] PROGMEM = "Your dreams are; achievable and; within reach.\0";
const char afStr47[] PROGMEM = "You are a beacon of; light and hope.\0";
const char afStr48[] PROGMEM = "You choose happiness; and abundance.\0";
const char afStr49[] PROGMEM = "You are worthy of; success and; happiness.\0";
const char afStr50[] PROGMEM = "You are always in; your loved ones'; thoughts.\0";
PGM_P const affirmations[NUM_ARRAYS] PROGMEM =
{
afStr0, afStr1, afStr2, afStr3, afStr4, afStr5, afStr6, afStr7, afStr8, afStr9,
afStr10, afStr11, afStr12, afStr13, afStr14, afStr15, afStr16, afStr17, afStr18, afStr19,
afStr20, afStr21, afStr22, afStr23, afStr24, afStr25, afStr26, afStr27, afStr28, afStr29,
afStr30, afStr31, afStr32, afStr33, afStr34, afStr35, afStr36, afStr37, afStr38, afStr39,
afStr40, afStr41, afStr42, afStr43, afStr44, afStr45, afStr46, afStr47, afStr48, afStr49,
afStr50
};
bool isRunning = true;
unsigned long prevShutOffTime = 0;
LiquidCrystal_I2C lcd(I2C_ADDR, NUM_COLS, NUM_ROWS);
bool checkButton() {
static int oldBtnState = HIGH;
bool isButtonPressed = false;
int btnState = digitalRead(BTN_PIN);
if (btnState != oldBtnState) {
oldBtnState = btnState;
if (btnState == LOW) {
isButtonPressed = true;
// if not running show splash
if (!isRunning) {
showSplash();
delay(1000);
}
Serial.println("Get new affirmation");
isRunning = true;
// Reset the shutoff timer when the button is pressed
prevShutOffTime = millis();
} else {
Serial.println("Button released\n");
isRunning = true;
}
delay(20); // for debounce
}
return isButtonPressed;
}
void displayRandomAffirmation() {
lcd.clear();
int lineNumber = 1;
int index = random(NUM_ARRAYS);
Serial.print("Random index: ");
Serial.println(index);
// Get random string in the affirmations array
char buffer[strlen_P(affirmations[index]) + 1]; // +1 for null
strcpy_P(buffer, (PGM_P)pgm_read_word(&affirmations[index]));
//Serial.println(buffer);
char * pch;
pch = strtok (buffer, ";-");
while (pch != NULL) {
lcd.setCursor(0, lineNumber);
lcd.print(pch);
Serial.println (pch);
lineNumber++;
if (lineNumber > 3) lineNumber = 1;
pch = strtok (NULL, ";-");
}
//delay(1000);
}
void showSplash() {
lcd.clear();
lcd.backlight();
lcd.setCursor(2, 1);
lcd.print("Here is your");
lcd.setCursor(6, 2);
lcd.print("affirmation!");
isRunning = true;
}
void shutOffDisplay() {
if (isRunning && (millis() - prevShutOffTime >= shutoffDelay)) {
prevShutOffTime = millis();
lcd.clear();
lcd.noBacklight();
isRunning = false;
Serial.println("Display and backlight shutoff");
}
}
void setup() {
Serial.begin(115200);
lcd.begin(20, 4);
pinMode(BTN_PIN, INPUT_PULLUP);
randomSeed(analogRead(0));
showSplash();
}
void loop() {
if (checkButton()) {
displayRandomAffirmation();
}
shutOffDisplay();
}