ArduinoSTM32 Blue PillLab virtual

Blue Pill: button interrupt

A button on PB12 toggles the PC13 LED through an interrupt; the LED lights on LOW.

Blue Pill: button interrupt in Argynix Lab

The code

sketch.inoC++
// STM32 Blue Pill: a button on PB12 (to GND) toggles the PC13 LED through an interrupt.
// The LED is wired to 3.3 V, so it lights when PC13 is LOW. Pins by their STM32 names.
const int BUTTON = PB12;
volatile int presses = 0;

void pressed() { presses++; }

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);  // off
  pinMode(BUTTON, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(BUTTON), pressed, FALLING);
  Serial.print("Blue Pill at ");
  Serial.print(F_CPU / 1000000);
  Serial.println(" MHz");
}

void loop() {
  static int seen = 0;
  if (presses != seen) {
    seen = presses;
    digitalToggle(LED_BUILTIN);
    Serial.print("Pressed ");
    Serial.print(seen);
    Serial.println(digitalRead(LED_BUILTIN) == LOW ? " times, LED on" : " times, LED off");
  }
  delay(5);
}