Normally the button would be between a pin ( set with a pullup in setup ) and ground,
so if you press the button and digitalRead the pin it will be low,
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
Normally the LED is wired from the pin through a resistor to ground ( as is the built in LED on pin 13 ) so you would
digitalWrite(ledPin, HIGH) to switch it on
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
}
else {
// turn LED off:
digitalWrite(ledPin, HIGH);
}
even when you get the logic sorted out, what you are doing is saying " if the buttons is pressed, light the LED' else ( otherwise ) turn it off.
So you might as well wire the LED to the button ![]()
when you have the logic levels sorted search for toggle.