Actions

Microbit Code02:buttons&arrows

From /oooooooooo.io/

Revision as of 10:15, 1 September 2019 by Andre (talk | contribs) (Text replacement - "HackPact" to "Code Notes")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
from microbit import *

# Button clicks point LED arrows to clicked button
# a + b button clicked LED arrows: North
# b + a button clicked LED arrows: South

pressed = []
while True:
    # logic conditions: buttons reading
    if button_a.is_pressed():
        if 'a' not in pressed:
            # action: only a is pressed
            pressed.append('a')
    if button_b.is_pressed():
        # if rather than elif as:
        # 1st condition can be True 
        # at same time that 2nd condition is also True
        if 'b' not in pressed:
            # action: only b is pressed
            pressed.append('b')
    if button_a.is_pressed() is False and button_b.is_pressed():
        if 'a' in pressed:
            # action: from 2 buttons pressed a is released, while b is pressed
            pressed.remove('a')
    if button_b.is_pressed() is False and button_a.is_pressed():
        if 'b' in pressed:
            # action: from 2 buttons pressed b is released, while a is pressed
            pressed.remove('b')
    if button_a.is_pressed() is False and button_b.is_pressed() is False:
        # action: both buttons released
        pressed = []
    
    # display conditions: reading pressed list    
    if len(pressed) is 0:    
        display.show(Image.NO)
    elif len(pressed) is 1:
        if 'a' in pressed:
            display.show(Image.ARROW_W)
        elif 'b' in pressed:
            display.show(Image.ARROW_E)
    elif len(pressed) is 2:
        if pressed == ['a', 'b']:
            display.show(Image.ARROW_N)
        elif pressed == ['b', 'a']:
            display.show(Image.ARROW_S)

Code Notes 2019

SectionCode Notes +
Date
"Date" is a type and predefined property provided by Semantic MediaWiki to represent date values.
2019 +