How to pause the loop without using delay

Hello, i'm try to create a "menu" for my lcd screen. I'm using a cycle like this:

int x=digitalRead(button);

IF(x==HIGH){

delay(1000); x=digitalRead(button);

while(x==LOW){

.... lcd.print("hello"); .... x=digitalRead(button); }}

but the "hello" print blink because the while cycle continue to loop and printing the string.

How can i "pause" my program (i would like to print "hello" only one time) and waiting for new input?

Thx for help. (ps.sorry for my bad english)

i would like to print "hello" only one time

Print it before the while loop.

sparta88: Hello, i'm try to create a "menu" for my lcd screen. I'm using a cycle like this:

int x=digitalRead(button);

IF(x==HIGH){

delay(1000); x=digitalRead(button);

while(x==LOW){

.... lcd.print("hello"); .... x=digitalRead(button); }}

but the "hello" print blink because the while cycle continue to loop and printing the string.

How can i "pause" my program (i would like to print "hello" only one time) and waiting for new input?

Thx for help. (ps.sorry for my bad english)

DEBOUCE: If your button "bounces" when pressed: on-off-on-off-on .... Then you need to DEBOUNCE your button.

Read this ... https://www.arduino.cc/en/Tutorial/Debounce

Instead of Toggling the LED, you want to substitute your lcd.print("Hello") command

Non-blocking Delays: For non-blocking DELAYS use the TIMERS as shown here ... http://playground.arduino.cc/Code/AvoidDelay

think of the while as a very small loop

while(x==LOW){


lcd.print(“hello”);

x=digitalRead(button);
}}

x is low so print hello then check button, x is still low so print hello then check button, x is still low so print hello then check button

most people use state change to do something once

int buttonstate=digitalRead(button);

// first line test to see if button state has changed
if (prevbuttonstate != buttonstate){
  delay(40);//poor way to add a debounce
  if (buttonstate==HIGH){//if it changed is it high
    lcd.print("hello");//do this
  }
}
prevbuttonstate = buttonstate;//make button state = prev 

//next time the loop passes the above code if the button has not changed 
//from high to low or low to high then it will not be executed

I have added some comments to your code. It may help you to understand how this snippet alone can get stuck printing. As written you are timing the button press and I am sure that was not your intend.

It seems that people code for denounce irregardless if the device needs it or not and often wrong. I would suggest you test you code without using debounce first to make sure it will do what you want it to do. You can emulate the button press / state and add real debounce later if actually needed.

You are on right track using while instead of if when the code is waiting for response and when there is nothing else for it to do.

( I am adding that to avoid flaming " do not use delay .. it is blocking code ... etc", nothing personal).

void setup() {
  // put your setup code here, to run once:
  int button = 10; // test pin number
  int x = digitalRead(button); // read I/O button 10
  if (x == HIGH) {
    delay(1000);              // debounce(?)  , way too long should be max couple hundreds of ms depending on button mechnics
    x = digitalRead(button);   // read I/O button 10 again
    // if debouce was coded properly ( as debouce )  x shoul be still HIGH
    // but  when coded this way if the button
    // is not held operated / pushed for 1 second
    while (x == LOW) {         // the read will return LOW
      Serial.print("hello");   // and "hello" will be printed 
      x = digitalRead(button); // as long as button is not pressed 
    }
  }
  // execute this when x  == LOW
}

void loop() {
  // put your main code here, to run repeatedly:
}