Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Need help tonight PLEASE :) Can you re-arrange this code properly?

needhelpnow

New Coder
When first button is pressed it does led, led1, led2. When it is pushed a second time i need it to be led2, led and led1
I need this to alternate every time the button is pressed



import machine
import time
import utime


blue_led = machine.Pin(20, machine.Pin.OUT)
blue_led1 = machine.Pin(18, machine.Pin.OUT)
blue_led2 = machine.Pin(16, machine.Pin.OUT)
blue_btn = machine.Pin(13, machine.Pin.IN, machine.Pin.PULL_UP)



blue_last = time.ticks_ms()


def button_handler(pin):
global blue_last, blue_btn

if pin is blue_btn:
if time.ticks_diff(time.ticks_ms(), blue_last) > 800:
blue_led.value(1)
utime.sleep(0.2)
blue_led.value(0)
blue_last = time.ticks_ms()

utime.sleep(4.0)

blue_led1.value(1)
utime.sleep(0.2)
blue_led1.value(0)

utime.sleep(24)

blue_led2.value(1)
utime.sleep(0.2)
blue_led2.value(0)

blue_last = time.ticks_ms()



blue_led.value(0)
blue_btn.irq(trigger = machine.Pin.IRQ_RISING, handler = button_handler)

blue_led1.value(0)
blue_btn.irq(trigger = machine.Pin.IRQ_RISING, handler = button_handler)

blue_led2.value(0)
blue_btn.irq(trigger = machine.Pin.IRQ_RISING, handler = button_handler)
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom