#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 VCC2 = 8;// define extra VCC for potentiometer
int val; // variable to read the value from the analog pin
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int VCC3 = 4;// define extra VCC for LCD
int degPos;// degree symbol position
void setup() {
//
Serial.begin(9600); // setup serial
myservo.attach(servoPin); // attaches the servo on pin 3 to the servo object
pinMode(VCC2, OUTPUT);// set pin 8 as output
digitalWrite(VCC2, HIGH);// set pin 8 HIGH so it acts as 5V pin
// lines bellow are for LCD
pinMode(VCC3, OUTPUT);// set pin 4 as output
digitalWrite(VCC3, HIGH);// set pin 4 HIGH so it acts as 5V pin
lcd.begin(); // initialize the LCD,
// Turn on the blacklight and print a message.
lcd.backlight();
}
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); // waits for the servo to get there
}