#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define SERVO_PIN 9
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
#define BUTTON_PIN 2
Servo myservo;
LiquidCrystal_I2C lcd(LCD_ADDRESS, LCD_COLUMNS, LCD_ROWS);
bool direction=true;
int angle = 0;
int pos = 0;
void setup()
{
lcd.begin(16, 2);
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP);
myservo.attach(SERVO_PIN);
lcd.setCursor(0,0);
myservo.write(0);
lcd.print("Angle: ");
}
void loop()
{
delay(15);
lcd.setCursor(6,0);
lcd.print(" ");
lcd.setCursor(6,0);
lcd.print(angle);
if(digitalRead(BUTTON_PIN)==LOW)
{
Serial.println("Pressed");
if(angle<180&&direction==true)
{
angle++;
myservo.write(angle);
}
else if(direction == false&&angle>0)
{
angle--;
myservo.write(angle);
}
else
{
direction=!direction;
}
}
}