title: LED control on Raspberry Pi by GPIO with Python
body: <p>I am using momentary switches wired to GPIO pins on a Raspberry Pi to control 4 LEDs.
  I have five buttons wired up.
  The first 4 buttons when pressed toggle the state of a connected LED from on to off or off to on depending on the current state.
  The fifth button turns on all 4 LEDs or turns off all 4 LEDs based on the on/off state of GPIO 18.
  I also have an extra LED wired to GPIO pin 18, this simply turns on or off based on the state of pin 18.
  The idea of this project is to be able to independently control the LEDs and have a master control button as well. The LED attached to pin 18 is a monitoring LED, it should be on if ANY of the 4 LEDs is on and it should only be off if all 4 of the LEDs are off simultaneously. 
  This is all based on python scripts that monitor for button presses and act accordingly.</p>
  
  <p>The code I've written to control all LEDs at once looks like this:</p>
  
  <pre><code>import RPi.GPIO as GPIO
  import time
  
  GPIO.setwarnings(False)
  
  GPIO.setmode(GPIO.BCM)
  
  GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  
  chan_list = (4,17,22,27)
  
  GPIO.setup(chan_list,GPIO.OUT)
  
  GPIO.output(chan_list,0)
  
  GPIO.setup(18,GPIO.OUT)
  
  GPIO.output(18,0)
  
  while True:
      input_state = GPIO.input(26)
      if input_state == False:
           chan_list = (4,17,22,27)
           GPIO.output(18, not GPIO.input(18))
           time.sleep(0.1)
           GPIO.output(chan_list, GPIO.input(18))
           time.sleep(0.4)
  </code></pre>
  
  <p>This code appears to operate perfectly. As you can see it toggles the current state of pin 18 and then applies that state to pins 4,17,22 and 27 which are the pins the LEDs are connected to.</p>
  
  <p>The code I've written to control the individual LEDs is a bit more complicated and it looks like this:</p>
  
  <pre><code>import RPi.GPIO as GPIO
  import time
  
  GPIO.setwarnings(False)
  
  GPIO.setmode(GPIO.BCM)
  
  GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  
  GPIO.setup(4,GPIO.OUT)
  GPIO.output(4,0)
  
  GPIO.setup(17,GPIO.OUT)
  
  GPIO.setup(22,GPIO.OUT)
  
  GPIO.setup(27,GPIO.OUT)
  
  GPIO.setup(18,GPIO.OUT)
  
  while True:
      input_state = GPIO.input(5)
      if input_state == False:
          if GPIO.output(17,0) == False:
              GPIO.output(4, not GPIO.input(4))
          elif GPIO.output(22,0) == False:
              GPIO.output(4, not GPIO.input(4))
          elif GPIO.output(27,0) == False:
              GPIO.output(4, not GPIO.input(4))
          else:
              GPIO.output(4, not GPIO.input(4))
              time.sleep(0.1)
              GPIO.output(18, GPIO.input(4))
              time.sleep(0.4)
  </code></pre>
  
  <p>There are 4 versions of this with the pins adjusted, one version for each LED, This version uses pin 5 as an input to detect button presses and pin 4 as an output to activate the LED. What I want is to do is this:</p>
  
  <p>When pressed (if LED1 is off) </p>
  
  <pre><code>- Toggle LED1 on, also toggle pin 18 on to activate the indicator light.
  - Take no further action.
  </code></pre>
  
  <p>When pressed (if LED1 is on) </p>
  
  <pre><code>- Check if pin 17 is on or off; 
      - If pin 17 is on toggle LED1 off and take no further action. 
      - If pin 17 is off check if pin 22 is on or off; 
          - If pin 22 is on toggle LED1 off and take no further action. 
          - If pin 22 is off check if pin 27 is on or off; 
              - If pin 27 is on toggle LED1 off and take no further action. 
              - If pin 27 is off toggle LED1 off then set pin 18 to the current status of pin 4 (LED1).
  </code></pre>
  
  <p>However, what is actually happening is this:</p>
  
  <p>When pressed (if LED1 is off)</p>
  
  <pre><code>- Turns off all of the other 3 LEDs then toggles LED1 on and toggles pin 18 on.
  </code></pre>
  
  <p>When pressed (if LED1 is off)</p>
  
  <pre><code>- Turns off all LEDs and toggles pin 18 off.
  </code></pre>
  
  <p>I can not for the life of me figure this out.
  Your help is hugely appreciated.</p>
  
  <p>P.S. Pardon my ignorance, I started learning python yesterday and have no prior programming experience at all. I'm sure it's something simple but I can't seem to solve it.</p>