#include <IRremote.hpp> // Use the latest IRremote library
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int irReceiverPin = 11;
const int servoPin = 9;
const int ledPin = 10;
const unsigned long irCode1 = 0xCF30FF00; // Updated Remote control code 1 (0 degrees)
const unsigned long irCode3 = 0x857AFF00; // Updated Remote control code 3 (180 degrees)
int currentAngle = 90; // Initialize servo angle to 90 degrees (neutral position)
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD display with address 0x27 and 16x2 size
Servo servo; // Servo motor control
IRrecv irrecv(irReceiverPin); // Declare the IR receiver
IRData results; // Variable to store the IR results
void setup() {
Serial.begin(9600);
irrecv.begin(irReceiverPin); // Start the receiver
servo.attach(servoPin); // Attach the servo to the specified pin
servo.write(currentAngle); // Set the initial servo position
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the LCD backlight
pinMode(ledPin, OUTPUT); // Set the LED pin as output
digitalWrite(ledPin, LOW); // Turn off the LED at the start
updateLCD(); // Display the initial angle
}
void loop() {
if (irrecv.decode()) { // Check if an IR signal is received
Serial.println(irrecv.decodedIRData.decodedRawData, HEX); // Print the IR code in HEX
// Perform actions based on the received IR code
if (irrecv.decodedIRData.decodedRawData == irCode1) {
servo.write(0); // Move the servo to 0 degrees
currentAngle = 0;
digitalWrite(ledPin, LOW); // Turn off the LED
delay(500); // Short delay for debounce
} else if (irrecv.decodedIRData.decodedRawData == irCode3) {
servo.write(180); // Move the servo to 180 degrees
currentAngle = 180;
digitalWrite(ledPin, HIGH); // Turn on the LED
delay(500); // Short delay for debounce
}
updateLCD(); // Update the LCD with the new angle
irrecv.resume(); // Resume the receiver for the next signal
}
}
// Function to update the LCD display with the current servo angle
void updateLCD() {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0);
lcd.print("Angle: ");
lcd.print(currentAngle); // Display the current angle
lcd.print(" degrees");
}