---
# System prepended metadata

title: UPN -Primeri iz predavanj

---

# UPN -Primeri iz predavanj

[Wokwi main page](https://wokwi.com/)
[New ESP32 MicroPy project](https://wokwi.com/projects/new/micropython-esp32)


## 2 LED blink 
![prva](https://hackmd.io/_uploads/BydqL5Jka.png "Screenshot nalepim kar iz clipboarda")

```python

from machine import Pin 

from utime import sleep 

p18 = Pin(18, Pin.OUT) 

p5 = Pin(5, Pin.OUT) 

d = 0.1 

while True: 

  #prva ledica 

  p18.value(1) 

  sleep(d) 

  p18.value(0) 

  sleep(d) 

  #druga ledica 

  p5.value(1) 

  sleep(d) 

  p5.value(0) 

  sleep(d) 

 
 

# naj LEDice izmenično utripajo. 

 

 
```
## 2 LEDici blink list var 

```python

from machine import Pin 

from utime import sleep 


pList = [5, 18] 

pins = [] 

d = 0.2 

for p in pList: 

  pins.append(Pin(p, Pin.OUT)) 


while True: 

  for p in pins: 

    p.value(1) 

    sleep(d) 

    p.value(0) 

    sleep(d) 

 
 

# naj LEDice utripajo sinhrono (obe naenkrat). 

 ```

 
 
## NeoPixel(WS2812) ring - intro
```python

import neopixel 

from machine import Pin 

from time import sleep 

NUM_LED = 24 


leds = neopixel.NeoPixel(Pin(5), NUM_LED) 


while True: 

  for l in range(NUM_LED): 

    leds[l] = [200, 100, 10] 

    leds.write() 

    sleep(0.2) 


  for l in range(NUM_LED): 

    leds[l] = (0, 0, 0) 

    leds.write() 

    sleep(0.2) 

#prižgi vse lučke z naključno barvo 

 ```
 
 
 
# prižgi lučke v križu in se premikajo v krogu
```python

import neopixel 

from machine import Pin 

from time import sleep 

NUM_LED = 16
sz = [4,8,12,16]

leds = neopixel.NeoPixel(Pin(5), NUM_LED) 

while True: 
  for i in range(NUM_LED):
    leds[i] = [3, 244, 252]
    leds.write()
    sleep(0.5)
    leds[i] = [0,0,0]
    leds.write()
 ```

## 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)
      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 machine import Pin 

from time import sleep 

NUM_LED = 16


leds = neopixel.NeoPixel(Pin(21), NUM_LED) 


while True: 

  for l in range(NUM_LED): 

    if l % 2 == 0:
      leds[l] = (88, 235, 52)
      leds.write()
    else:
      leds[l] = (66, 135, 245)
      leds.write()


  sleep(1)


  for l in range(NUM_LED): 
    leds[l] = (0,0,0)
    leds.write()
  sleep(1)
```

## Button
Button
```python
from machine import Pin

button = Pin(19, Pin.IN, Pin.PULL_UP)


while True:
    btnval = not bool(button.value())
    print(btnval)
```
![](https://hackmd.io/_uploads/SJXY-rxxa.png)


## 3 potentimetri led ring
```python
from machine import Pin, ADC #analog digital converter
import neopixel
from time import sleep

NUM_LED = 16

leds = neopixel.NeoPixel(Pin(18), NUM_LED) 

r= ADC(Pin(26))
g= ADC(Pin(25))
b= ADC(Pin(4))

    

while True: 
    for i in range(NUM_LED):

        leds[i] = (r.read_u16(), g.read_u16(), b.read_u16())

    leds.write()
    sleep(0.5)
```
![](https://hackmd.io/_uploads/B1Gtwrgxa.png)


## Animacija
```python
from machine import Pin,ADC
from time import sleep
import neopixel

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

while True:
    for i in range(num_leds):
        leds[i-1] = [0,0,0]
        leds[i] = [255,0,0]

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

## mutacija barv
```python
from machine import Pin,ADC
from time import sleep
import neopixel
from random import randrange

num_led= 16
pot = ADC(Pin(4))
leds = neopixel.NeoPixel(Pin(26), 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

```

## vaja nalaganje barv
```python
import neopixel 
from machine import Pin 
from time import sleep 
import random

NUM_LED = 16
stop = 0
rgb = [255,0,0]

leds = neopixel.NeoPixel(Pin(5), NUM_LED) 


while True: 
    rgb = (random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
    for i in range(NUM_LED-stop):
        
        if i > 0:
            leds[i-1] = [0,0,0]
            leds[i] = rgb
            leds.write()
        else:
            leds[i] = rgb
            leds.write
        sleep(0.1)
    stop+=1 
    
    if NUM_LED-stop == 0:
        sleep(3)
        stop = 0
```
![](https://hackmd.io/_uploads/Hytr68Yga.png)

## Fukncija Convert
```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

for i in range(1):
    print(i, convert(i, 0, 4096, 0, 16))

```

## vaja
```python
"""import dht
from machine import Pin, PWM
import neopixel
# https://tinyurl.com/wokwiconvert1

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

for i in range(4096):
    print(i, convert(i, 0, 4096, 0, 16))


nl = 16
leds = neopixel.NeoPixel(Pin(18), nl)
redSteps = 255//nl
for i in range(nl):
    leds[i] = [i*redSteps, 255 - (i*redSteps), 0]
leds.write()

# DHT senzor!!
sense = dht.DHT22(Pin(4))

while True:
    sense.measure()
    print(sense.humidity())"""

# z novo osvojenim znanje naredi kar se da lep prikazovalnik temperature in vlage =).
# lahko na enem krogu, lahko na dveh
# torej poljubna implementacija prikazovalnika temp/vlage

import dht
from machine import Pin, PWM
import neopixel
# https://tinyurl.com/wokwiconvert1

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

"""for i in range(4096):
    print(i, convert(i, 0, 4096, 0, 16))"""


nl1 = 16
nl2 = 16
leds1 = neopixel.NeoPixel(Pin(18), nl1)
leds2 = neopixel.NeoPixel(Pin(25), nl2)

"""redSteps = 255//nl
for i in range(nl):
    leds[i] = [i*redSteps, 255 - (i*redSteps), 0]"""


# DHT senzor!!
sense = dht.DHT22(Pin(4))

while True:
    sense.measure()
    hum = sense.humidity()
    h = convert(hum, 0, 100, 0 , 10)

    temp = sense.temperature()
    t = convert(temp, -40, 80, 0, 10)

    leds1.fill([0, 25*int(h), 0])
    leds1.write()
    leds2.fill([25*int(t), 0, 0])
    leds2.write()
```
![](https://hackmd.io/_uploads/r1LLxU-bp.png)

## Lokacija barv
```python
from machine import Pin,ADC
import neopixel

num_leds=16
pot=ADC(Pin(4))
leds = neopixel.NeoPixel(Pin(14), num_leds)

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, 4095, 0, num_leds-1)
    leds.fill([0,0,0])
    leds[x] = [255,192,203]
    leds.write()
```
![](https://hackmd.io/_uploads/SJH6E5zbT.png)


## met kocke
```python
from machine import Pin 
from utime import sleep 
import random

p1 = Pin(19, Pin.OUT) 
p2 = Pin(18, Pin.OUT) 
p3 = Pin(5, Pin.OUT) 
p4 = Pin(21, Pin.OUT) 
p5 = Pin(32, Pin.OUT) 
p6 = Pin(33, Pin.OUT) 
p7 = Pin(25, Pin.OUT) 

button = Pin(35, Pin.IN, Pin.PULL_UP)


stev = random.randrange(1,7)
print(stev)

while True:
    btnval = not bool(button.value())
    print(btnval)
    if btnval == False:
        sleep(0.1)
        if stev == 1:
            p4.value(1)
        
        elif stev == 2:
            p1.value(1)
            p7.value(1)
        
        elif stev == 3:
            p1.value(1)
            p4.value(1)
            p7.value(1)

        elif stev == 4:
            p1.value(1)
            p5.value(1)
            p3.value(1)
            p7.value(1)
        
        elif stev == 5:
            p1.value(1)
            p5.value(1)
            p4.value(1)
            p3.value(1)
            p7.value(1)

        elif stev == 6:
            p1.value(1)
            p5.value(1)
            p2.value(1)
            p3.value(1)
            p7.value(1)

    elif btnval == True:
        sleep(0.1)
        if stev == 1:
            p4.value(0)
        
        elif stev == 2:
            p1.value(0)
            p7.value(0)
        
        elif stev == 3:
            p1.value(0)
            p4.value(0)
            p7.value(0)

        elif stev == 4:
            p1.value(0)
            p5.value(0)
            p3.value(0)
            p7.value(0)
        
        elif stev == 5:
            p1.value(0)
            p5.value(0)
            p4.value(0)
            p3.value(0)
            p7.value(0)

        elif stev == 6:
            p1.value(0)
            p5.value(0)
            p2.value(0)
            p3.value(0)
            p7.value(0)
```
![](https://hackmd.io/_uploads/SJ0W-hIbT.png)


## **Vaja 6**
Za vajo potrebuješ NeoPixel krog in gumb.

Prva lučka se prižge samodejno na zeleno barvo.

Program čaka, da uporabnik pritisne gumb.
Ko uporabnik pritisne gumb, se naslednja ledica v zaporedju pobarva na eno od naslednjih barv.

Če je bil gumb kliknjen po manj kot 1s po prejšne prižgani ledici - ZELENA
Če je bil pritisnjen po 1s in pred 2s - MODRA
po 2s - RDEČA
BONUS

na koncu sortiraj vse LEDice po barva (prvo zelene, modre in nato rdeče)
NAMIG

začni si izpisovati čase med kliki na gumb.
```=
ne pozabi
import time
time.time_ns
```
### resitev
```python=
import machine
import neopixel
import time

NEOPIXEL = machine.Pin(19)
BUTTON = machine.Pin(26, machine.Pin.IN, machine.Pin.PULL_UP)
NUM_PIXELS = 16
np = neopixel.NeoPixel(NEOPIXEL, NUM_PIXELS)

ZELENA = (0, 255, 0)
MODRA = (0, 0, 255)
RDECA = (255, 0, 0)

num_pixel = 1
prejsni_cas = time.ticks_ms()
np[num_pixel-1] = ZELENA
np.write()

while True:
    if BUTTON.value() == 0:
        zdajsni_cas = time.ticks_ms()
        delta_time = zdajsni_cas - prejsni_cas
        if delta_time < 1000:
            np[num_pixel] = ZELENA
        elif delta_time < 2000:
            np[num_pixel] = MODRA
        else:
            np[num_pixel] = RDECA
        np.write()
        num_pixel += 1

        if num_pixel == NUM_PIXELS:

            green_indices = [i for i, x in enumerate(np) if x == ZELENA]
            blue_indices = [i for i, x in enumerate(np) if x == MODRA]
            red_indices = [i for i, x in enumerate(np) if x == RDECA]


            for i in range(len(green_indices)):
                np[i] = ZELENA
            for i in range(len(green_indices), len(green_indices) + len(blue_indices)):
                np[i] = MODRA
            for i in range(len(green_indices) + len(blue_indices), NUM_PIXELS):
                np[i] = RDECA

            np.write()
            num_pixel = 0

        prejsni_cas = zdajsni_cas
        time.sleep(0.2)
```

## vaja 7
```python
from machine import Pin, ADC #analog digital converter
import neopixel
from time import sleep
import random

NUM_LED = 16

leds = neopixel.NeoPixel(Pin(32), NUM_LED) 
leds2 = neopixel.NeoPixel(Pin(27), NUM_LED)
button = Pin(22, Pin.IN, Pin.PULL_UP)


r= ADC(Pin(26))
g= ADC(Pin(25))
b= ADC(Pin(4))



barva = (random.randrange(0,255),random.randrange(0,255),random.randrange(0,255))
leds.fill(barva)
leds.write()


while True:
    btnval = bool(button.value()) 
    print(btnval)
    for i in range(NUM_LED):
        leds2[i] = (r.read_u16(), g.read_u16(), b.read_u16())

    if btnval == False:
        print(f"Red = {barva[0]}, Green = {barva[1]},  Blue = {barva[2]}")
        print(f"Red = {r.read_u16() // 256}, Green = {g.read_u16() // 256},  Blue = {b.read_u16() // 256}")
        break
        
    

    leds2.write()
    sleep(0.5)
```
![](https://hackmd.io/_uploads/rkAnFQtGp.png)
