#include <Adafruit_NeoPixel.h>
#include <DHT.h>
DHT dht(4, DHT22);
#define PIN 3
int NUMPIXELS = 16;
int predNUM = 0;
bool one = true;
#define CLKpin 7
#define DTpin 6
#define SWpin 5
float h, t, pred_t;
int speed = 50;
int rgb = 0;
int a = 255;
bool hum = false;
int stateCLK = 0;
int lastStateCLK;
unsigned long lastSWtime;
unsigned long lastSWtimeDolg;
int lastSWstate = 0;
bool dolg = false;
bool test = false;
bool zamer = false;
bool error = false;
Adafruit_NeoPixel pixels(16, PIN, NEO_GRB + NEO_KHZ800);
int RGB[3] = {0, 0, 0};
int nol[3] = {0, 0, 0};
void setup() {
pixels.begin();
dht.begin();
pinMode(CLKpin, INPUT);
pinMode(DTpin, INPUT);
pinMode(SWpin, INPUT_PULLUP);
}
void loop() {
but_press();
if(!test) {
update_value();
if(!error) {
stateCLK = digitalRead(CLKpin);
int stateDT = digitalRead(DTpin);
if (stateCLK != lastStateCLK && stateCLK == 1){
if (stateDT != stateCLK) {
a -= 15;
if(a < 0) {
a = 0;
}
RGB[rgb] = a;
write_new_color();
}
else {
a += 15;
if(a > 255) {
a = 255;
}
RGB[rgb] = a;
write_new_color();
}
}
lastStateCLK = stateCLK;
if(t < 0 && !hum) {
write_invert_krug();
}
else {
write_krug();
}
}
}
}
void clearRGB() {
for(int i {0}; i < 3; i++) {
RGB[i] = 0;
}
}
void update_value() {
h = dht.readHumidity();
t = dht.readTemperature();
if(!hum) {
if(t > 0) {
rgb = 0;
clearRGB();
NUMPIXELS = map(t, 0.1, 80, 1, 16);
}
else if(t < 0) {
rgb = 2;
clearRGB();
NUMPIXELS = map(t, -40, -0.1, 0, 15);
if(one) {
predNUM = 16;
one = false;
}
}
}
else {
NUMPIXELS = map(h, 0.1, 100, 1, 16);
}
RGB[rgb] = a;
if ((t == 0 && !hum) || (h == 0 && hum)) {
NUMPIXELS = 0;
}
if (isnan(h) || isnan(t)) {
errorState();
error = true;
return;
}
else {
if(error) {
pixels.clear();
pixels.show();
}
error = false;
}
}
void but_press() {
int butState = digitalRead(SWpin);
if(butState != lastSWstate) {
if(butState == LOW) {
if(millis() - lastSWtime > 50) {
test = false;
hum = !hum;
pixels.clear();
pixels.show();
if(hum) {
rgb = 1;
clearRGB();
predNUM = 0;
NUMPIXELS = map(h, 0.1, 100, 1, 16);
}
else {
if(t > 0) {
predNUM = 0;
}
else {
predNUM = 15;
}
}
}
lastSWtime = millis();
}
}
lastSWstate = butState;
if(butState == LOW) {
if(zamer) {
lastSWtimeDolg = millis();
zamer = false;
}
if(millis() - lastSWtimeDolg > 1000 && dolg) {
test = true;
dolg = false;
moment_write(a, a, a);
}
}
else {
zamer = true;
dolg = true;
}
}
void write_krug() {
int vrNUM = NUMPIXELS;
if(pred_t < 0 && !hum) {
write_plus(predNUM, 16, vrNUM, nol);
predNUM = 0;
}
if(predNUM > vrNUM) {
write_minus(predNUM - 1, vrNUM, vrNUM, nol);
predNUM = NUMPIXELS;
}
if(predNUM != vrNUM) {
write_plus(predNUM - 1, vrNUM, vrNUM, RGB);
predNUM = NUMPIXELS;
}
pred_t = t;
}
void write_invert_krug() {
int vrNUM = NUMPIXELS;
if(pred_t > 0) {
write_minus(predNUM - 1, 0, vrNUM, nol);
predNUM = 15;
}
if(predNUM < vrNUM) {
write_plus(predNUM, vrNUM, vrNUM, nol);
predNUM = NUMPIXELS;
}
if(predNUM != vrNUM) {
write_minus(predNUM, vrNUM, vrNUM, RGB);
predNUM = NUMPIXELS;
}
pred_t = t;
}
void write_minus(int nach, int kon, int vr_num, int massiv[3]) {
for(int i = nach; i >= kon; i--) {
update_value();
if(vr_num != NUMPIXELS) {
predNUM = vr_num;
pred_t = t;
write_krug();
return;
}
pixels.setPixelColor(i, massiv[0], massiv[1], massiv[2]);
pixels.show();
delay(speed);
}
}
void write_plus(int nach, int kon, int vr_num, int massiv[3]) {
for(int i = nach; i < kon; i++) {
update_value();
if(vr_num != NUMPIXELS) {
predNUM = vr_num;
pred_t = t;
write_krug();
return;
}
pixels.setPixelColor(i, massiv[0], massiv[1], massiv[2]);
pixels.show();
delay(speed);
}
}
void write_new_color() {
if(t < 0) {
for(int i = 15; i >= NUMPIXELS; i--) {
pixels.setPixelColor(i, RGB[0], RGB[1], RGB[2]);
pixels.show();
}
}
else {
for(int i = 0; i < NUMPIXELS; i++) {
pixels.setPixelColor(i, RGB[0], RGB[1], RGB[2]);
pixels.show();
}
}
}
void moment_write(int r, int g, int b) {
for(int i = 0; i < 16; i++) {
pixels.setPixelColor(i, r, g, b);
pixels.show();
}
}
void errorState() {
pixels.clear();
pixels.show();
delay(166);
moment_write(255, 255, 0);
delay(167);
}