Using Tactile Switch as PushButton

Good day. I am an Intern currently working on a tester kit for testing a switch named Slotted Optical Switch, which detects the analog input from the sensor and map it into range of 5V. I am almost at the finishing stage after adding LCD display, Buzzers, LED to interact with the tester. Now, my supervisor gave me a tactile switch which only stays on as long as it is pressed and goes off the moment its released, and want it to be used like push button. The whole testing thing should start when the button is HIGH, and nothing happens when its LOW. I have no idea how to use a tactile switch as pushbutton. I'm still beginner in Arduino and any help would be appreciated. Thanks much.

Use a state change of the tactile switch to change the toggle switch state.

Welcome to the Forum!

Check this out:

This state change tutorial may be of interest.

Thank you so much! The state change detection sketch Example in Arduino helped. Thanks again.

Hi, thanks for replying. The attached sketch only turns the led on as long as I am pressing the switch. Anyway thanks again.

If it's solved, please mark an answer as the solution or post your working code and mark that as the solution.

Thank you so much. As @er_name_not_found mentioned too, the state change helped. Thanks again for replying.

This example will toggle the state of an LED attached to pin 7 each time that the pushbutton switch attached to pin 4 is pressed. First press on, next press off, next press on, etc. The switch is wired to ground and pin 4 with the pinMode set to INPUT_PULLUP. It also toggles the state of a boolean variable, mode.

// by C Goulding aka groundFungus

const byte  buttonPin = 4;    // the pin to which the pushbutton is attached
const byte ledPin = 7;       // the pin to which the LED is attached

bool mode = false;

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(115200);
   Serial.println("Select mode with push button");
}

void loop()
{

   static bool lastButtonState = 1;     // previous state of the button
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      bool buttonState = digitalRead(buttonPin);
      // compare the new 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:
            digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the LED output
            mode = !mode;
            if (mode == true)
            {
               Serial.println("Manual mode\n");
            }
            else
            {
               Serial.println("Scan mode\n");
            }
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}