#define LED1_PIN PB0
#define LED2_PIN PB7
#define LED3_PIN PB14
#define BUTTON_PIN PB13

#define DEBOUNCE_DELAY_MS 50  // Debounce delay in milliseconds

volatile bool ledState = false;  // LED state (ON/OFF)
volatile unsigned long lastButtonPress = 0;  // Last button press time

void setup() {
  // Initialize the LEDs
  pinMode(LED1_PIN, OUTPUT);
  pinMode(LED2_PIN, OUTPUT);
  pinMode(LED3_PIN, OUTPUT);
  
  // Initialize the button
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Use internal pull-up resistor
}

void loop() {
  // Check the button state
  if (digitalRead(BUTTON_PIN) == LOW) {  // Button is pressed (active low)
    unsigned long currentMillis = millis();
    
    // Check for debounce
    if (currentMillis - lastButtonPress >= DEBOUNCE_DELAY_MS) {
      lastButtonPress = currentMillis;  // Update last button press time
      toggleLEDs();  // Toggle the LEDs
    }
  }
}

void toggleLEDs() {
  ledState = !ledState;  // Toggle LED state

  // Set LED states based on current state
  digitalWrite(LED1_PIN, ledState ? HIGH : LOW);
  digitalWrite(LED2_PIN, ledState ? HIGH : LOW);
  digitalWrite(LED3_PIN, ledState ? HIGH : LOW);
}
Loading
st-nucleo-c031c6