#define DECODE_NEC // DECODE_NEC
#include <IRremote.hpp> // Do not change header order.
class FanTimer {
public:
void start() {
timeStamp = millis();
}
bool operator()(const unsigned long duration) {
return (millis() - timeStamp >= duration) ? true : false;
}
private:
unsigned long timeStamp{0};
};
FanTimer fanTimer;
constexpr uint32_t FAN_ACTIVE_DURATION_MS {5000}; // 5 Sekunden
constexpr uint8_t RECV_PIN {2};
constexpr uint8_t LED {12};
enum class Fan : uint8_t {OFF, ON} state;
//const uint32_t s1 = 1082110031;
const uint16_t s1 = 0x30; // Taste 1
const uint16_t s2 = 0x18; // Taste 2
void setup()
{
Serial.begin(9600);
pinMode (LED, OUTPUT); // Lüfter
digitalWrite (LED, LOW);
// digitalWrite(5, HIGH);
// digitalWrite(4, HIGH);
IrReceiver.begin(RECV_PIN);
Serial.print(F("Ready to receive IR signals at pin "));
Serial.println(RECV_PIN);
}
void loop()
{
static uint16_t irSwitch;
irSwitch = irReceive();
if (fanTimer(FAN_ACTIVE_DURATION_MS)) {
digitalWrite (LED, LOW);
state = Fan::OFF;
}
switch(irSwitch) {
case s1:
if (state == Fan::OFF) {
digitalWrite (LED, HIGH);
state = Fan::ON;
fanTimer.start();
}
break;
case s2:
state = Fan::OFF;
digitalWrite (LED, LOW);
break;
}
}
uint16_t irReceive() {
uint16_t received{0};
if (IrReceiver.decode()) {
IrReceiver.printIRResultShort(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
// We have an unknown protocol here, print more info
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
if (IrReceiver.decodedIRData.protocol == NEC) {
received = IrReceiver.decodedIRData.command;
Serial.print("Command: 0x");
Serial.println(received, HEX);
}
IrReceiver.resume();
}
return received;
}