#include <Servo.h>
#include <IRremote.h>
// ---------- Пины ----------
const int SERVO_PIN = 9;
const int IR_PIN = 2;
// ---------- Углы ----------
const int ANGLE_OPEN = 10;
const int ANGLE_CLOSE = 90;
// ---------- Коды кнопок вашего пульта ----------
// ВАЖНО: замените на свои! См. раздел "Как узнать коды"
const unsigned long CODE_OPEN = 0x18; // кнопка "открыть"
const unsigned long CODE_CLOSE = 0x5A; // кнопка "закрыть"
const unsigned long CODE_TOGGLE = 0x45; // кнопка "переключить"
Servo claw;
bool clawClosed = false;
void setup() {
Serial.begin(9600);
claw.attach(SERVO_PIN);
claw.write(ANGLE_OPEN);
IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK); // LED_FEEDBACK — мигание встроенным светодиодом
Serial.println("Ready. Press buttons on remote...");
}
void loop() {
if (IrReceiver.decode()) {
unsigned long code = IrReceiver.decodedIRData.command; // сама команда
// unsigned long fullCode = IrReceiver.decodedIRData.decodedRawData; // полный код (для некоторых пультов)
Serial.print("Code: 0x");
Serial.println(code, HEX);
// Игнорируем повторы (кнопка удерживается)
if (!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)) {
handleCommand(code);
}
IrReceiver.resume(); // готовы принимать следующий сигнал
}
}
void handleCommand(unsigned long code) {
if (code == CODE_OPEN) {
claw.write(ANGLE_OPEN);
clawClosed = false;
Serial.println("-> OPEN");
}
else if (code == CODE_CLOSE) {
claw.write(ANGLE_CLOSE);
clawClosed = true;
Serial.println("-> CLOSE");
}
else if (code == CODE_TOGGLE) {
clawClosed = !clawClosed;
claw.write(clawClosed ? ANGLE_CLOSE : ANGLE_OPEN);
Serial.println(clawClosed ? "-> TOGGLE: CLOSE" : "-> TOGGLE: OPEN");
}
}