// ESP32 + IR Alıcı + Kumanda + 3 LED + Buzzer + OLED
// IR kumanda ile LED kontrolü + OLED'de durum gösterimi
// Serial'da tam decode sonucu → yeni buton eklemek kolay
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <IRremote.hpp> // IRremote v4+
#define IR_RECV_PIN 5
#define LED_GREEN 18
#define LED_YELLOW 4
#define LED_RED 15
#define BUZZER_PIN 14
#define SCREEN_W 128
#define SCREEN_H 64
#define OLED_ADDR 0x3C
Adafruit_SSD1306 oled(SCREEN_W, SCREEN_H, &Wire, -1);
String lastKey = "---";
String lastAction = "Bekliyor";
uint32_t pressCount = 0;
bool ledG = false, ledY = false, ledR = false;
// IR debounce
uint16_t lastCmd = 0;
unsigned long lastCmdMs = 0;
// ============ Helpers ============
void lcdAt(int x, int y, uint8_t size, const String &t) {
oled.setTextSize(size);
oled.setCursor(x, y);
oled.print(t);
}
void centered(uint8_t y, uint8_t size, const String &t) {
int w = t.length() * 6 * size;
int x = (128 - w) / 2;
if (x < 0) x = 0;
lcdAt(x, y, size, t);
}
void beepShort() {
digitalWrite(BUZZER_PIN, HIGH);
delay(50);
digitalWrite(BUZZER_PIN, LOW);
}
// ============ Ekranlar ============
void drawSplash() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
centered(10, 2, "BlueGrays");
centered(40, 1, "IR Remote v1.0");
oled.drawRoundRect(2, 2, 124, 60, 6, SSD1306_WHITE);
oled.display();
}
void drawStatus() {
oled.clearDisplay();
oled.setTextColor(SSD1306_WHITE);
lcdAt(0, 0, 1, "> IR Remote");
oled.drawLine(0, 10, 127, 10, SSD1306_WHITE);
// Merkez: büyük son tuş
centered(14, 3, lastKey);
// Alt açıklama
centered(44, 1, lastAction);
oled.drawLine(0, 53, 127, 53, SSD1306_WHITE);
// Alt bilgi: LED durumu + toplam basım
String leds = String(ledG ? "G" : "-") + String(ledY ? "Y" : "-") + String(ledR ? "R" : "-");
lcdAt(0, 55, 1, "LED:" + leds);
lcdAt(72, 55, 1, "Say:" + String(pressCount));
oled.display();
}
// ============ Buton İşleme ============
void handleCommand(uint16_t cmd, uint32_t rawCode) {
pressCount++;
switch (cmd) {
// PWR → tüm LED söner (master off)
case 0xA2:
case 0x45: lastKey = "PWR";
ledG = ledY = ledR = false;
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_RED, LOW);
lastAction = "Hepsi kapali"; break;
// 1 → Yeşil LED toggle
case 0x30:
case 0x16: lastKey = "1";
ledG = !ledG; digitalWrite(LED_GREEN, ledG);
lastAction = ledG ? "Yesil ACIK" : "Yesil KAPALI"; break;
// 2 → Sarı LED toggle
case 0x18:
case 0x19: lastKey = "2";
ledY = !ledY; digitalWrite(LED_YELLOW, ledY);
lastAction = ledY ? "Sari ACIK" : "Sari KAPALI"; break;
// 3 → Kırmızı LED toggle
case 0x7A:
case 0x0D: lastKey = "3";
ledR = !ledR; digitalWrite(LED_RED, ledR);
lastAction = ledR ? "Kirmizi ACIK" : "Kirmizi KAPALI"; break;
// 4-9 → OLED'de özel gösterim
case 0x10:
case 0x0C: lastKey = "4"; lastAction = "Ozel: 4"; break;
case 0x38: lastKey = "5"; lastAction = "Ozel: 5"; break;
case 0x09:
case 0x5E: lastKey = "6"; lastAction = "Ozel: 6"; break;
case 0x42:
case 0x08: lastKey = "7"; lastAction = "Ozel: 7"; break;
case 0x4A:
case 0x1C: lastKey = "8"; lastAction = "Ozel: 8"; break;
case 0x52:
case 0x5A: lastKey = "9"; lastAction = "Ozel: 9"; break;
// Volume
case 0x02:
case 0x46: lastKey = "V+"; lastAction = "Ses arttir"; break;
case 0x98:
case 0x44: lastKey = "V-"; lastAction = "Ses azalt"; break;
// Bilinmeyen buton → Serial'da kodunu gör
default:
lastKey = "?";
lastAction = "Kod: 0x" + String(cmd, HEX);
break;
}
Serial.printf("[IR] cmd=0x%02X raw=0x%08lX -> %s (%s)\n",
cmd, (unsigned long)rawCode, lastKey.c_str(), lastAction.c_str());
beepShort();
drawStatus();
}
// ============ SETUP ============
void setup() {
Serial.begin(115200);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_YELLOW, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
Wire.begin();
if (!oled.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
while (1);
}
drawSplash();
delay(2500);
IrReceiver.begin(IR_RECV_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR Alici hazir, kumanda ile tetikleyin");
drawStatus();
}
// ============ LOOP ============
void loop() {
if (IrReceiver.decode()) {
uint16_t cmd = IrReceiver.decodedIRData.command;
uint32_t raw = IrReceiver.decodedIRData.decodedRawData;
// Repeat frame'leri atla + debounce (aynı komut 300ms içinde tekrar gelmesin)
bool isRepeat = (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT);
unsigned long now = millis();
if (raw != 0 && !isRepeat && IrReceiver.decodedIRData.protocol != UNKNOWN) {
if (cmd != lastCmd || (now - lastCmdMs > 300)) {
handleCommand(cmd, raw);
lastCmd = cmd;
lastCmdMs = now;
}
}
IrReceiver.resume();
}
delay(50);
}
Loading
ssd1306
ssd1306