// Constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin
 
// Variables will change:
int buttonState = 0;      // variable for reading the pushbutton status
int lastButtonState = 0;  // variable for storing the last pushbutton status
int ledState = LOW;       // variable for storing the LED state
int mode = 3;             // variable to select the mode (1: momentary, 2: toggle, 3: active low)
 
void setup() {
  // Initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // Initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  
  // Set initial LED state:
  digitalWrite(ledPin, ledState);
}
 
void loop() {
  // Read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
 
  // Use a switch-case structure to handle different modes:
  switch (mode) {
    case 1:  // Momentary button function
      if (buttonState == HIGH) {
        // Turn LED on:
        digitalWrite(ledPin, HIGH);
      } else {
        // Turn LED off:
        digitalWrite(ledPin, LOW);
      }
      break;
      
    case 2:  // Toggle button function
      if (buttonState == HIGH && lastButtonState == LOW) {
        // If the button was just pressed (transition from LOW to HIGH)
        ledState = !ledState; // Toggle the LED state
        digitalWrite(ledPin, ledState);
      }
      break;
 
    case 3:  // Momentary Active Low button function
      if (buttonState == HIGH) {
        // Turn LED off:
        digitalWrite(ledPin, LOW);
      } else {
        // Turn LED on:
        digitalWrite(ledPin, HIGH);
      }
      break;
      
 
    default:
      // Default case if no mode matches
      digitalWrite(ledPin, LOW);
      break;
  }
  
  // Save the current button state as the last button state
  lastButtonState = buttonState;
  
  // Add a small delay to debounce the button
  delay(50);
}