#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#define pinBuzzer 7
#define pinServo 9
#define pinPot A0
Servo myServo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int val;
//karakter derajat pada LCD
byte derajat[] = {
B00110,
B01001,
B01001,
B00110,
B00000,
B00000,
B00000,
B00000
};
void setup() {
pinMode(pinBuzzer, OUTPUT);
Serial.begin(115200);
myServo.attach(pinServo);
lcd.init();
lcd.backlight();
lcd.createChar(0, derajat);
}
void suaraBeep() {
tone(pinBuzzer, 120);
delay(500);
noTone(pinBuzzer);
delay(1000);
tone(pinBuzzer, 100, 500);
}
void loop() {
val = analogRead(pinPot);
Serial.println("Nilai Potensio: " + String(val));
lcd.setCursor(0, 0);
lcd.print("Potensio: " + String(val) + " ");
val = map(val, 0, 1023, 0, 180);
Serial.println("Nilai Derajat: " + String(val));
lcd.setCursor(0, 1);
lcd.print("Derajat: " + String(val) + " ");
int lebar = String(val).length();
if (lebar == 3) {
lcd.setCursor(12, 1);
lcd.write(0);
} else if (lebar == 2) {
lcd.setCursor(11, 1);
lcd.write(0);
} else if (lebar == 1) {
lcd.setCursor(10, 1);
lcd.write(0);
}
if (val >= 90) {
suaraBeep();
} else {
noTone(pinBuzzer);
}
myServo.write(val);
delay(15);
}