title: C program a button to perform a task once when pressed (latch)
body: <p>I am relatively new to c and the Raspberry Pi and am trying simple programs. What I would like is when the button is pressed it printfs once and doesn't printf again until the button is pressed again, even if the button is held down (sort of a latch). I thought maybe adding the second while loop in would fix this, but sometimes it still doesn't detect a button press.</p>
  
  <pre><code>#include &lt;bcm2835.h&gt;
  #include &lt;stdio.h&gt;
  #define PIN RPI_GPIO_P1_11
  
  int main()
  {
      if(!bcm2835_init())
          return 1;
  
      bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
  
      while(1)
      {
          if(bcm2835_gpio_lev(PIN))
          {
              printf("The button has been pressed\n");
          }
  
         while(bcm2835_gpio_lev(PIN)){}
      }
  
      bcm2835_close();
      return 0;
  }
  </code></pre>