How To Serial Print Button State Only Once When Pressed Once?

I have two pushbuttons wired to an LED and an arduino nano. When I press button1 it prints LED IS ON not once, but as long as the button is held down, and the same for button2 where it prints LED IS OFF. Is there a way to print only one line of code in the Serial Monitor, when the button is pressed once?

Here's my code:

int ledPin = 3;
int buttonApin = 7;
int buttonBpin = 9;

void setup() {

pinMode(ledPin,OUTPUT);
pinMode(buttonApin,INPUT_PULLUP);
pinMode(buttonBpin,INPUT_PULLUP);
Serial.begin(9600);

}

void loop() {

if(digitalRead(buttonApin) == LOW)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
}
if(digitalRead(buttonBpin) == LOW)
{
digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
}

}

See the StateChangeDetection example in the IDE

You need to detect when the button becomes pressed not when it is pressed

UKHeliBob:
See the StateChangeDetection example in the IDE

You need to detect when the button becomes pressed not when it is pressed

just looked at it, it adds a delay(50);
which isn't ideal, would much prefer a way without adding a delay.

just looked at it, it adds a delay(50);
which isn’t ideal, would much prefer a way without adding a delay.

It is an example of how to detect the change of state. Feel free to remove the delay() if you want

That delay comes after you have reacted to the state change. As a human (my assumption), you won't be able to detect a 50ms delay. Mechanical buttons/switches will bounce whether you deal with it or not.