7. NeoPixel

7.1. Zasvietenie jednej LEDky

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin1, 8)
np[0] = (255, 0, 0)
np.show()

7.2. Zasvietenie všetkých LEDiek

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin1, 8)
np[0] = (255, 0, 0)
np[1] = (255, 0, 0)
np[2] = (255, 0, 0)
np[3] = (255, 0, 0)
np[4] = (255, 0, 0)
np[5] = (255, 0, 0)
np[6] = (255, 0, 0)
np[7] = (255, 0, 0)
np.show()

7.3. Zasvietenie všetkých LEDiek cyklom

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin1, 8)

for i in [0, 1, 2, 3, 4, 5, 6, 7]:
    np[i] = (255, 0, 0)
np.show()
from microbit import *
import neopixel

np = neopixel.NeoPixel(pin1, 8)

for i in range(0, 8, 1):
    np[i] = (255, 0, 0)
np.show()

7.4. Jednosmerný Knight Rider

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin1, 8)

while True:

    for i in range(0, len(np)):
        for led_id in range(0, len(np)):
            if i == led_id:
                np[led_id] = (0, 255, 0)
            else:
                np[led_id] = (0, 0, 0)
        sleep(100)
        np.show()

Úloha: Naprogramuj NeoPixel tak, aby sa LEDka odrazila naspäť

7.5. FadeIn and FadeOut

from microbit import *
import neopixel

np = neopixel.NeoPixel(pin1, 8)

while True:

    # fade in
    for i in range(0, 256, 1):
        for led_id in range(len(np)):
            np[led_id] = (0, i, 0)
        np.show()

    # fade out
    for i in range(255, 0, -1):
        for led_id in range(len(np)):
            np[led_id] = (0, i, 0)
        np.show()

    # clear
    for i in range(len(np)):
        np[i] = (0, 0, 0)
    np.show()

7.6. Náhodné generovanie farieb

from microbit import *
import neopixel
from random import randint, seed

seed(5)
np = neopixel.NeoPixel(pin1, 8)

while True:

    for i in range(0, len(np)):
        red = randint(0, 255)
        green = randint(0, 255)
        blue = randint(0, 255)

        np[i] = (red, green, blue)

        np.show()
        sleep(500)

Úloha: Pozmeň program tak, aby každú sekundu zmenil farby všetkých LED diód naraz