#include <Adafruit_NeoPixel.h>
#define PIN_STATUS_LED 33 // STATUS LED - NEOPIXEL
#define NUM_PIXELS 1 // The number of LEDs (pixels) on NeoPixel
#define BUTTON 35
Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_STATUS_LED, NEO_GRB + NEO_KHZ800);
// tlacitko + LED
unsigned long tmStatLed;
unsigned long tmStatButton;
bool mStatButton = false;
bool feedOK1 = false;
bool feedOK2 = false;
bool feedOK1A = false;
bool feedOK2A = false;
bool blink;
bool enStatLed = true; // povol status LED MEM
int statLedHH1 = 50, statLedHH2 = 230; // cas kdy bude svitit status LED (od-do) MEM
bool enFeed1 = true, enFeed2 = true; // povol indikaci krmeni 1, 2 MEM
int feedHH11 = 100, feedHH12 = 130; // cas krmeni 1 (od-do) MEM
int feedHH21 = 170, feedHH22 = 200; // cas krmeni 2 (od-do) MEM
#define RED 0xFF0000
#define GREEN 0x00FF00
#define BLUE 0x0000FF
#define BLACK 0x000000
//----------------------------------------------+----------------------------------------------
unsigned long tmClock;
int hh,mm;
bool almMessage = true;
//----------------------------------------------+----------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
setupStatusLED(); // inicializace WS2816B - stavova LED
}
void loop() {
// put your main code here, to run repeatedly:
statusLED(); // stavova LED - status zarizeni
simul();
}
//----------------------------------------------+----------------------------------------------
void setupStatusLED() {
NeoPixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pinMode(BUTTON, INPUT);
NeoPixel.setBrightness(200); // jas = 0-255
tmStatLed = millis();
}
void statusLED() {
bool button = !digitalRead(BUTTON);
unsigned long color = BLACK;
if (button && !mStatButton) {
tmStatButton = millis();
mStatButton = true;
if (hh >= feedHH11) feedOK1 = true;
if (hh >= feedHH21) feedOK2 = true;
} else if (mStatButton && millis() - tmStatButton > 500) {
mStatButton = false;
} else if (feedOK1 && millis() - tmStatButton > 10000 && !feedOK1A) {
feedOK1A = true;
} else if (feedOK2 && millis() - tmStatButton > 10000 && !feedOK2A) {
feedOK2A = true;
}
if (hh >= statLedHH2) {
feedOK1 = feedOK1A = feedOK2 = feedOK2A = false;
}
if (millis() - tmStatLed >= 1000) {
tmStatLed = millis();
if (hh >= statLedHH1 && hh < statLedHH2 && enStatLed) { // cas kdy bude LED svitit
if (almMessage && blink) {
color = RED; // alarm -> RED
} else if (enFeed1 && !feedOK1 && ((hh >= feedHH11 && hh < feedHH12) || (hh >= feedHH12 && !blink))) {
color = BLUE; // nakrmit -> BLUE
} else if (enFeed2 && !feedOK2 && ((hh >= feedHH21 && hh < feedHH22) || (hh >= feedHH22 && !blink))) {
color = BLUE; // nakrmit -> BLUE
} else if ((enFeed1 && hh < feedHH12 && feedOK1 && !feedOK1A) || (enFeed2 && hh < feedHH22 && feedOK2 && !feedOK2A)) {
color = GREEN; // nakrmeno -> GREEN
}
}
NeoPixel.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called
NeoPixel.fill(color); // set color to red -> it only takes effect if pixels.show() is called
NeoPixel.show(); // update to the NeoPixel Led
blink = !blink;
//Serial.println("Button:" + String(button));
Serial.println("Time : " + String(hh) + " ; color : " + String(color));
}
}
void simul() {
if (millis() - tmClock > 100) {
tmClock = millis();
hh++;
if (hh>239)hh=0;
}
}