---
# System prepended metadata

title: Test

---

# Test
## 1.Vaja
Prva ledica se prižge in druga se ugasne. To se ponavlja.
```python
from machine import Pin
from time import sleep

l1 = Pin(21, Pin.OUT)
l2 = Pin(22, Pin.OUT)
d = 1
while True:
    l1.value(1)
    sleep(d)
    l2.value(0)
    sleep(d)
    
    l1.value(0)
    sleep(d)
    l2.value(1)
    sleep(d)
```
![image](https://hackmd.io/_uploads/SkukkwjEa.png)


## 2.Vaja
Prižgi vse lučke z naključno barvo
```python
import neopixel
from machine import Pin
from time import sleep
from random import randrange as r

num_leds = 16
np = neopixel.NeoPixel(Pin(18), num_leds)
rad = [r(255), r(255), r(255)]

while True:
    for i in range(num_leds):
        np[i] = rad
    np.write()
```
![image](https://hackmd.io/_uploads/B178xviE6.png)

## 3.Vaja
Prižgi lučke navzkrižno z naključno barvo.
```python
from machine import Pin
from utime import sleep
import neopixel

NUM_LEDS = 16
leds = neopixel.NeoPixel(Pin(18), NUM_LEDS)
I =[0,NUM_LEDS//4,NUM_LEDS//2,NUM_LEDS//2+NUM_LEDS//4]


while True:
    leds[I[0]]=[204, 204, 255]
    leds[I[1]]=[222, 49, 99]
    leds[I[2]]=[100, 149, 237]
    leds[I[3]]=[159, 226, 191]

    leds.write()
    leds[I[0]]=[0,0,0]
    leds[I[1]]=[0,0,0]
    leds[I[2]]=[0,0,0]
    leds[I[3]]=[0,0,0]

    for i in range(len(I)):
        if I[i]<NUM_LEDS-1:
            I[i]+=1
        else:
            I[i]=0
        
    sleep(1)

```
![image](https://hackmd.io/_uploads/By54bDs46.png)

## 4.Vaja
Ko pritisneš gumb se začne ledica vrteti v krogu.
```python=
from machine import Pin
from utime import sleep
import neopixel

NUM_LEDS = 16
btns = Pin(25, Pin.IN, Pin.PULL_UP)
leds = neopixel.NeoPixel(Pin(18), NUM_LEDS)
bl = [255, 0, 0]

index = 0
while True:
    if index >= 16:
        index = 0
    btn = not bool(btns.value())
    for i in range(NUM_LEDS):
        index += 1
        leds[i] = bl
        if btn:
            leds.write()
            sleep(0.1)
        leds[i] = [0, 0, 0]

```
![image](https://hackmd.io/_uploads/ByaPQvi4a.png)


## 5.Vaja
Prižgi lučke in jih spreminjaj z potencialometi.
```python
from machine import Pin, ADC
from utime import sleep
import neopixel

NUM_LEDS = 16
leds = neopixel.NeoPixel(Pin(18), NUM_LEDS)
pot = ADC(Pin(2))
pot1 = ADC(Pin(0))
pot3 = ADC(Pin(4))


while True:
    leds.fill([pot.read()//16, pot1.read()//16, pot3.read()//16])
    leds.write()


```
![image](https://hackmd.io/_uploads/r1vLSPoNp.png)
## 6.Vaja
Opravljaj hitrost lučke s pomočjo potencialometra.
```python=
from machine import Pin, ADC
from utime import sleep
import neopixel

num_led = 16
pot = ADC(Pin(33))
leds = neopixel.NeoPixel(Pin(4), num_led)

while True:
    for i in range(num_led):
        leds[i-1] = [0,0,0]
        leds[i] = [150,200,125]

        sleep(max(pot.read() / 1000, 0.1))
        leds.write()
```
![](https://hackmd.io/_uploads/HJUUxmExa.png)

## 7.Vaja
S potenciaometrom opravljaj barve.
```python=
from machine import Pin, ADC
from utime import sleep
import neopixel
from random import randrange

num_led = 16
pot = ADC(Pin(33))
leds = neopixel.NeoPixel(Pin(4), num_led)

while True:
    for i in range(num_led):
        leds[i] = [randrange(255), randrange(255), randrange(255)]
        sleep(max(pot.read() / 1000, 0.1))
        leds.write()
    break
```
![](https://hackmd.io/_uploads/rJItw74e6.png)

## 8.Vaja
- 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)
    1. R, M, R, M, R, M, R, M
    2. M, R, M, R, M, R, M, R
    3. R, M, R, M, R, M, R, M
    4. M, R, M, R, M, R, M, R
```python
import neopixel
from time import sleep
from machine import Pin

NUM_PIN = 8
np = neopixel.NeoPixel(Pin(23), NUM_PIN)

rd = [250,0,0]
bl = [0,0,250]
isrd = False

while True:
  for item in range(NUM_PIN):
    if isrd:
      np[item] = bl
      isrd = not isrd
    else:
      np[item] = rd
      isrd = not isrd
  isrd = not isrd
  np.write()
  sleep(1)

```
ali
```pyton
import neopixel
from time import sleep
from machine import Pin

NUM_PIN = 16
np = neopixel.NeoPixel(Pin(12), NUM_PIN)

red = [250,0,0]
blue = [0,0,250]

prvi = [i for i in range(0, NUM_PIN, 2)]
drugi = [i for i in range(1, NUM_PIN, 2)]
while True:
    for en in prvi:
        np[en] = blue
    for drug in drugi:
        np[drug] = red
    prvi, drugi = drugi, prvi
    np.write()
    sleep(1)
```
![image](https://hackmd.io/_uploads/By7XMh-Sa.png)

## 9.Vaja
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)
```python
import neopixel
from time import sleep
from machine import Pin

NUM_PIN = 16
np = neopixel.NeoPixel(Pin(19), NUM_PIN)
red = [255,0,0]

def turn_on_lights(np, num_lights, color, t):
    # sva na začetku: morava prižgati število lučk od 0 --> num_lights
    # sva nekje vmes: morava prižgati lučke med: t --> t + num_lights
    # kaj je ta t (to je večkratnik num_lights) 0, 1*num_lights, 2*.....
    for i in range(t, t+num_lights):
        np[i] = color
    np.write()

num_divisions = 4
num_lights = NUM_PIN//num_divisions
index = 0
while True:
    if index >= NUM_PIN:
        index = 0
    turn_on_lights(np, num_lights, red, index)
    index += num_lights
    # moramo ugasniti lučke
    for i in range(NUM_PIN):
        np[i] = [0,0,0] 
    sleep(1)
```
![image](https://hackmd.io/_uploads/r1z0X3WBa.png)

## 10.Vaja
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.

```python
import neopixel
from time import sleep
from machine import Pin
from random import randint

NUM_PIN = 16
np = neopixel.NeoPixel(Pin(23), NUM_PIN)
bl = [255,255,255]
gr = [0,255,0]

def vrtenje(np, color, NUM_PIN, num_steps):
    """

    Funkcija izvede vrtenje za num_steps
    color .... barva pri vrtenju
    NUM_PIN ... število vseh pinov
    num_steps ... število korakov vrtenja
    """
    for t in range(num_steps):
        # vse pobarvat črno
        for i in range(NUM_PIN):
            np[i] = [0,0,0]
        # pobarvat t-to vrednost
        pin_index = t%NUM_PIN 
        np[pin_index] = color
        np.write()
        sleep(0.025)
    return pin_index

while True:
    # izbereva naključno število
    random = randint(0, 100)

    #vrtiva
    pin_index = vrtenje(np, bl, NUM_PIN, random)

    #pobarvava izbrano vrednost
    # final_index = random%NUM_PIN - 1
    np[pin_index] = gr
    np.write()
    
    # sleep
    sleep(2.5)
```
![image](https://hackmd.io/_uploads/H1FQE2Wra.png)

## 11.Vaja
```python

import neopixel
from machine import Pin, ADC

Num = 16
pot = ADC(Pin(27))
np = neopixel.NeoPixel(Pin(19), Num)


index = 0
while True:
    svetloba = int((pot.read()/4096)*255)
    svetloba = max(0, min(255, svetloba))
    np[index] = (svetloba, svetloba, svetloba)
    np.write()
```
![image](https://hackmd.io/_uploads/SktjpdhIa.png)

## 12.Vaja
Simuliraj gibanje sekundnega kazalca ure tako, da prižgeš LED diodo, ki predstavlja sekunde. Posodobi jo vsako sekundo.Uporabi 60 LED krog.
```python
import neopixel
from time import sleep
from machine import Pin
from random import randrange

NUM_PIN = 16
np = neopixel.NeoPixel(Pin(19), NUM_PIN)


sec_index = 0
while True:
  if sec_index >= NUM_PIN:
    sec_index = 0
  # color all black
  for i in range(NUM_PIN):
    np[i] = [0,0,0]
  np[sec_index] = [255,0,0]
  sec_index +=1
  np.write()
  sleep(1)
```
![image](https://hackmd.io/_uploads/SJgCHnWH6.png)

## 13.Vaja
vsaka led lučka se prižge drug za drugim od zadaj naprej, prej pa še naredi cel krog 
```python
import neopixel
import machine
import time
from random import randrange as r
NUMPIXELS = 100
pin = 15  
np = neopixel.NeoPixel(machine.Pin(pin), NUMPIXELS)
fill = 0
prev = []

def stack(pos, color, delay):
    #print(fill)
    # narišimo shranjene barve - fill pove koliko jih je
    for i in range(fill):
       # NUMPIXELS-i-1, ker rišemo iz zadnje strani
       # iz seznama prev prerišemo ledice iz zadnjega konca
        np[NUMPIXELS-i-1] = prev[i]
    np.write()
    # rotacija ledice do zadnje neprižgane ledice (do numpixels - fill)
    for i in range(0, NUMPIXELS - fill):
        if i != 0:
            np[i-1] = [0,0,0]
        np[i] = color  
        np.write()  
        time.sleep_ms(delay)        
# main loop
for i in range(NUMPIXELS):
    np.fill([0,0,0])
    col = [r(100,255),r(100,255),r(100,255)]
    stack(i, col, 100)  
    # povečamo število barv na koncu
    fill += 1
    #shranimo novo barvo 
    prev.append(col)

np.write()

```
![image](https://hackmd.io/_uploads/r1ZN83WS6.png)

## 14.Vaja
Upravljanje lucke z potenciometrom
```python
import neopixel
from machine import Pin, ADC
from time import sleep

num_led = 16
pot = ADC(Pin(12))
np = neopixel.NeoPixel(Pin(19), num_led)

color = [0,0,255]
def convert(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min




while True:
    x = convert(pot.read(), 0, 4096, 0, 16)
    for i in range(num_led):
        np[i] = [0,0,0]
        np[x] = color
        np.write()


```
![image](https://hackmd.io/_uploads/S1elbZCIp.png)


# -------------------------------------------------------
## Vaje za test
Zgradite vezje, ki vključuje potenciometer in NeoPixel diodo. Ko obračate potenciometer, naj se
intenzivnost svetlobe na NeoPixel diodi dinamično spreminja od najsvetlejše do najtemnejše.
Dodajte gumb, ki omogoča preklop med barvnimi načini (npr. rdeča, zelena, modra).

```python


from machine import Pin, ADC
import neopixel

pot = ADC(Pin(27))
num_leds = 16 
np = neopixel.NeoPixel(Pin(19), num_leds)

while True:
    svetloba = int((pot.read() / 4095) * 255)
    svetloba = max(0, min(255, svetloba))
    for i in range(num_leds):
        np[i] = (svetloba, svetloba, svetloba)
        np.write()

        
    gumb
    
    
from machine import Pin, ADC
import neopixel
import time

num_leds = 16
pot = ADC(Pin(14))
btn = Pin(5, Pin.IN, Pin.PULL_UP)
np = neopixel.NeoPixel(Pin(19), num_leds)

color = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] 
index = 0

while True:
    svetloba = (pot.read() / 4095.0) 
    np[0] = tuple(int(svetloba * c) for c in color[index])
    np.write()
    if not btn.value():
        index = (color_mode + 1) % len(color)
    time.sleep(0.1)
```




Funkcija za potecimoeter
```python
def convert(x, in_min, in_max, out_min, out_max):
    return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min

klic
x = convert(pot.read(), 0, 4096, 0, 16)
```

```python
import neopixel
from machine import Pin
from time import sleep
from random import randrange as r

NUM_LEDS = 16
btnsRed = Pin(35, Pin.IN, Pin.PULL_UP)
btnsGreen = Pin(32, Pin.IN, Pin.PULL_UP)
np = neopixel.NeoPixel(Pin(19), NUM_LEDS)

color = [r(255), r(255), r(255)]

index = 0
while True:
    btnRed = not bool(btnsRed.value())
    btnGreen = not bool(btnsGreen.value())
    if btnRed == False:
        for i in range(NUM_LEDS):
            np[i] = [0, 0, 0]
        np[index] = color
        np.write()
        index += 1
        sleep(0.1)
        if index >= NUM_LEDS:
            index = 15
        
    if btnGreen == True:
        for i in range(NUM_LEDS - 1, -1, -1):
            np[i] = [0,0,0]
        np[i] = color
        index -= 1
        np.write()
        sleep(0.1)
        if index >=NUM_LEDS:
            index = 0
```

```python
from machine import Pin
from time import sleep
import dht 

sensor = dht.DHT22(Pin(19))

while True:
    sleep(2)
    sensor.measure()    # zagon senzorjev ( da lah odcitas sploh kej)
    temp = sensor.temperature()
    hum = sensor.humidity()
    #temp_f = temp * (9/5) + 32.0
    print("temp - ",temp,"°C")
    print("humidity - ",hum,"°C")
    print("-"*20)
```
![image](https://hackmd.io/_uploads/H1_6Kb08a.png)

