from machine import Pin, ADC
from time import sleep, time
from neopixel import NeoPixel
import random
import dht
import time, utime
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
l = 16
NUM_LEDS=30
led = NeoPixel(Pin(5),l)
pixels1 = NeoPixel(Pin(4),NUM_LEDS)
gumb = Pin(14,Pin.IN,Pin.PULL_UP)
gumb2 = Pin(16,Pin.IN,Pin.PULL_UP)
pot = ADC(Pin(27,Pin.IN))
pot1 = ADC(Pin(12,Pin.IN))
pot_hitrost = ADC(Pin(14,Pin.IN))
sense = dht.DHT22(Pin(2))
trenutni_cas = time.time()
prejsnje = convert(pot_hitrost.read(), 0, 4095, 0.5, 2.5) / 10
cas = trenutni_cas - 2
while True:
if gumb2.value() == 0:
start = time.time()
for i in range(5):
st=0
a = random.randint(1, NUM_LEDS)
pixels1.fill([0,0,0])
pixels1[a] = [0,250,0]
pixels1.write()
poten = convert(pot1.read(),0,4096,0,NUM_LEDS)
time1 = 0
time2 = 0
while (time2-time1) <= 0.5:
time1 = time.time()
st += 1
a = random.randint(1, 30)
while poten != a:
pixels1[poten] = [250,0,0]
pixels1.write()
print(poten)
poten = convert(pot1.read(),0,4096,0,NUM_LEDS)
pixels1.fill([0,0,0])
pixels1[a] = [0,250,0]
pixels1.write()
time2 = time.time()
finish = time.time()
print("Čas: ", end ="")
print(finish-start,end="")
print("s")
print("Število ponovitev", end="")
print(st)
led.fill([0,0,0])
barva = [random.randint(100,255),random.randint(100,255),random.randint(100,255)]
for i in range(l):
sleep_time = convert(pot_hitrost.read(),0,4095,0.5,2.5)/10
if prejsnje == sleep_time and ((time.time() - cas) >= 60):
led.fill([0, 0, 0])
break
else:
cas = time.time() - 2
# utrip in brisanje prejsnje ledice
led[i] = barva
led.write()
sleep(sleep_time)
led[i] = [0,0,0]
led.write()
sleep(sleep_time)
led[i] = barva
led.write()
#sprememba barve
for i in range(3):
sense.measure()
temp_vrednost = convert(int(sense.temperature()),-40,80,0,7)
vlaznost_vrednost = convert(int(sense.humidity()),0,100,0,7)
offset = temp_vrednost + vlaznost_vrednost
if offset < barva[i]:
barva[i] -= offset
else:
barva[i] += offset
led.fill([0,0,0])