How to control the switch to light a small bulb in the specific press order?

Can you get the LED to toggle on and off with each press of the switch? That is, the first time you press the switch, the LED turns on, the second time you press the switch it turns off, and so on?

Ps. I read the debounce example, but I am confused about it. Should I use debounce function to deal with this, I think that it is not about the time interval.

void setup() { pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, INPUT); }
void loop() {
int switchValue = digitalRead(2);
if (switchValue == HIGH) {
digitalWrite(LED_BUILTIN, HIGH); }
else {
digitalWrite(LED_BUILTIN, LOW); }
delay(10); }

The state change detection example is, I think, what you are looking for. In the IDE go to File, Examples, Digital. Using that you can toggle the LED each time the button becomes pressed (as opposed to is pressed).

I modified the state change detection example to show how to toggle the on-board LED with a push buttonn switch. I changed the switch wiring to wired to ground and internal pullup. See comments in code.

  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time,
  but you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON.  This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.  No debounce required for this case.

  The circuit:
   pushbutton attached to pin 2 from ground
   internal pullup resistor on pin 2 enabled
   LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.


// 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:

void loop()
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState)
    // if the state has changed, increment the counter
    if (buttonState == LOW)
      // if the current state is LOW then the button
      // went from off to on:
      digitalWrite(ledPin, !digitalRead(ledPin));  // toggle LED 
      // if the current state is LOW then the button
      // wend from on to off:
    // Delay a little bit to avoid bouncing
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

@MinAn - as suggested, look at the StateChangeDetection example

Thanks for taking the trouble to use code tags in your post, it certainly makes it easier to deal with. I would suggest laying your code out slightly differently, perhaps like this

const byte switchPin = 2;  //const because it will not change
                           //byte because the value is below 255 so don't waste memory by using an int

void setup()
{             //each curly bracket on its own line to enable code blocks to be seen more easily
  pinMode(switchPin, INPUT_PULLUP);  //use the name given to the pin.
                                     //INPUT_PULLUP keeps the input HIGH when the switch is not closed

void loop()
  if (digitalRead(switchPin) == LOW)  //use the name given to the pin
    digitalWrite(LED_BUILTIN, HIGH);
    digitalWrite(LED_BUILTIN, LOW);

To use INPUT_PULLUP you need to wire the switch to take the pin LOW when pressed but it avoids the need to use an external pullup (or pulldown) resistor to keep the pin in a known state at all times.

Note that the program reproduces what your original one did and is not a solution to your question