toggle switch

please help me. I'm trying to built a simple toggle switch to turn on or off the led on pin 13.
but the led do not light up when I press the button. can anyone help?

const int button  = 2;
const int led = 13;
int preButtonState;
int buttonState;


void setup() {
  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  buttonState = digitalRead(button);
  if (buttonState != preButtonState){
    if (led == HIGH){
      digitalWrite(led, LOW);
    }
    else {
      digitalWrite(led, HIGH);
    }
  }
  preButtonState = buttonState;
}

Screen Shot 2017-10-23 at 8.31.04 PM.png

Please show how the switch (button) is wired.
I think that what is happening is that when you push the button the state change is detected and the LED turns on. Then a few microseconds later the loop() repeats and the button state is no longer different than the previous button state (because the button is still held down) so the LED turns off. So the LED turns on, but only for a few microseconds.
To test my theory, insert a delay(1000) below the preButtonState = buttonState; line and see what happens.

Take a closer look at the state change detection example to see how to fix your sketch.

if (led == HIGH){
      digitalWrite(led, LOW);
    }

You forgot a digitalRead. led has the value 13 and HIGH is defined as 1. So it will nver be true.

groundFungus:
Please show how the switch (button) is wired.

To test my theory, insert a delay(1000) below the preButtonState = buttonState; line and see what happens.

Take a closer look at the state change detection example to see how to fix your sketch.

I've attached my wiring in my first post.
I've also tried to add a delay as you mention, now the led can light up when the button is press once.
However, the led will not turn off if I press the switch another time.

Your wiring is correct, but there is a better way to wire momentary switches. Wire one side of the switch directly to ground and the other side of the switch to a digital input. Take advantage of the internal pullup resistor by enabling the internal pullup with pinMode(pin, INPUT_PULLUP). The switch will read HIGH when not pushed and LOW when pushed. An advantage of that wiring is that you can easily debounce the switch with a small cap across the switch (hardware debounce).

Here is the state change detection example modified to use an active low switch (LOW when pushed) to toggle the led on pin 13.

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup()
{
  // initialize the button pin as a input with internal pullup enabled
  pinMode(buttonPin, INPUT_PULLUP);  
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop()
{
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
  {
    if (buttonState == LOW)
    {
      // if the current state is LOW then the button
      // went from off to on:
      Serial.println("on");
      digitalWrite(ledPin, !digitalRead(ledPin));  // toggle the led state
    }
    else
    {
      // if the current state is LOW then the button
      // went from on to off:
      Serial.println("off");
    }   
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;
}

And a schematic of the switch wiring: