IF-Statement is looping

Hey folks,

I have a probably simple problem while programming my Arduino Uno R3..

The code is shortened of course, so its just showing the area where I got my problem, but its compiling as it is.

I got a digitalRead reading from a button. The status is printed via the serial interface. So if the button is pressed, it says "pressed". But its saying pressed the whole time, until I get my finger of the button, which is clear to me. But I want to change it. I want it to just show me the "pressed" one time, and then continue with the code. Is that somehow possible?

Thank you in advance (:

const int firstButton = 2;
int first;
  
void setup() {
  Serial.begin(9600);

  pinMode(firstButton, INPUT);
}

void loop() {
  first = digitalRead(firstButton);

  if(first) {
    Serial.println("pressed");
  } 
}

So, set "first" to false when you print

Thank you my friends. I was able to solve my problem with the state change example, which Delta_G noted at.

For people looking for a similar solution:

const int firstButton = 2;

int firstState = 0;
int firstLastState = 0;
  
void setup() {
  pinMode(firstButton, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  firstState = digitalRead(firstButton);

  if(firstState != firstLastState) {
    if(firstState == HIGH) {
    } else {
      Serial.println("First button pressed");
    }
    delay(50);
  }
  firstLastState = firstState;

}

Good for getting it working. Now improve it.

Could your int variables be byte instead ? It would save memory, not important here but it could be in a larger program, so it is a good habit to get into.

Have you got a pull down resistor on the input pin or is it floating at an uncertain voltage. Consider using
  pinMode(firstButton, INPUT_PULLUP); to activate the internal pullup resistor and change the pushbutton wiring to take the pin LOW when pressed.

What will happen if the switch contacts bounce ? If you don't know what that means then Google "contact bounce"

int firstLastState = 0;

That makes no sense.

  byte currStateOne = LOW;
  byte prevStateOne = LOW;

does.