#include <LiquidCrystal.h> // Include the LiquidCrystal library for LCD control
#include <Servo.h> // Include the Servo library to control the servo motor
Servo myservo; // Create a Servo object to control a servo motor
// Define the Arduino pins connected to the LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // Initialize the LiquidCrystal object with the pin numbers
const int potpin = A0; // Define the analog pin connected to the potentiometer
int val = 0; // Variable to store the potentiometer reading
int lastVal = -1; // Variable to store the last potentiometer value (used to detect changes)
void setup() {
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.setCursor(0, 0); // Set the cursor to the first column of the first row
lcd.print("hello, world!"); // Print "hello, world!" on the LCD
myservo.attach(6); // Attach the servo motor to pin 6
}
void loop() {
val = analogRead(potpin); // Read the analog value from the potentiometer
val = map(val, 0, 1023, 0, 180); // Map the potentiometer value to a range of 0 to 180
// Only update the LCD if the potentiometer value has changed
if (val != lastVal) {
lastVal = val; // Update the lastVal to the current value
myservo.write(val); // Move the servo to the position corresponding to the potentiometer value
lcd.setCursor(0, 1); // Set the cursor to the first column of the second row
lcd.print(val); // Print the current potentiometer value on the LCD
lcd.setCursor(9, 1); // Set the cursor to the 10th column of the second row
lcd.print(" "); // Clear any previous status message (e.g., "closed" or "opened")
// Display a status message based on the potentiometer value
if (val == 0) {
lcd.setCursor(9, 1); // Set the cursor to the 10th column of the second row
lcd.print("closed"); // If the value is 0, print "closed"
} else if (val == 180) {
lcd.setCursor(9, 1); // Set the cursor to the 10th column of the second row
lcd.print("opened"); // If the value is 180, print "opened"
}
}
delay(100); // Short delay to stabilize the reading and avoid rapid changes
}