#include <LiquidCrystal_I2C.h> // include the library for the LCD screen
#include <IRremote.h> // include the library for IR remote and receiver
#include <Servo.h> // Include Servo library
/*
Zainab Khan
PuttingitallTogether.ino
Thursday, April 10, 2025
This program uses an IR remote to turn a servo in increments of 15 degrees
it also has a Ultra Sonic Distance sensor that displays a different message
on an lcd screen depending on how far away an object is
*/
LiquidCrystal_I2C lcd (0x27, 16, 2); // I2C address is 0x27, 16 columns, 2 rows
int trigPin = 3; // trigPin is connected to pin 3, sends out signal
int echoPin = 2; // echoPin is connected to pin 2, receives signal
int servoPin = 9; // servo moter is connected to pin 9
Servo myServo; // Create a servo object to control the servo
int receiverPin = 12; // receiver is connected to pin 12
int commandValue = 0; // a variable to hold the IR command number of the remote
IRrecv receiver(receiverPin); // creating a receiver object that is connected to receiverPin (pin 12)
int angle = 0; // variable to hold the angle of the servo
void setup() {
pinMode(trigPin, OUTPUT); // setting trigPin as output
pinMode(echoPin, INPUT); // setting echoPin as input
lcd.init(); //initialize the lcd screen
lcd.backlight(); // turn on the backlight
myServo.attach(servoPin); // Attach the servo motor to pin 9
receiver.enableIRIn(); // enables IR receiver
Serial.begin(9600);
} // end of void setup
void loop() {
if (receiver.decode()){ // if remote is used
translateIR(); // call translateIR function
receiver.resume(); // read for signals again
} // end of if
// start a new measurment
digitalWrite(trigPin, HIGH); // turns on sensor
delayMicroseconds(10); // delay in microseconds
digitalWrite(trigPin, LOW); //turns off sensor
int duration = pulseIn(echoPin, HIGH); // measuring the time it takes for the signal to return
int centimeter = duration/58; // converts duration reading to centimeters
lcd.setCursor (0,1); // move cursor to first column on bottom row
lcd.print(centimeter);
lcd.print(" cm away ");
delay(1000); // delay of 1 second
if (centimeter < 50){ // checks if centimeter is less than 50
lcd.setCursor (0,0); // move cursor to first column on top row
lcd.print("You're close!");
lcd.print(" ");
} // end of if
else if (centimeter < 100){ // checks if centimeter is less than 100
lcd.setCursor (0,0); // move cursor to first column on top row
lcd.print("Medium distance");
lcd.print(" ");
} // end of else if
else { // checks if centimeter is any number that isnt less than 100 and less than 50 (any number greater than 100)
lcd.setCursor (0,0); // move cursor to first column on top row
lcd.print("More than 1m");
lcd.print(" ");
} // end of else
} // end of void loop
int translateIR(){ // function for IR inputs and outputs
commandValue = receiver.decodedIRData.command; // decode and store the command value
if(commandValue == 144){ // checking if commandValue is equal to 144
angle = angle + 15; // update angle to equal angle + 15
myServo.write(angle); // Servo moves according to ubdated angle
delay(15); // delay of 15 milliseconds
} // end of if for forward
else if(commandValue == 224){ // checking if commandValue is equal to 224
angle = angle - 15; // update angle to equal angle - 15
myServo.write(angle); // Servo moves according to ubdated angle
delay(15); // delay of 15 milliseconds
} // end of if for backward
if(angle == 195){ // checking if angle is equal to 195
angle = 180; // update angle to 180
myServo.write(angle); // Servo moves according to ubdated angle
delay(15); // delay of 15 milliseconds
} // end of if for angle greater than 180
if (angle == -15){ // checking if angle is equal to -15
angle = 0; // update angle to 0
myServo.write(angle); // Servo moves according to ubdated angle
delay(15); // delay of 15 milliseconds
} // end of if for angle less than 0
} // end of function translateIR