user28942: My code is displaying voltage and I want to have a button that when pressed it displays a text and when I pressed it again it would turn off the text. I put the word "YES" just for a test
float vPow = 4.7; 
float r1 = 1000000; 
float r2 = 100000;
const int buttonPin = 8;
int buttonState = 0;
#include <LiquidCrystal.h> 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  pinMode(buttonPin, INPUT);
  Serial.begin(100);
}

void loop() { 
  float v = (analogRead(0) * vPow) / 1024.0; 
  float v2 = (v / (r2 / (r1 + r2)))/.48;
  lcd.setCursor(0,0);
  lcd.print("V:");Serial.println(v2); lcd.print(v2); lcd.print("%");delay(100);
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    lcd.setCursor(0,1); lcd.print("YES");
  } else {
    lcd.setCursor(0,1); lcd.print("");
  }
}


user28033: You can try to use a variable which is used to save the last condition. (whether it displaying or removing in the last button-press event)
bool state=false;
bool pressed = false;

void loop() { 
  float v = (analogRead(0) * vPow) / 1024.0; 
  float v2 = (v / (r2 / (r1 + r2)))/.48;
  lcd.setCursor(0,0);
  lcd.print("V:");Serial.println(v2); lcd.print(v2); lcd.print("%");delay(100);

  pressed = false;
  while (digitalRead(buttonPin)) pressed = true; //wait the button to unpressed

  if (pressed) 
    {
    state=!state;
    if (state) {lcd.setCursor(0,1); lcd.print("YES");}else
               {lcd.setCursor(0,1); lcd.print("   ");} //3 spaces. To erase 3 characters ("YES")
    }
}


user7508: You need to somehow debounce the button. Using a long delay in the loop
(your delay(100)) can do the trick, but it's probably not the best
way, as it will slow down anything that you may later add to your
program.
Better would be to write your own debounce function, or to use one of
the libraries that already implement that. For example, with
Bounce2, you would write
something like:
#include <Bounce2.h>

const int buttonPin = 8;
Bounce button;       // debounced button
bool textDisplayed;  // whether the text is displayed or not

void setup()
{
    button.attach(buttonPin, INPUT);

    // etc...
}

void loop()
{
    // Read and display the analog value, then...

    // Update the state of the debounced button.
    button.update();

    // Toggle display on rising edges of the button.
    if (button.rose()) {
        lcd.setCursor(0,1);
        if (textDisplayed) {
            lcd.print("   ");
            textDisplayed = false;
        } else {
            lcd.print("YES");
            textDisplayed = true;
        }
    }
}


28942: <p>My code is displaying voltage and I want to have a button that when pressed it displays a text and when I pressed it again it would turn off the text. I put the word "YES" just for a test</p>

<pre><code>float vPow = 4.7; 
float r1 = 1000000; 
float r2 = 100000;
const int buttonPin = 8;
int buttonState = 0;
#include &lt;LiquidCrystal.h&gt; 
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  pinMode(buttonPin, INPUT);
  Serial.begin(100);
}

void loop() { 
  float v = (analogRead(0) * vPow) / 1024.0; 
  float v2 = (v / (r2 / (r1 + r2)))/.48;
  lcd.setCursor(0,0);
  lcd.print("V:");Serial.println(v2); lcd.print(v2); lcd.print("%");delay(100);
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    lcd.setCursor(0,1); lcd.print("YES");
  } else {
    lcd.setCursor(0,1); lcd.print("");
  }
}
</code></pre>
 28033: <p>You can try to use a variable which is used to save the last condition. (whether it displaying or removing in the last button-press event)</p>

<pre><code>bool state=false;
bool pressed = false;

void loop() { 
  float v = (analogRead(0) * vPow) / 1024.0; 
  float v2 = (v / (r2 / (r1 + r2)))/.48;
  lcd.setCursor(0,0);
  lcd.print("V:");Serial.println(v2); lcd.print(v2); lcd.print("%");delay(100);

  pressed = false;
  while (digitalRead(buttonPin)) pressed = true; //wait the button to unpressed

  if (pressed) 
    {
    state=!state;
    if (state) {lcd.setCursor(0,1); lcd.print("YES");}else
               {lcd.setCursor(0,1); lcd.print("   ");} //3 spaces. To erase 3 characters ("YES")
    }
}
</code></pre>
 7508: <p>You need to somehow debounce the button. Using a long delay in the loop
(your <code>delay(100)</code>) can do the trick, but it's probably not the best
way, as it will slow down anything that you may later add to your
program.</p>

<p>Better would be to write your own debounce function, or to use one of
the libraries that already implement that. For example, with
<a href="https://github.com/thomasfredericks/Bounce2" rel="nofollow noreferrer">Bounce2</a>, you would write
something like:</p>

<pre><code>#include &lt;Bounce2.h&gt;

const int buttonPin = 8;
Bounce button;       // debounced button
bool textDisplayed;  // whether the text is displayed or not

void setup()
{
    button.attach(buttonPin, INPUT);

    // etc...
}

void loop()
{
    // Read and display the analog value, then...

    // Update the state of the debounced button.
    button.update();

    // Toggle display on rising edges of the button.
    if (button.rose()) {
        lcd.setCursor(0,1);
        if (textDisplayed) {
            lcd.print("   ");
            textDisplayed = false;
        } else {
            lcd.print("YES");
            textDisplayed = true;
        }
    }
}
</code></pre>
