#include <LiquidCrystal_I2C.h>
#include <ESP32Servo.h>// for wokwi only, comment this line when uploading to kit
#define SERVO_PIN 26 // ESP32 pin GIOP26 connected to servo motor's pin
Servo servo; // create servo object to control a servo
int angle = 0; // the current angle of servo motor
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// Potentiometer is connected to GPIO 34 (Analog ADC1_CH6)
const int potPin = 34;
// variable for storing the potentiometer value
int potValue = 0;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
//define sound speed in cm/uS
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
void setup(){
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
Serial.begin(115200); // Starts the serial communication
delay(1000);
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
}
void loop(){
potValue = analogRead(potPin);
Serial.print("ADC Value :");
Serial.println(potValue);
// set cursor to first column, first row
lcd.setCursor(0, 0);
// print message
lcd.print("ADC Value: ");
lcd.setCursor(0,1);
lcd.print(potValue);
lcd.print("");
delay(1000);
lcd.clear();
potValue=map(potValue, 0, 4095, 0, 180); // Map the values from 0 to 180 degrees
servo.write(potValue); // Write the angle to the servo
delay(15); // Delay of 15ms to allow servo to reach position
servo.write(potValue);
Serial.print("Pot Value :");
Serial.print(potValue);
Serial.println("°");
lcd.print("Servo Angle Val: ");
lcd.setCursor(0,1);
lcd.print(potValue);
lcd.print("");
delay(1000);
lcd.clear();
}