#include <IRremote.h>
#include <ESP32Servo.h>
#define RECV_PIN 12
#define SERVO 15
const int ldrPin = 34;
const int relayPin = 19;
IRrecv receiver(RECV_PIN);
decode_results results;
Servo servo;
int angle = 0;
void setup() {
Serial.begin(115200);
servo.attach(SERVO);
servo.write(angle);
receiver.enableIRIn(); // Start the receiver
pinMode(RECV_PIN, INPUT);
pinMode(SERVO, OUTPUT);
pinMode(ldrPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
if (receiver.decode()) {
translateIR();
receiver.resume();
}
int ldrValue = analogRead(ldrPin);
String str = String(ldrValue);
Serial.println("LDR value: " + str);
if (ldrValue < 40) {
Serial.println(" => Dark");
} else if (ldrValue < 800) {
Serial.println(" => Dim");
} else if (ldrValue < 2000) {
Serial.println(" => Light");
} else if (ldrValue < 3200) {
Serial.println(" => Bright");
} else {
Serial.println(" => Very bright");
delay(1000);
}
if (ldrValue < 800) {
digitalWrite(relayPin, HIGH);
Serial.println("Relay ON");
} else {
digitalWrite(relayPin, LOW);
Serial.println("Relay OFF");
delay(1000);
}
}
void translateIR()
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 104:
Serial.println("Menutup jendela");
servo.write(0);
break;
case 48:
Serial.println("Buka 20 derajat");
servo.write(60);
break;
case 24:
Serial.println("Buka 40 derajat");
servo.write(120);
break;
case 122:
Serial.println("Buka 60 derajat");
analogWrite(SERVO, 220);
break;
case 16:
Serial.println("Buka 80 derajat");
analogWrite(SERVO, 240);
break;
case 56:
Serial.println("Buka 100 derajat");
analogWrite(SERVO, 260);
break;
case 90:
Serial.println("Buka 120 derajat");
analogWrite(SERVO, 280);
break;
case 66:
Serial.println("Buka 140 derajat");
analogWrite(SERVO, 300);
break;
case 74:
Serial.println("Buka 160 derajat");
analogWrite(SERVO, 400);
break;
case 82:
Serial.println("Buka 180 derajat");
analogWrite(SERVO, 500);
break;
default:
Serial.println("WARNING: undefined key:");
break;
}
}