title: Interrupts in Python
body: <p>I wrote a simple code to test interrupts in Python:</p>
  
  <pre><code>import RPi.GPIO as GPIO
  
  brakepin = 32
  brake_value = 0
  
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(brakepin, GPIO.IN, pull_up_down = GPIO.PUD_UP) #Brake
  
  def brake_isr(channel):
      global brake_value 
      print ("Entered interrupt")
      brake_value = GPIO.input(brakepin)
  
  GPIO.add_event_detect(brakepin, GPIO.RISING, callback = brake_isr)
  
  while True:
      print("Brake_value: %d" % (brake_value))
      time.sleep(0.025)
  </code></pre>
  
  <p>I am using interrupts to capture when the brakes are pressed on a scooter and when they are not. Ofcourse, 1 indicates the brakes are pressed and 0 indicates they are not. I have three questions:</p>
  
  <ol>
  <li><p>In the line:
  <code>GPIO.setup(brakepin, GPIO.IN, pull_up_down = GPIO.PUD_UP)</code> what should I put the value of pull_up_down? UP or DOWN. Actually both are working the same way, there is no difference. The brake's default value is 0 (when not pressed) because it is connected to ground.</p></li>
  <li><p>In the line: <code>GPIO.add_event_detect(brakepin, GPIO.RISING, callback = brake_isr)</code> should I put GPIO.RISING or FALLING? Since I want to detect brake events (i.e. when a brake is being pressed and when it is not) I think I should use RISING only.</p></li>
  <li><p>One very weird thing is happening:
  When I press the brake I get output as:</p></li>
  </ol>
  
  <p><strong>Brake_value: 1</strong>                   -> This is as expected. Great!</p>
  
  <p>When I let go off the brake it still remains 1, i.e.:</p>
  
  <p><strong>Brake_value: 1</strong>                -> This should be 0</p>
  
  <p>Why is the brake value not coming back to 0 when I let go of the brake</p>
  
  <p><strong>Edit:</strong>
  I modified my interrupt code block to this:</p>
  
  <pre><code>def brake_isr(channel):
      print ("Entered interrupt")
      global brake_value
      while GPIO.input(brakepin) == 1:
         brake_value = GPIO.input(brakepin)
  
      brake_value = 0
  </code></pre>
  
  <p>Well this solved the problem that I was facing with Question 3. However another weird thing is happening with the output (which was happening even before):</p>
  
  <p><strong>Brake_value: 0</strong><br>
  .<br>
  .<br>
  .<br>
  When I press the brakes:<br>
  <strong>Entered interrupt</strong><br>
  <strong>Entered interrupt</strong><br>
  <strong>Entered interrupt</strong>  </p>
  
  <p><strong>Brake_value: 1</strong><br>
  <strong>Brake_value: 1</strong><br>
  .<br>
  .<br>
  .<br>
  When I let go of the brakes:<br>
  <strong>Entered interrupt</strong><br>
  <strong>Entered interrupt</strong><br>
  <strong>Brake_value: 0</strong><br>
  <strong>Brake_value: 0</strong><br>
  .<br>
  .<br>
  .<br>
  Why is the code entering the interrupt block several times when I press the brakes (as can be seen with the multiple print statement) and why is it entering the interrupt when I let go off the brakes? This is happening for <code>time.sleep(1)</code> or <code>time.sleep(0.025)</code>, deosnt matter what the delay is.</p>