Controll LED with a push button. Problems with Code and maybe with arduino!!!

I just received my new Arduino nano, and started to learn by doing, I do some blink led, potentiometer simple projects, and so on. now I want to do another simple project: turn on and off led by a simple button.

so I take this tutorial:
1.https://www.arduino.cc/en/Tutorial/DigitalReadSerial
2.https://www.arduino.cc/en/tutorial/blink
3.https://www.arduino.cc/en/Reference/Else

and make this code:

int pushButton = 7;
int LED = 9;
int val = 0;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  delay(1);

  val = digitalRead(pushButton); 
  if (val == HIGH) {     
    digitalWrite(LED, LOW);  
  } else {
    digitalWrite(LED, HIGH); 
  }
}

u can see in the code, that the Arduino send noted by serial port to the computer, and I can see when I push the button, I get the notes from the serial monitor, but the led don't turn on/off.

thanks for the help, duck12raz.

this is how everything looks like:

The LED looks backwards. The flat on the side of the LED is the ground side.

Why are you reading the state of the input twice ?

You have two variables for the button State, which is absolutely unnecessary. Also it would be enough to define them as booleans, but this is just finetuning...

does the LED light up if you turn it on like so:

void setup(){
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
}

if the LED does turn on we know it is in your code...

thank a lot, you helped me so much.
i change the led sides(my electrical knowledge is not so good as you see)
and change the code(so it doesn't read the input 2 times)
and now it's working but i have another problem...
when i close the serial plotter, the program stop to work and led not working with me.

this is the code:

int pushButton = 7;
int LED = 9;

void setup() {
Serial.begin(9600);
pinMode(pushButton, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
int buttonState = digitalRead(pushButton);
Serial.println(buttonState);
delay(1);

if (buttonState == LOW) {
digitalWrite(LED, LOW);
} else {
digitalWrite(LED, HIGH);
}
}

If this is all your code, there is no reason for the Arduino to do so...
What happens if you use serial monitor instead?

If I find some time I will try your code on my Arduino...

What board setting are you using in the IDE?

.. you should consider to debounce your button; otherwise you will get "surprising" flickering.

As serial.print is pretty slow - after removing this Serial.println your LED is reacting without a time buffer to the button reads and will present unpreductable results unless you care about debouncing the button.

Try this code maybe -

int pushButton = 7;
int LED = 9;
int val = 0;

void setup() {

  Serial.begin(9600);

  pinMode(pushButton, INPUT);
  pinMode(LED, OUTPUT);
  
}

void loop() {

  val = digitalRead(pushButton);
  Serial.println(val);

  if (val == HIGH) {

    digitalWrite(LED, HIGH);
    
  }

  else {

    digitalWrite(LED, LOW);
    
  }
  
}

If this doesn't work, try this: Connect the button to ground, and on the other side, connect it to the Arduino, and use this code:

int pushButton = 7;
int LED = 9;
int val = 0;

void setup() {

  Serial.begin(9600);

  pinMode(pushButton, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  
}

void loop() {

  val = digitalRead(pushButton);
  Serial.println(val);

  if (val == LOW) {

    digitalWrite(LED, HIGH);
    
  }

  else {

    digitalWrite(LED, LOW);
    
  }
  
}

As rpt007 mentioned, your button might be 'floating.'