LCD Help Coding

Hello, I have the general LCD code done. It detects light using a photoresistor going into A0 then the LCD displays the text, that all works right. What I'm currently trying to do is add a button that can change the text displayed on the lcd when pressed once, then when pressed again it changes back to the original text. Can anyone help? This is the code I got so far:
piggers7727

#include <LiquidCrystal.h>

// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int photoresistorPin = A0;
const int buttonPin = 7;
bool lightDetected = false; // Flag to track if light is detected
bool buttonPressed = true; // Flag to track if light is detected

String currentText = "text1";

void setup() {
  // Set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  pinMode(buttonPin, INPUT_PULLUP);
  // Don't print anything initially
}
void loop() {
  int sensorValue = analogRead(photoresistorPin);
  int buttonState = digitalRead(buttonPin);

  if (buttonState == LOW && !buttonPressed) {
    buttonPressed = true; // Set the flag to true
    currentText = "text2";
  } else if (buttonState == HIGH) {
    buttonPressed = false; // Reset the flag when button is released
  }

  // Check if the sensor reading indicates light
  if (sensorValue > 500) {  // Adjust this threshold based on your environment
    if (!lightDetected) { // If light was not previously detected
      lightDetected = true; // Set the flag to true if light is detected
      lcd.clear(); // Clear the LCD

      // Print the message at the beginning of the first row
      lcd.setCursor(0, 0);
      lcd.print(currentText);

      // Delay to allow time for the message to be read
      delay(2000);

      // Loop to scroll the message
      for (int i = 0; i < 10000 && lightDetected; i++) {
        lcd.scrollDisplayLeft(); // Scroll the display to the left
        delay(1000); // Adjust scrolling speed

        // Check light level again during scrolling
        sensorValue = analogRead(photoresistorPin);
        if (sensorValue <= 500) { // If it's dark
          lightDetected = false; // Set the flag to false to exit the loop
          lcd.clear(); // Clear the LCD
          break; // Exit the loop
        }
      }
    }
  } else {
    if (lightDetected) { // If light was previously detected
      lightDetected = false; // Reset the flag
      lcd.clear(); // Clear the LCD
    }
  }

  delay(500); // Adjust delay as needed
}

Do you have an external resistor on pin 7 so it is fixed at either ground or +5 volts until the button switch is pressed?

Yes a 10k one.

got to lose all the use of the delay function..
no need for external pull up resistor as you are using INPUT_PULLUP for the button pin..
buttons do bounce though..

~q

Yeah, but those are all for the moving text display

Yes, I see that..
You should be using the loop instead of delay loops inside the loop..
A state machine would be a nice design choice for this..
~q

Then how often does your code check for a button press?

It only checks as it runs through the loop

Im sorry I'm pretty new to the C language.

Add up the delays and see how often that is.

I understand what you're saying now. It is ever.y 3.5 seconds

Well, if light is detected and you start a scrolling, looks like 10,000 seconds too me..

Does the button text only display while the button is down and when released goes back to original text??

If I'm wrong please describe in detail..

~q

I am yet to implement the part that changes it back to the original text, but yes I'm trying to make it so when the button is down it toggles to the other text. So if the button is pressed and let go of once it will switch then when pressed and let go of again it will switch back to the original.

ok, I'm gonna work it over a bit..
give me a few..

~q

Thank you alot.

here try this..
untested sorry..

/*
  https://forum.arduino.cc/t/lcd-help-coding/1277759

*/


#include <LiquidCrystal.h>

// Initialize the LCD library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int photoresistorPin = A0;
const int buttonPin = 7;
bool lightDetected = false; // Flag to track if light is detected

byte buttonPressed = 1; // Flag to track if last button state

String currentText = "text1";
String buttonText = "text2";

byte stateSys = 0;
unsigned long lastMove = 0;
unsigned long intervalMove = 2000;

unsigned long lastPress = 0;
unsigned long debounceDelay = 50;

void setup() {
  // Set up the LCD's number of columns and rows
  lcd.begin(16, 2);
  pinMode(buttonPin, INPUT_PULLUP);
  // Don't print anything initially
}
void loop() {

  unsigned long now = millis();
  int sensorValue = analogRead(photoresistorPin);

  if ( now - lastPress >= debounceDelay) {
    byte buttonState = digitalRead(buttonPin);
    if (buttonState != buttonPressed) {
      //button change, start debouncing
      lastPress = now;
      buttonPressed = buttonState; // remember
      if (buttonState == LOW) {
        if (stateSys < 99) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print(buttonText);
          stateSys = 99;
        } else {
          lcd.clear();
          stateSys = 0;
        }
      }
    }
  }

  //skip this if button was press
  //have to press button again first
  if (stateSys < 99) {
    // Adjust this threshold based on your environment
    if (sensorValue > 500) {
      lightDetected = true;
      //only if less than 1, could be scrolling state 2
      if (stateSys < 1) {
        stateSys = 1;
      }
    } else {
      lightDetected = false;
      stateSys = 0;
    }
  }


  switch (stateSys) {
    case 0: lcd.clear(); //no light just Clear the LCD
      break;
    case 1: lcd.clear(); // Clear the LCD
      // Print the message at the beginning of the first row
      lcd.setCursor(0, 0);
      lcd.print(currentText);
      //setup 2 second delay
      lastMove = now;
      intervalMove = 2000;//delay for static text
      stateSys = 2;
      break; //light first detect
    case 2: if (now - lastMove >= intervalMove) {
        lastMove = now;
        intervalMove = 1000;// scrolling delay..
        lcd.scrollDisplayLeft(); // Scroll the display to the left
      }
      break;
  }
}

the switch case is the state machine..
stateSys is the state machine var..

let me know how it goes..

~q

That worked really well, besides one thing, when their isnt light for text2 it doesnt disappear, and one more thing though is there a way to make it so it can scroll?

lol, so really, you just want the test to be different but whether it's displayed or not depends on the light??
~q

Yeah lol