#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define LED_PIN 6
#define LED_COUNT 24
#define BUTTON_UP 2
#define BUTTON_DOWN 3
#define PHOTO_PIN A0
#define SLIDER_PIN 4
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int mode = 1;
int modesCount = 3;
int lastButtonStateUp = HIGH;
int lastButtonStateDown = HIGH;
bool energySaving = false;
bool ledsOn = true;
int lightThreshold = 500;
// Прототипы функций
void updateDisplay();
void checkButtons();
void checkEnergySaving();
void randomPattern();
void clearMatrix();
void setup() {
pinMode(BUTTON_UP, INPUT_PULLUP);
pinMode(BUTTON_DOWN, INPUT_PULLUP);
pinMode(SLIDER_PIN, INPUT_PULLUP);
pinMode(PHOTO_PIN, INPUT);
strip.begin();
strip.show();
strip.setBrightness(50);
lcd.init();
lcd.backlight();
updateDisplay();
randomSeed(analogRead(A5)); // Инициализация генератора случайных чисел
}
void loop() {
checkButtons();
checkEnergySaving();
if(ledsOn) {
switch(mode) {
case 1: randomPattern(); break;
case 2: randomPattern(); break;
case 3: randomPattern(); break;
}
delay(500); // Задержка между сменой изображений
} else {
clearMatrix();
}
}
void updateDisplay() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Mode: ");
lcd.print(mode);
lcd.setCursor(0, 1);
lcd.print("Random Patterns");
}
void checkButtons() {
int currentStateUp = digitalRead(BUTTON_UP);
int currentStateDown = digitalRead(BUTTON_DOWN);
if(currentStateUp == LOW && lastButtonStateUp == HIGH) {
mode = (mode % modesCount) + 1;
updateDisplay();
delay(200);
}
if(currentStateDown == LOW && lastButtonStateDown == HIGH) {
mode = (mode == 1) ? modesCount : mode - 1;
updateDisplay();
delay(200);
}
lastButtonStateUp = currentStateUp;
lastButtonStateDown = currentStateDown;
}
void checkEnergySaving() {
energySaving = !digitalRead(SLIDER_PIN);
if(energySaving) {
int lightLevel = analogRead(PHOTO_PIN);
ledsOn = (lightLevel < lightThreshold);
} else {
ledsOn = true;
}
}
void randomPattern() {
clearMatrix();
// Генерируем случайное изображение
for(int i = 0; i < LED_COUNT; i++) {
if(random(0, 2) == 1) { // 50% вероятность включения светодиода
strip.setPixelColor(i, strip.Color(
random(0, 256), // Красный
random(0, 256), // Зеленый
random(0, 256) // Синий
));
}
}
strip.show();
}
void clearMatrix() {
for(int i = 0; i < LED_COUNT; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
}
strip.show();
}