turn on/off with one button

Hey i need help please
I would like to have one button to turn on and off a led or somthing else. So a button that changes position every time you press it

const int buttonPin1 = 2;

const int Relay1 = 8;

void setup() {

pinMode(buttonPin1, INPUT);

pinMode(Relay1, OUTPUT);
}
void loop() {

if (Relay1 == LOW){
if (buttonPin1 == HIGH){
delay(500);
digitalWrite(Relay1, HIGH);

}}else{
if (Relay1 == HIGH){
if (buttonPin1 == HIGH){
delay(500);
digitalWrite(Relay1, LOW);

}
}}}

Test_1.ino (402 Bytes)

You're missing digitalRead on each of the input pins.

This example does exactly what you want. https://www.arduino.cc/en/Tutorial/Debounce
It is even comes with the installation of the IDE, no need to type it or copy it from the web.