How to ue a push button as a switch to control a section of code??

How do I code to turn off a feature in my sensor?

Right now, whenever my PIR sensor turns on, it will print something on the Serial monitor as well as turn on the LED. Though, I would like to use a push button to turn the function off or on. How do you code to use a push button as a switch for the led? To expand, I would like to run the code to turn the LED on when the PIR sensor is HIGH and by the press of the button, do the same thing without turning on the LED.

I assume that my wiring and circuits are correct.

Here is my code:

const int inputPin = 2;
const int ledPin = 6;

int pirState = LOW;
int lastpirState = 0;
int pirCounter = 0;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);

void loop() {
  pirState = digitalRead(inputPin);

  if (pirState != lastpirState) {
    if (pirState == HIGH) {
      Serial.print("number of passes: ");
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
  lastpirState = pirState;


Looks like you need a case statement. See link below

Also you can use the following link and see an example that adafruit made for a button cycler. This works for Neopixel LEDs. You can try something similar.

Looks like you need a case statement.

Perhaps you should explain to the OP why the more complex case statement is more appropriate than the simpler if-else statement here?

@Huy-LT, your new code to read a pushbutton will be very similar to your existing code to read the PIR sensor input, in that it will need to detect when the button's state changes. But instead of incrementing a counter, the new code can simply toggle a new boolean variable between true and false. Then you can use that Boolean variable in your existing code to decide whether to light the led. With buttons, there is a danger of "bouncing", but the delay(100) you already have in your code should prevent that from being a problem.