---
# System prepended metadata

title: naloga 1. a

---

naloga 1. a
"""1. Naloga: Alternirajče utripanje vsake druge LED diode:
Napiši program, ki naj  prižge vsako prvo in drugo LED diodo v različni barvi. Barvi naj se izmenjujeta na eno sekundo.
Primer 8 Ledic. (R - rdeča, M - modra)R, M, R, M, R, M, R, M
M, R, M, R, M, R, M, R
R, M, R, M, R, M, R, M"""

from utime import sleep
from neopixel import NeoPixel
from machine import Pin

NUM_LED = 16

leds = NeoPixel(Pin(17), NUM_LED)

while True:
    # First sequence: Red and Blue
    for i in range(0, 16, 2):
        leds[i] = (255, 0, 0)  # Red
        leds.write()
        sleep(0.3)  # Adjust the delay as needed
        leds[i + 1] = (0, 0, 255)  # Blue
        leds.write()
        sleep(0.3)  # Adjust the delay as needed

    sleep(0.3)  # Delay between sequences
![image](https://hackmd.io/_uploads/r1yqvqeDp.png)

naloga 1.b

"""2. Naloga: Vklop in izklop LED diod v četrtinah obroča:
Razdeli Neopixel obroč na štiri dele in napiši program, ki vklopi in izklopi LED diode v vsaki četrtini zaporedoma. (naenkrat naj je prižgana samo ena četrtina)
BONUS: funkcijo pripravi, da krog razdeli na poljubno delov."""
from utime import sleep
from neopixel import NeoPixel
from machine import Pin

NUM_LED = 16

leds = NeoPixel(Pin(17), NUM_LED)

a = 0
b = 4

while True:
    for i in range(a, b, 2):
        leds[i] = (255, 0, 0)  # Red
        leds[i + 1] = (0, 0, 255)  # Blue
        leds.write()

    sleep(1)  # Delay between quarters

    # Turn off all LEDs
    for i in range(NUM_LED):
        leds[i] = (0, 0, 0)
    leds.write()

    sleep(1)  # Delay before the next quarter

    # Update the start and end indices for the next quarter
    a = (a + 4) % NUM_LED
    b = (b + 4) % NUM_LED
    ![image](https://hackmd.io/_uploads/H1TM3cxD6.png)

1.c
"""3. Naloga: LED ruleta:
Ustvari program, ki naključno izbere eno LED diodo in jo prižge. Pred tem naj se lučka vrti po krogu kot pri ruleti, nato pa se ustavi na naključno izbrani LED diodi.
 Po kratkem časovnem zamiku jo ugasne, nato pa izbere novo LED diodo."""
import random
from utime import sleep
from neopixel import NeoPixel
from machine import Pin

NUM_LED = 16

leds = NeoPixel(Pin(17), NUM_LED)

a = 0
b = random.randint(1, 16)

while True:
    for i in range(0, b+1, 1):
        if i == b:
            sleep(2)
            leds[i] = [0, 0, 0]
            b = random.randint(1, 16)
        leds[i] = (255, 0, 0)  # Red
        leds.write()
        sleep(0.1)
        leds[i] = (0, 0, 0)
    print(b)
![image](https://hackmd.io/_uploads/SJ28kjeP6.png)

2.a
"""1. Hitrost animacije
Naredi preprosto animacijo na Neopixel krogu, 
kjer se ena LEDica vrti okoli kroga. S potenciometrom kontroliramo hitrost vrtenja."""

from utime import sleep
from neopixel import NeoPixel
from machine import Pin, ADC


NUM_LED = 16

leds = NeoPixel(Pin(17), NUM_LED)

adc = ADC(34)
while True:
    for i in range(0, NUM_LED, 1):
        pot = adc.read()
        delay_time = (pot / 4095) * 0.5 + 0.05

        leds[i] = (255, 0, 0) 
        sleep(delay_time)
        leds.write()
        sleep(delay_time)
       
        leds[i] = (0, 0, 0)  # Turn off the LED
        sleep(delay_time)
        print(i)
       [image](https://hackmd.io/_uploads/rJKyhogPa.png)

3.a
from machine import Pin, ADC
from neopixel import NeoPixel
from utime import sleep_ms
import urandom

igra 

# Configuration
NUM_LED = 8  # Adjust the number of LEDs in your NeoPixel rings
NEOPIXEL_PIN_1 = 17  # Adjust the pin number for the 1st NeoPixel ring
NEOPIXEL_PIN_2 = 18  # Adjust the pin number for the 2nd NeoPixel ring
POTENTIOMETER_PINS = [34, 35, 36]  # Adjust the pin numbers for the potentiometers
BUTTON_PIN = 15  # Adjust the pin number for the button

# Initialize NeoPixels
neopixel_1 = NeoPixel(Pin(NEOPIXEL_PIN_1, Pin.OUT), NUM_LED)
neopixel_2 = NeoPixel(Pin(NEOPIXEL_PIN_2, Pin.OUT), NUM_LED)

# Initialize potentiometers
potentiometers = [ADC(Pin(pin)) for pin in POTENTIOMETER_PINS]

# Initialize button
button = Pin(BUTTON_PIN, Pin.IN)

def generate_random_color():
    """Generate a random RGB color."""
    return (urandom.randint(0, 255), urandom.randint(0, 255), urandom.randint(0, 255))

def calculate_color_difference(color1, color2):
    """Calculate the absolute difference between two RGB colors."""
    return sum(abs(c1 - c2) for c1, c2 in zip(color1, color2))

def display_color(neopixel, color):
    """Display a color on the NeoPixel ring."""
    neopixel.fill(color)
    neopixel.write()

def main():
    while True:
        # Generate a random color for the 1st NeoPixel ring
        target_color = generate_random_color()
        display_color(neopixel_1, target_color)

        # Allow the player to adjust the color on the 2nd NeoPixel ring
        while True:
            adjusted_color = tuple(pot.read() for pot in potentiometers)
            display_color(neopixel_2, adjusted_color)

            # Check if the button is pressed
            if button.value() == 0:
                break

            sleep_ms(50)

        # Calculate color difference and display results
        difference = calculate_color_difference(target_color, adjusted_color)
        print("RGB Differences:", adjusted_color[0] - target_color[0], adjusted_color[1] - target_color[1], adjusted_color[2] - target_color[2])
        print("Total Difference:", difference)

        sleep_ms(2000)  # Wait for a moment before starting a new game

if __name__ == "__main__":
    main()
    

    

    