title: How to detect button press on stm32f4discover?
body: <p>I like to detect a button press to switch through the 4 LEDs on the board, like</p>
  
  <pre><code>Click-1 --&gt; LED 1 on
  Click-2 --&gt; LED 2 on
  Click-3 --&gt; LED 3 on
  Click-4 --&gt; LED 4 on
  Click-5 --&gt; LED 1 off
  Click-6 --&gt; LED 2 off
  Click-7 --&gt; LED 3 off
  Click-8 --&gt; LED 4 off
  Click-9 --&gt; LED 1 on
  …
  </code></pre>
  
  <p>as far I have this, but the button click detection isn't working as expected: </p>
  
  <pre><code>/*
   * This file is part of the libopencm3 project.
   *
   * Copyright (C) 2009 Uwe Hermann &lt;uwe@hermann-uwe.de&gt;,
   * Copyright (C) 2010 Piotr Esden-Tempski &lt;piotr@esden.net&gt;
   * Copyright (C) 2011 Stephen Caudle &lt;scaudle@doceme.com&gt;
   *
   * This library is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this library.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
   */
  
  #include &lt;libopencm3/stm32/f4/rcc.h&gt;
  #include &lt;libopencm3/stm32/f4/gpio.h&gt;
  
  uint16_t exti_line_state;
  
  /* Set STM32 to 168 MHz. */
  static void clock_setup(void)
  {
      rcc_clock_setup_hse_3v3(&amp;hse_8mhz_3v3[CLOCK_3V3_168MHZ]);
  }
  
  static void gpio_setup(void)
  {
      /* Enable GPIOD clock. */
      rcc_peripheral_enable_clock(&amp;RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
  
      /* Set GPIO12 (in GPIO port D) to 'output push-pull'. */
      gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT,
              GPIO_PUPD_NONE, GPIO12 | GPIO13 | GPIO14 | GPIO15);
  }
  
  static void button_setup(void)
  {
      /* Enable GPIOA clock. */
      rcc_peripheral_enable_clock(&amp;RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
  
      /* Set GPIO0 (in GPIO port A) to 'input open-drain'. */
      gpio_mode_setup(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO0);
  }
  
  int main(void)
  {
  
      int i;
      i = 1;
      clock_setup();
      button_setup();
      gpio_setup();
  
      while (1) {
  
          exti_line_state = GPIOA_IDR;
          if ((exti_line_state &amp; (1 &lt;&lt; 0)) != 0) {
  
              if(i == 1){
                  i = 2;
                  gpio_toggle(GPIOD, GPIO12);
              }
              if(i == 2){
                  i = 3;
                  gpio_toggle(GPIOD, GPIO13);
              }
              if(i == 3){
                  i = 4;
                  gpio_toggle(GPIOD, GPIO14);
              }
              if(i == 4){
                  i = 5;
                  gpio_toggle(GPIOD, GPIO15);
              }
              if(i == 5){
                  i = 1;
                  //Shut off all LEDs
              }
  
          } 
      }
  
      return 0;
  }
  </code></pre>