// приёмник на D2
#define IR_1 12 //0x50
#define IR_2 24 //0x68
#include "NecDecoder.h"
NecDecoder ir;
byte counter;
//=============================
#include <FastLED.h>
#define DATA_PIN 5
#define NUM_LEDS 8
CRGB leds[NUM_LEDS];
#define BUT_PIN 6
#define ARRAY_LEN 64 // Для хранения времен прерываний
uint32_t timeArray[ARRAY_LEN]; // Для хранения времен прерываний
volatile byte arrIndex = 0; // Указатель внутри массива
void setup() {
Serial.begin(115200);
pinMode(BUT_PIN, INPUT_PULLUP);
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS); // GRB ordering is typical
// Показываем, что лента работает
leds[0] = CRGB::Red;
leds[1] = CRGB::Green;
leds[2] = CRGB::Blue;
FastLED.show();
delay(1000);
attachInterrupt(0, irIsr, FALLING);
Serial.println("===START===");
//strip.begin();
//strip.show(); // Initialize all pixels to 'off'
}
void irIsr() {
byte i = arrIndex;
ir.tick();
/*
timeArray[i++] = SP;
if (i >= ARRAY_LEN)
i = 0;
arrIndex = i; // Сохраняем новый индекс
*/
sei(); // Так делать нельзя (т.е. это плохо)
Serial.println(SP);
delay(4);
}
//=====================
void RGBLoop() {
for (int j = 0; j < 3; j++ ) {
// Fade IN
for (int k = 0; k < 256; k++) {
switch (j) {
case 0: setAll(k, 0, 0); break;
case 1: setAll(0, k, 0); break;
case 2: setAll(0, 0, k); break;
}
showStrip();
delay(3);
}
// Fade OUT
for (int k = 255; k >= 0; k--) {
switch (j) {
case 0: setAll(k, 0, 0); break;
case 1: setAll(0, k, 0); break;
case 2: setAll(0, 0, k); break;
}
showStrip();
delay(3);
}
}
}
//=====================
void loop() {
if (digitalRead(BUT_PIN) == LOW) {
Serial.print("Loop millis = "); Serial.println(millis());
RGBLoop();
}
// Для отладочных целей выводим времена срабатывания прерываний
if (timeArray[0] != 0) {
delay(100); // Ждем окончания заполения буфера
for (int i = 0; i < ARRAY_LEN, timeArray[i] != 0; i++) { // Если в буфер что то записалось
Serial.println(timeArray[i]);
timeArray[i] = 0;
}
arrIndex = 0; // Обнуляем индекс
}
if (ir.available()) {
auto codeIR = ir.readCommand();
Serial.print("codeIR = "); Serial.println(codeIR);
switch (codeIR) {
case IR_1: {
Serial.println("Pressed UP");
counter++;
break;
}
case IR_2: {
Serial.println("Pressed DOWN");
counter--;
break;
}
}
}
}
void showStrip() {
//strip.show();
FastLED.show();
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
leds[Pixel] = CRGB(red, green, blue);
// strip.setPixelColor(Pixel, strip.Color(red, green, blue));
}
void setAll(byte red, byte green, byte blue) {
for (int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
//showStrip();
}