#include <IRremote.hpp> // Library for IR remote control
#include <LiquidCrystal.h> // Library for controlling LCD display
#include <Servo.h> // Library for servo motor control
// Pin Definitions
#define IR_RECEIVE_PIN 2 // Pin for the IR receiver module
#define TEMP_PIN A5 // Pin for temperature sensor (analog input)
#define PHOTO_PIN A0 // Pin for photoresistor (light sensor)
#define SERVO_PIN 3 // Pin for the first servo motor
#define SERVO_PIN2 10 // Pin for the second servo motor
#define PIEZO_PIN 4 // Pin for the piezo buzzer
#define potPin A4 // Pin for the potentiometer (analog input)
#define LED_PIN 13 // Pin for the built-in LED
// Global Variables
int potVal; // Stores potentiometer value
int angle; // Stores servo angle
bool systemEnabled = false; // System status: ON/OFF
// LCD Initialization (RS, E, D4, D5, D6, D7 pins)
LiquidCrystal lcd(9, 8, 7, 6, 5, 4);
Servo myServo; // Servo motor object for first servo
Servo myServo2; // Servo motor object for second servo
// Function Prototypes
void initializeReceiver(); // Initialize the IR receiver
void initializeLCD(); // Initialize the LCD
void lcdPrint(char* text); // Display text on the LCD
void translateIR(); // Handle IR remote inputs
int mapPhotoresistorToServo(int); // Map light sensor value to servo angle
float readTemperature(); // Read temperature sensor value
void setup() {
// Initialize Components
initializeReceiver(); // Setup IR receiver
initializeLCD(); // Setup LCD display
pinMode(TEMP_PIN, INPUT); // Set TEMP_PIN as input
pinMode(LED_PIN, OUTPUT); // Set LED_PIN as output
digitalWrite(LED_PIN, LOW); // Ensure LED is off initially
myServo.attach(SERVO_PIN); // Attach first servo to SERVO_PIN
myServo2.attach(SERVO_PIN2); // Attach second servo to SERVO_PIN2
Serial.begin(9600); // Start serial communication for debugging
lcd.setCursor(0, 0); // Set cursor at first line, first column
lcd.print("System Ready..."); // Print system ready message
delay(2000); // Wait 2 seconds
lcd.clear(); // Clear the LCD display
}
void loop() {
// Check if IR input is received
if (IrReceiver.decode()) {
translateIR(); // Decode and respond to IR command
IrReceiver.resume(); // Resume IR receiver for next input
}
// Run sensors and logic when the system is enabled
if (systemEnabled) {
float temperature = readTemperature(); // Read temperature
int lightLevel = analogRead(PHOTO_PIN); // Read light level from photoresistor
potVal = analogRead(potPin); // Read potentiometer value
// Map potentiometer value to servo angle (0 to 179 degrees)
angle = map(potVal, 0, 1023, 0, 179);
myServo2.write(angle); // Move second servo to calculated angle
// Map light sensor value to servo angle (0 to 90 degrees)
int servoAngle = mapPhotoresistorToServo(lightLevel);
myServo.write(servoAngle); // Move first servo
// Display temperature and light level on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C ");
lcd.setCursor(0, 1);
lcd.print("Light: ");
lcd.print(lightLevel);
lcd.print(" ");
}
}
// Map photoresistor value (0-1023) to servo angle (0-90 degrees)
int mapPhotoresistorToServo(int lightValue) {
int servoAngle = map(lightValue, 0, 1023, 0, 90);
return constrain(servoAngle, 0, 90); // Ensure servo angle is within 0-90 range
}
// Read temperature from TMP36 sensor
float readTemperature() {
int sensorValue = analogRead(TEMP_PIN); // Read analog value from temperature sensor
float voltage = sensorValue * 5.0 / 1023.0; // Convert analog value to voltage
float temperature = (voltage - 0.5) * 100; // Convert voltage to temperature in Celsius
return temperature;
}
// Initialize IR receiver
void initializeReceiver() {
IrReceiver.begin(IR_RECEIVE_PIN); // Start the IR receiver on defined pin
}
// Initialize the LCD
void initializeLCD() {
lcd.begin(16, 2); // Set up the LCD with 16 columns and 2 rows
lcd.print("<press a button>"); // Display initial message
}
// Print text on the LCD
void lcdPrint(char* text) {
lcd.clear(); // Clear the LCD screen
lcd.setCursor(0, 0); // Set cursor at first line
lcd.print("Button: "); // Print Button label
lcd.setCursor(0, 1); // Set cursor at second line
lcd.print(text); // Print received text
}
// Handle IR remote input and commands
void translateIR() {
// Turn on LED and print code to Serial Monitor
digitalWrite(LED_PIN, HIGH); // Turn on built-in LED
Serial.print("Button Code: ");
Serial.println(IrReceiver.decodedIRData.command);
switch (IrReceiver.decodedIRData.command) {
case 162: // POWER Button
systemEnabled = !systemEnabled; // Toggle system state ON/OFF
lcdPrint(systemEnabled ? "System ON" : "System OFF");
break;
case 226: lcdPrint("MENU"); break;
case 34: lcdPrint("TEST"); break;
case 2: lcdPrint("PLUS"); break;
case 194: lcdPrint("BACK"); break;
case 224: lcdPrint("PREV."); break;
case 168: lcdPrint("PLAY"); break;
case 144: lcdPrint("NEXT"); break;
case 104: lcdPrint("num: 0"); break;
case 152: lcdPrint("MINUS"); break;
case 176: lcdPrint("key: C"); break;
case 48: lcdPrint("num: 1"); break;
case 24: lcdPrint("num: 2"); break;
case 122: lcdPrint("num: 3"); break;
case 16: lcdPrint("num: 4"); break;
case 56: lcdPrint("num: 5"); break;
case 90: lcdPrint("num: 6"); break;
case 66: lcdPrint("num: 7"); break;
case 74: lcdPrint("num: 8"); break;
case 82: lcdPrint("num: 9"); break;
default: // Unknown Command
lcd.clear();
lcd.print("Unknown: ");
lcd.print(IrReceiver.decodedIRData.command);
}
delay(200); // Keep LED on briefly
digitalWrite(LED_PIN, LOW); // Turn off LED
}