"Serial.println" after pushing a button

Hy guys! :slight_smile:
I have a problem:
In my project, i use a simple button. When i press it, the Arduino sends thru the serial port a string.
The problem is that i don't want so send the data over and over until the button is not pressed anymore.
How can i do that? :smiley:

This is my code so far:

void loop() {
    if (digitalRead(ButtonPin) == HIGH) {
      Serial.println("Pressed!");
     }
}

I tried with Serial.flush(); but it didn't work.

I also tried this:

int Val;
int Val_OLD;

void loop() {
    if (digitalRead(ButtonPin) == HIGH) {
      
      Val = digitalRead(ButtonPin);
      
      if (Val != Val_OLD) {
      Val = Val_OLD;
      Serial.println("Pressed!");
      }
      
     }
}

And still doesn't work! :frowning:

Ok so you just want to send it out once per press? Ok we'll you had the right idea using a check last state method, you just had the wrong setup.

boolean button;

button = digitalRead(buttonpin);

if(button == HIGH && button != last_state)
{
//Print message
}
last_state = button;

HazardsMind:
Ok we'll you had the right idea using a check last state method, you just had the wrong setup.

I see that now. One part of the problem was that i set "Val = Val_OLD" within the "if" statement. :))
I'm working late and i'm tired. :smiley:

HazardsMind:
boolean button;
button = digitalRead(buttonpin);
if(button == HIGH && button != last_state)
{
//Print message
}
last_state = button;

Your code works perfect!
Thanks a lot!

And see this for advice about debouncing: