0

I'm working on project with single input multiple output logic. The project require two or more led controlled by single button (momentary). the steps are like these : 1. When the button pressed for first time, LED 1 on 2. When the button pressed for the second time, LED 1 still on, LED 2 turned on and so on for more led..

I've tried to use if nested statement but it didn't work for me. i want to use normal rpi.gpio library, not the tkinter lib, can anyone help me?

  • So it is like this? (1) 1 on, all others off, (2) 1, 2, on all others off, (3) 1, 2, 3 on, all others off? – tlfong01 Apr 14 at 3:53
  • 1
    If what you tried didn't work,try something else.If you want help, write exactly what you tried. – RalfFriedl Apr 14 at 5:00
1

I would have a list of leds and increment an led position each time the button is pressed. Then turn on/off the leds according to the led position.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# setup button (assuming button connected to ground and GPIO16)
button=16
GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_UP)

leds = [23,24,25]
for led in leds:
    GPIO.setup(led,GPIO.OUT)
    GPIO.output(led,GPIO.LOW)

ledPos=0
while True:
    if GPIO.input(button) == GPIO.LOW:
        print(ledPos)
        for ledn in range(len(leds)):
            if ledn <= ledPos:
                print(leds[ledn],'on')
                GPIO.output(leds[ledn],GPIO.HIGH)
            else:
                print(leds[ledn],'off')
                GPIO.output(leds[ledn],GPIO.LOW)
        ledPos+=1
        if ledPos>2:
            ledPos=-1
    time.sleep(0.1)

enter image description here

0

In addition to the answer given by @codermike I'd point out that the general principle you're looking for is known as a state machine. Although your example is fairly trivial to implement with some logic (because the output pattern is linked to the button press count) if you wanted to expand the system to have other inputs or break the pattern then changing a state machine can make life easier. You might argue that it's also clearer what the code is doing.

I'm no Python expert so the following might not actually work as intended but gives a rough idea of how you might implement a state machine for your described system, think of it more as pseudo-code. Actual Python experts could probably make it a lot more elegant with lists and interrupts and whatnot...

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# Setup button
GPIO.setup(16,GPIO.IN,pull_up_down=GPIO.PUD_UP)

# Setup LEDs as output.
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)

# Start in state zero
state = 0

# Run the state machine in a loop
while True:
  # Check which state we're in.
  if state == 0
    stateZero()
  elif state == 1
    stateOne()
  elif state == 2
    stateTwo()
  elif state == 3
    stateThree()
  else
    # Should never get here!
    stateFinal()

  # Wait for a button press to move to next state
  while GPIO.input(16) == GPIO.HIGH:
    time.sleep(0.1)

#
# Now we have functions for each state in the machine that will
# set the LEDs we want and tell the state machine what the next
# state will be.
#

# State 0: All LEDs off, next state 1
def stateZero():
  GPIO.output(23,GPIO.LOW)
  GPIO.output(24,GPIO.LOW)
  GPIO.output(25,GPIO.LOW)
  state = 1
  return

def stateOne():
  GPIO.output(23,GPIO.HIGH)
  GPIO.output(24,GPIO.LOW)
  GPIO.output(25,GPIO.LOW)
  state = 2
  return

def stateTwo():
  GPIO.output(23,GPIO.HIGH)
  GPIO.output(24,GPIO.HIGH)
  GPIO.output(25,GPIO.LOW)
  state = 3
  return

# State 3: All LEDs on, next state back to 1
def stateThree():
  GPIO.output(23,GPIO.HIGH)
  GPIO.output(24,GPIO.HIGH)
  GPIO.output(25,GPIO.HIGH)
  state = 1
  return

# Safety state in case something goes wrong, go back to state 0.
def stateFinal():
  state = 0
  return

Say you wanted to add new "state" between stateOne and stateTwo then you'd write the new function; add a check for it in the main loop (note that the value in the state variable does not have to match the logical state order) and then change the value assigned to state at the end of stateOne to "point" to the new state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.