#include <Wire.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = A0; // analog pin used to connect the potentiometer
int servoPin = 9; // define a digital pin (with PWM) for servo. Can use only pin 3, 5, 6,9, 10, 11
int val; // variable to read the value from the analog pin
int degPos;// degree symbol position
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);// Define I2C with LCD Module
void setup() {
Serial.begin(9600);
lcd.begin(16, 2); // initialize the LCD,
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
}
void loop() {
val = analogRead(potpin)/5; // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023/5, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
Serial.print(val);
Serial.println();
lcd.clear();
lcd.print("Servo with Pot.");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print("Angle:");
lcd.setCursor (7,1); // go to 7 char of of 2nd line
lcd.print(val);// print angle
if(val >99)
{
degPos =10;
}else if(val >9)
{
degPos =9;
}else
{
degPos =8;
}
lcd.setCursor (degPos,1); // go to 11 char of of 2nd line
lcd.print((char)223);// degree symbol
delay(500);
}