Out of sequence output

Viewed 41

I built a little toy for my kid who is fascinated by gadgets and lights. It's a box with 6 LEDs and 2 buttons. Push a button, an LED turns on, press it again, another lights up, etc. Press the other button and one light turns off...etc.

A weird thing is happening though where the lights are not turning on and off in order. It's mostly in order, but not always.

So here's the code. I know this is not the ideal way to do this. My goal was to get it working, and then learn to do it by using increments, which could then loop around.

import time
import board
import digitalio
from digitalio import DigitalInOut, Direction, Pull

btn1 = DigitalInOut(board.D12)
btn1.direction = Direction.INPUT
btn1.pull = Pull.UP

btn2 = DigitalInOut(board.D13)
btn2.direction = Direction.INPUT
btn2.pull = Pull.UP

led3 = digitalio.DigitalInOut(board.A1)
led3.direction = digitalio.Direction.OUTPUT

led2 = digitalio.DigitalInOut(board.A2)
led2.direction = digitalio.Direction.OUTPUT

led1 = digitalio.DigitalInOut(board.A3)
led1.direction = digitalio.Direction.OUTPUT

led4 = digitalio.DigitalInOut(board.A4)
led4.direction = digitalio.Direction.OUTPUT

led5 = digitalio.DigitalInOut(board.A5)
led5.direction = digitalio.Direction.OUTPUT

led6 = digitalio.DigitalInOut(board.SCK)
led6.direction = digitalio.Direction.OUTPUT



while True:
    if not btn1.value and not led1.value:
        led1.value = True
        time.sleep(0.2)
        continue
    elif not btn1.value and not led2.value:
        led2.value = True
        time.sleep(0.2)
        continue
    elif not btn1.value and not led3.value:
        led3.value = True
        time.sleep(0.2)
        continue
    elif not btn1.value and not led4.value:
        led4.value = True
        time.sleep(0.2)
        continue    
    elif not btn1.value and not led5.value:
        led5.value = True
        time.sleep(0.2)
        continue
    elif not btn1.value and not led6.value:
        led6.value = True
        time.sleep(0.2)
        continue
    elif not btn2.value and led6.value:
        led6.value = False
        time.sleep(0.2)
        continue
    elif not btn2.value and led5.value:
        led5.value = False
        time.sleep(0.2)
        continue
    elif not btn2.value and led4.value:
        led4.value = False
        time.sleep(0.2)
        continue
    elif not btn2.value and led3.value:
        led3.value = False
        time.sleep(0.2)
        continue
    elif not btn2.value and led2.value:
        led2.value = False
        time.sleep(0.2)
        continue
    elif not btn2.value and led1.value:
        led1.value = False
        time.sleep(0.2)
        continue

I wouldn't mind some guidance as far as doing it by increments.

But also, I am wondering why this code is going out of sequence like this:

on 1
on 2
on 3
on 4
on 5
on 6
off 4
off 6
off 5
off 3
off 2
off 1
on 1
on 2
on 3
on 4
on 5
on 6
off 5
off 6
off 4
off 3
off 2
off 1
on 1
on 2
on 3
on 4
on 5
on 6
off 2
off 6
off 5
off 4
off 3
off 1
on 1
on 2
on 3
on 4
on 5
on 6
off 6
off 5
off 4
off 3
off 2
off 1
on 2
on 1
on 3
on 4
on 5
on 6
off 6
off 5
off 4
off 3
off 2
off 1
on 6
on 1
on 2
on 3
on 4
on 5
off 6
off 5
off 4
off 3
off 2
off 1
on 3
on 1
on 2
on 4
on 5
on 6
off 6
off 5
off 4
off 3
off 2
off 1
on 2
on 1
on 3
on 4
on 5
on 6
off 2
off 6
off 5
off 4
off 3
off 1

0 Answers
Related