Home Micro BitMicro Bit Code MIcrobit RGB led examples

MIcrobit RGB led examples

This was another couple of RGB led examples for the Micro:bit

Layout

anode rgb_bb

Code

The first example just shows some extra colour combinations

[codesyntax lang=”python”]

from microbit import *

while True:  
    pin0.write_digital(0)
    pin1.write_digital(1)
    pin2.write_digital(1)
    sleep(500)
    pin0.write_digital(1)
    pin1.write_digital(0)
    pin2.write_digital(1)
    sleep(500)
    pin0.write_digital(1)
    pin1.write_digital(1)
    pin2.write_digital(0)
    sleep(500)
    pin0.write_digital(0)
    pin1.write_digital(0)
    pin2.write_digital(1)
    sleep(500)
    pin0.write_digital(1)
    pin1.write_digital(0)
    pin2.write_digital(0)
    sleep(500)
    pin0.write_digital(0)
    pin1.write_digital(1)
    pin2.write_digital(0)
    sleep(500)
    pin0.write_digital(0)
    pin1.write_digital(0)
    pin2.write_digital(0)
    sleep(500)

[/codesyntax]

 

The next example displays a colour depending on whether button a, button b or no button is pressed

[codesyntax lang=”python”]

from microbit import *
while True:
    if button_a.is_pressed():
        pin0.write_digital(0)
        pin1.write_digital(1)
        pin2.write_digital(1)
    elif button_b.is_pressed():
        pin0.write_digital(1)
        pin1.write_digital(0)
        pin2.write_digital(1)
    else:
        pin0.write_digital(1)
        pin1.write_digital(1)
        pin2.write_digital(0)

[/codesyntax]

You may also like