user8162: When pushing a button I want to read the value...once.
If I push(hold) the button I get a continuous print of button1Pushed
    if(button1State == LOW) { 
     Serial.println("button1Pushed");
  }

But if I try to solve it like this, nothing happens, not a single print. What am I missing?
 int button1State = digitalRead(button1);
  int lastButton1State;
  
  if(button1State != lastButton1State){
      if(button1State == LOW){
        Serial.println("button1Pushed");
               }
        }

  lastButton1State = button1State;


user38721: lastButton1State is not initialized and therefore has no strictly defined value. In your case, it appears to have a value of 0 (which is random, it's not due to implicit initialization, which is only done for global variables). The condition if(button1State != lastButton1State) becomes true only if the button is released.
You can check that by changing
if(button1State == LOW){
  Serial.println("button1Pushed");
}

to
if(button1State == HIGH){
  Serial.println("button1 released");
}

The following should work:
int last_button_state=HIGH; // initialize to HIGH, so the button does not teigger on very first iteration
void setup () {
  // whatever you want to do here
}
void loop() {
  int button_state=digitalRead(button1);
  if (button_state!=last_button_state && button_state==LOW) {
    Serial.println("button pressed");
  }
  else if (button_state!=last_button_state && button_state==HIGH) {
    Serial.println("button released");
  }
  last_button_state=button_state;
}


8162: <p>When pushing a button I want to read the value...once.
If I push(hold) the button I get a continuous print of button1Pushed</p>
<pre><code>    if(button1State == LOW) { 
     Serial.println(&quot;button1Pushed&quot;);
  }
</code></pre>
<p>But if I try to solve it like this, nothing happens, not a single print. What am I missing?</p>
<pre><code> int button1State = digitalRead(button1);
  int lastButton1State;
  
  if(button1State != lastButton1State){
      if(button1State == LOW){
        Serial.println(&quot;button1Pushed&quot;);
               }
        }

  lastButton1State = button1State;
</code></pre>
 38721: <p><code>lastButton1State</code> is not initialized and therefore has no strictly defined value. In your case, it appears to have a value of <code>0</code> (which is random, it's not due to implicit initialization, which is only done for global variables). The condition <code>if(button1State != lastButton1State)</code> becomes true only if the button is released.</p>
<p>You can check that by changing</p>
<pre><code>if(button1State == LOW){
  Serial.println(&quot;button1Pushed&quot;);
}
</code></pre>
<p>to</p>
<pre><code>if(button1State == HIGH){
  Serial.println(&quot;button1 released&quot;);
}
</code></pre>
<p>The following should work:</p>
<pre><code>int last_button_state=HIGH; // initialize to HIGH, so the button does not teigger on very first iteration
void setup () {
  // whatever you want to do here
}
void loop() {
  int button_state=digitalRead(button1);
  if (button_state!=last_button_state &amp;&amp; button_state==LOW) {
    Serial.println(&quot;button pressed&quot;);
  }
  else if (button_state!=last_button_state &amp;&amp; button_state==HIGH) {
    Serial.println(&quot;button released&quot;);
  }
  last_button_state=button_state;
}
</code></pre>
