#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int deg;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello, Moonboot!");
lcd.setCursor(2,1);
lcd.print("Welcome to my");
delay(2000);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Project");
delay(2000);
lcd.clear();
lcd.setCursor(2,1);
lcd.print("Have fun!");
delay(2000);
lcd.clear();
lcd.print("The Angle is:");
}
void loop() {
lcd.setCursor(0,1);
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 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
deg = 180 - val;
if (deg < 100) {
lcd.print("0");
}
if (deg < 10) {
lcd.print("0");
}
lcd.print(deg);
delay(15); // waits for the servo to get there
}