#include <FastLED.h>
#include "Button.h"
#define LED_PIN 2
#define NUM_LEDS 6
CRGB leds[NUM_LEDS];
CRGB sectionColorOn = CRGB(255,255,255);
CRGB sectionColorOff = CRGB(0,0,0);
// Кнопки
const uint8_t buttonPins[5] = {3, 4, 5, 6, 7};
// Количество светодиодов в каждой секции
const uint8_t sectionSizes[5] = {2, 1, 1, 1, 2};
// Состояние секций (вкл/выкл)
bool sectionState[5] = {true, false, false, true, true};
// Для антидребезга
bool lastButtonState[5];
unsigned long lastDebounceTime[5];
const unsigned long debounceDelay = 50;
unsigned int currentTimeMs = 0;
Button btn1(3, 1);
Button btn2(4, 1);
Button btn3(5, 1);
Button btn4(6, 1);
Button btn5(7, 1);
ISR (TIMER0_COMPA_vect) {
// delay(10);
currentTimeMs++;
btn1.process(currentTimeMs);
btn2.process(currentTimeMs);
btn3.process(currentTimeMs);
btn4.process(currentTimeMs);
btn5.process(currentTimeMs);
}
void setup() {
noInterrupts();
/**** setup timer0 on 1ms (call the function ISR (TIMER0_COMPA_vect)) ****/
TCCR0A |= (1 << WGM01); //сброс при совпадении
OCR0A = 0xF9; //начало отсчета до переполнения (249)
TIMSK0 |= (1 << OCIE0A); //разрешить прерывание при совпадении с регистром А
TCCR0B |= (1 << CS01) | (1 << CS00); //установить делитель частоты на 64
interrupts();
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
for (int i = 0; i < 5; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
lastButtonState[i] = HIGH;
lastDebounceTime[i] = 0;
}
btn1.onClick(processBtn1);
btn2.onClick(processBtn2);
btn3.onClick(processBtn3);
btn4.onClick(processBtn4);
btn5.onClick(processBtn5);
updateSections();
}
void loop() {
}
void updateSections() {
int ledIndex = 0;
for (int s = 0; s < 5; s++) {
for (int i = 0; i < sectionSizes[s]; i++) {
leds[ledIndex] = sectionState[s] ? sectionColorOn : sectionColorOff;
ledIndex++;
}
}
FastLED.show();
// int ledIndex = 0;
// for (int i = 0; i < 5; i++) {
// leds[i] = sectionState[i]? sectionColorOn: sectionColorOff;
// }
// FastLED.show();
}
void processBtn1() {
sectionState[0] = !sectionState[0];
updateSections();
}
void processBtn2() {
sectionState[1] = !sectionState[1];
updateSections();
}
void processBtn3() {
sectionState[2] = !sectionState[2];
updateSections();
}
void processBtn4() {
sectionState[3] = !sectionState[3];
updateSections();
}
void processBtn5() {
sectionState[4] = !sectionState[4];
updateSections();
}
// // =======
// #include <FastLED.h>
// #include "Button.h"
// #include "effects.h"
// #include "ledshelper.h"
// #define NUM_LEDS 100
// #define DATA_PIN 3
// bool (*effects[9])(int, CRGB*, int) = {
// Effects::effectFireYellow2,
// Effects::effectFireYellow,
// Effects::effectSimpleYellow,
// Effects::fireLights,
// Effects::runningLights,
// Effects::randomSlowPock,
// Effects::randomSmooth,
// Effects::colorCircle,
// Effects::randomSnow
// };
// // Define the array of leds
// CRGB leds[NUM_LEDS];
// int currentMode = 0;
// unsigned int currentTimeMs = 0;
// Button modeBtn(12, 50);
// ISR (TIMER0_COMPA_vect) {
// currentTimeMs++;
// modeBtn.process(currentTimeMs);
// if(effects[currentMode](currentTimeMs, leds, NUM_LEDS)) {
// FastLED.show();
// }
// }
// void setup() {
// noInterrupts();
// /**** setup timer0 on 1ms (call the function ISR (TIMER0_COMPA_vect)) ****/
// TCCR0A |= (1 << WGM01); //сброс при совпадении
// OCR0A = 0xF9; //начало отсчета до переполнения (249)
// TIMSK0 |= (1 << OCIE0A); //разрешить прерывание при совпадении с регистром А
// TCCR0B |= (1 << CS01) | (1 << CS00); //установить делитель частоты на 64
// interrupts();
// FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
// pinMode(12, INPUT_PULLUP);
// pinMode(LED_BUILTIN, OUTPUT);
// modeBtn.onClick(processModeBtn);
// for(int i = 0 ; i < NUM_LEDS; i++) {
// leds[i % NUM_LEDS] = color(0 + i*30/4, 0, 30);
// }
// }
// void loop() {}
// void processModeBtn() {
// if(currentMode < (sizeof(effects)/sizeof(effects[0]))-1) {
// currentMode++;
// }else{
// currentMode = 0;
// }
// }