#include <FastLED.h>
/* ================= KONFIGURACE ================= */
#define LED_PIN 6
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define BRIGHTNESS 150
#define NUM_LEDS 209
CRGB leds[NUM_LEDS];
// ===== Segmenty =====
#define SEG1_START 0
#define SEG1_LEN 58
#define SEG2_START 58
#define SEG2_LEN 58
#define SEG5_START 58 // prvních 7 LED segmentu 2
#define SEG5_LEN 7
#define SEG3_START 116
#define SEG3_LEN 50
#define SEG4_START 166
#define SEG4_LEN 27
// ===== Vstupy =====
#define RELAY1_PIN 2
#define RELAY2_PIN 3
#define RELAY3_PIN 4
#define POT_PIN A0
// ===== Barvy =====
const CRGB BASE_BLUE = CRGB(0, 0, 128); // 50 %
const CRGB FLOW_COLOR = CRGB(0, 255, 200);
// ===== Bar-graf kalibrace =====
#define BAR_START_ADC 200
#define BAR_END_ADC 850
#define BAR_STEP_LEDS 1
/* ================= PROMĚNNÉ ================= */
uint16_t flowPos = 0;
enum SegState { OFF, FADING_IN, RUNNING, FADING_OUT };
// Segmenty 1 a 2
SegState seg1State = OFF;
SegState seg2State = OFF;
uint16_t seg1Pos = 0;
uint16_t seg2Pos = 0;
// Režim 3 (SEG5 + SEG3)
SegState mode3State = OFF;
uint16_t mode3Pos = 0;
/* ================= SETUP ================= */
void setup() {
FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
pinMode(RELAY1_PIN, INPUT_PULLUP);
pinMode(RELAY2_PIN, INPUT_PULLUP);
pinMode(RELAY3_PIN, INPUT_PULLUP);
FastLED.clear();
FastLED.show();
}
/* ================= LOOP ================= */
void loop() {
FastLED.clear();
// Segment 1
handleSegment(digitalRead(RELAY1_PIN) == LOW,
SEG1_START, SEG1_LEN,
seg1State, seg1Pos,
true, true);
// Segment 2
handleSegment(digitalRead(RELAY2_PIN) == LOW,
SEG2_START, SEG2_LEN,
seg2State, seg2Pos,
true, false);
// Režim 3 – kombinovaný segment
handleMode3(digitalRead(RELAY3_PIN) == LOW);
// Bar-graf
barGraph();
FastLED.show();
flowPos++;
delay(30);
}
/* ================= FUNKCE ================= */
// ---------- Obsluha segmentu 1 / 2 ----------
void handleSegment(bool enabled,
uint16_t start,
uint16_t length,
SegState &state,
uint16_t &pos,
bool flowForward,
bool fadeForward) {
if (enabled && state == OFF) {
state = FADING_IN;
pos = 0;
}
if (!enabled && state == RUNNING) {
state = FADING_OUT;
pos = length;
}
switch (state) {
case FADING_IN:
for (uint16_t i = 0; i < pos; i++) {
int idx = flowForward ? i : (length - 1 - i);
leds[start + idx] = BASE_BLUE;
}
pos++;
if (pos >= length) state = RUNNING;
break;
case RUNNING:
for (uint16_t i = 0; i < length; i++)
leds[start + i] = BASE_BLUE;
for (uint8_t i = 0; i < 5; i++) {
int p = (flowPos + i * 4) % length;
if (!flowForward) p = length - 1 - p;
leds[start + p] = FLOW_COLOR;
}
break;
case FADING_OUT:
for (uint16_t i = 0; i < pos; i++) {
int idx = fadeForward ? i : (length - 1 - i);
leds[start + idx] = BASE_BLUE;
}
pos--;
if (pos == 0) state = OFF;
break;
default:
break;
}
}
// ---------- Režim 3: SEG5 + SEG3 ----------
void handleMode3(bool enabled) {
const uint16_t totalLen = SEG5_LEN + SEG3_LEN;
if (enabled && mode3State == OFF) {
mode3State = FADING_IN;
mode3Pos = 0;
}
if (!enabled && mode3State == RUNNING) {
mode3State = FADING_OUT;
mode3Pos = totalLen;
}
switch (mode3State) {
case FADING_IN:
for (uint16_t i = 0; i < mode3Pos; i++) {
drawMode3BasePixel(i);
}
mode3Pos++;
if (mode3Pos >= totalLen) mode3State = RUNNING;
break;
case RUNNING:
drawMode3Base();
drawMode3Flow();
break;
case FADING_OUT:
for (uint16_t i = 0; i < mode3Pos; i++) {
drawMode3BasePixel(i);
}
mode3Pos--;
if (mode3Pos == 0) mode3State = OFF;
break;
default:
break;
}
}
// ---------- Podklad režimu 3 ----------
void drawMode3Base() {
for (int i = 0; i < SEG5_LEN; i++)
leds[SEG5_START + i] = BASE_BLUE;
for (int i = 0; i < SEG3_LEN; i++)
leds[SEG3_START + i] = BASE_BLUE;
}
void drawMode3BasePixel(uint16_t p) {
if (p < SEG5_LEN) {
leds[SEG5_START + p] = BASE_BLUE;
} else {
// INVERTOVANÝ směr na SEG3
uint16_t idx = SEG3_LEN - 1 - (p - SEG5_LEN);
leds[SEG3_START + idx] = BASE_BLUE;
}
}
// ---------- Animace režimu 3 ----------
void drawMode3Flow() {
const uint16_t totalLen = SEG5_LEN + SEG3_LEN;
for (uint8_t i = 0; i < 5; i++) {
uint16_t p = (flowPos + i * 4) % totalLen;
if (p < SEG5_LEN) {
leds[SEG5_START + p] = FLOW_COLOR;
} else {
uint16_t idx = SEG3_LEN - 1 - (p - SEG5_LEN);
leds[SEG3_START + idx] = FLOW_COLOR;
}
}
}
// ---------- Bar-graf ----------
void barGraph() {
int adc = analogRead(POT_PIN);
if (adc < BAR_START_ADC) return;
if (adc > BAR_END_ADC) adc = BAR_END_ADC;
int range = BAR_END_ADC - BAR_START_ADC;
int value = adc - BAR_START_ADC;
int ledsOn = map(value, 0, range, 0, SEG4_LEN / BAR_STEP_LEDS);
ledsOn *= BAR_STEP_LEDS;
if (ledsOn > SEG4_LEN) ledsOn = SEG4_LEN;
for (int i = 0; i < ledsOn; i++) {
leds[SEG4_START + (SEG4_LEN - 1 - i)] = CRGB(0, 255, 0);
}
}