Led state using button

Kinda dumb question but I'd like to know if there is any other way or a better idea.

So I'm gonna use a button to light up a led and they gonna work as following :
Pressing once the button (and releasing) will turn ON the led and stay ON, if I press again the button
(and releasing it) the led will turn OFF, getting as example the button of your keyboard "Num Lock" and the Num Lock led.

I wrote a small code, theoretically should work but didn't tested it yet.
put some small comments as well to understand my logic.

int led = 13;
int button = 8;

void setup() {                

  pinMode(led, OUTPUT);  
  pinMode(button, INPUT);
}

int x = 0;
void loop() {
  
  int valofbutton = digitalRead(button); //value of the button 0 or 1 (LOW or HIGH)
  
   if(valofbutton == 1){ //if button HIGH
     x++;  // x+1;  x=1
     if(x == 1){  //if x = 1 
        digitalWrite(led, HIGH); //then led HIGH
        delay(50);  // small delay to release the button
     }
     if(x == 2){  // if we press again x=2  
        digitalWrite(led, LOW); // if x = 2 led LOW
        x =0;   // making x = 0 for the next loop 
        delay(50); // small delay to release the button
     }
   }
}

Had exactly this problem and not only, i had another problem that got plenty of noise from the resistor and rewrote the code:

int led = 13;
int button = 8;

void setup() {                
  Serial.begin(9600);
  pinMode(led, OUTPUT);  
  pinMode(button, INPUT);
}
long now = 0;
long interval = 500;
int x = 0;
void loop() {
  
  
  if(millis() - now >= interval){
  int valofbutton = digitalRead(button); //value of the button 0 or 1 (LOW or HIGH) 
   if(valofbutton == 1){ //if button HIGH
     x++;  // x+1;  x=1
     Serial.println(x);
     if(x == 1){  //if x = 1 
        digitalWrite(led, HIGH); //then led HIGH
     }
     if(x == 2){  // if we press again x=2  
        digitalWrite(led, LOW); // if x = 2 led LOW
        x =0;   // making x = 0 for the next loop
        Serial.println(x);
     }
   }
   now = millis();
  }
   
}

works better now
printing as well the values of the "x"

Now you are only reading the button once every 500ms. So if you press the button quickly the program may not respond. A better method to solve that problem is to use edge detection like in the State Change Example. You want to react to the button not anytime it IS pressed but only when it first BECOMES pressed.

int led = 13;
int button = 8;

void setup() {                
  Serial.begin(9600);
  pinMode(led, OUTPUT);  
  pinMode(button, INPUT);
}

long now = 0;
long interval = 1000;
int x = 0;
void Button1_Led1(){
   if(millis() - now >= interval){
  int valofbutton = digitalRead(button); //value of the button 0 or 1 (LOW or HIGH) 
   if(valofbutton == 1){ //if button HIGH
     x++;  // x+1;  x=1
     Serial.println(x);
     if(x == 1){  //if x = 1 
        digitalWrite(led, HIGH); //then led HIGH
     }
     if(x == 2){  // if we press again x=2  
        digitalWrite(led, LOW); // if x = 2 led LOW
        x =0;   // making x = 0 for the next loop
        Serial.println(x);
     }
   }
   now = millis();
  } 
}


void loop() {
  Button1_Led1();
}

once per sec, I have no problem pressing the button for a sec to activate the led, anyway the button will act as a start button of a function, like a car start button, you press the button and wait the car to start. I mostly added the millis because of the noise the resistor had i didn't wanted the function to get randomly activated buy the noise.