Sampling rate causes text to flash rapidly; 16x2 LCD (RGB) Shield

I am using a 16x2 RGB LCD across 2 breadboards and linked to my Adruino Uno R3 with the Vin, GND, Reset, A4 & A5 pins (power and i2c).

I am using the Adafruit kit # 716/714/772 (I know this is not the Adafruit forums but I don't believe the issue is with the display at all; hence, why I am here).

In a very basic description, here is my connections and code:

CONNECTIONS:

Vin --> Power Bus
GND --> Ground Bus
Pin 7 --> Input from tactile pushbutton switch

CODE:

const int button = 7;

int val = 0;
int old_val = 0;
int state = 0;

void setup() {
  lcd.begin(16,2);
  pinMode(button, INPUT);
}

void loop() {

val = digitalRead(button);

if((val == HIGH) && (old_val == LOW)) {
  state = 1 - state;
  delay(10);
}

old_val = val;

if(state == 0) {
function_x();
}

if(state == 1) {
function_y();
}

}

void function_x() {
  lcd.print("Output1");
}

void function_y() {
  lcd.print("Output2");
}

When I upload the code to the Arduino everything is fine. The only issue I have is that the text on the display is flashing incredibly fast and it makes it look washed out. Before you think that it's a bad/cheap/broken LCD, when I do a "lcd.print("");" in the setup() method, there is no washing out/flashing.

I feel like the issue is that the loop function is constantly sampling the value of "state" and each time it is sampled, it traverses the conditional "if" statements and follows through with the directions therein. Its really wasteful on resources and it makes the display look horrid. I thought that I could mitigate that by creating separate methods but the state needs to be read from and reading follows a sampling rate.

Any ideas as to why it is doing this and how better I can align my code to render it easier onto an LCD display?

Thank you in advance.

From the top of my head, I think you are trying to achieve something like this ?

(not tested)

const int button = 7;

int val = 0;
int state = 0;

void setup() {
  lcd.begin(16,2);
  pinMode(button, INPUT);
}

void loop() {

val = digitalRead(button);

if(state == 0 && val == HIGH) {
function_x();
state = 1;
}

if(state == 1  && val == LOW) {
function_y();
state=0;
}

}

void function_x() {
  lcd.print("Output1");
}

void function_y() {
  lcd.print("Output2");
}

Thanks for the response.

I forgot to specify that I am using the tact switch as to toggle between states and acts as a flip-flop actuator.

I only want function_x () or function_y () to be called when there is a change state.

It looks like we have the same idea but I just wanted to bounce the idea off of you.


After playing with it during my lunch hour, I finally made it work the way that I wanted. Here is the code that I came up with (with no little part in thanks to you):

 if(val == HIGH && old_val == LOW) {
    state = 1 - state;
    delay(10);
  }
  
  if(val == HIGH && state == 0) {
    function_x();
  }
  
  if(val == HIGH && state == 1) {
    function_y();
  }

The loop() function kept reading the val object and executing the code whereas I just needed a change state to occur only when I press my button. As for the call to the other functions, those worked perfectly as they were.

Thank you sincerely for the help. Cheers!