#include <Servo.h>
#include <LiquidCrystal_I2C.h>
int servo = 9;
int buzzer = 13;
int potentio = A0;
int mapval;
Servo myservo;
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte degr[] = {
B00110,
B01001,
B01001,
B00110,
B00000,
B00000,
B00000,
B00000
};
void setup() {
pinMode(buzzer, OUTPUT);
myservo.attach(9);
lcd.init();
lcd.backlight();
lcd.createChar(0, degr);
}
void loop() {
int potentio_value = potentioValue();
mapval = map(potentio_value, 0, 1023, 0, 180);
myservo.write(mapval);
// This is required so that the servo won't broke down
delay(15);
beep();
lcd.setCursor(0,0);
lcd.print("Potensio: " + String(potentio_value));
lcd.setCursor(0,1);
lcd.print("Derajat: " + String(mapval));
if(String(mapval).length() == 3){
lcd.setCursor(12, 1);
lcd.write(0);
}else if(String(mapval).length() == 2){
lcd.setCursor(11, 1);
lcd.write(0);
lcd.setCursor(12, 1);
lcd.print(" ");
}else {
lcd.setCursor(10, 1);
lcd.write(0);
lcd.setCursor(11, 1);
lcd.print(" ");
}
}
void beep(){
if(mapval > 90) {
tone(buzzer, 100, 150);
delay(1000);
} else if( mapval <90) {
noTone(buzzer);
}
//tone(buzzer, 100, 150);
//delay(1000);
}
int potentioValue(){
int val = analogRead(potentio);
return val;
}