#include <LCD_I2C.h>
LCD_I2C lcd(0x27, 16, 2);
#define sensor A0
#define buzz 13
#define PB1 12
#define PB2 11
int Setpoint = 50;
int value;
int sensorValue;
bool EN = false;
void setup() {
lcd.begin();
lcd.backlight();
pinMode(PB1, INPUT);
pinMode(PB2, INPUT);
pinMode(buzz, OUTPUT);
lcd.setCursor(2, 0);
lcd.print("KELAS TEKNIK");
lcd.setCursor(4, 1);
lcd.print("OTOMASI");
tone(buzz, 1000);
delay(1000);
noTone(buzz);
lcd.clear();
}
void loop() {
value = analogRead(sensor);
// sensorValue = value / 10;
sensorValue = map(value, -10, 1023, 0, 100);
// if (sensorValue < 0){
// sensorValue = 0;
// }
// if (sensorValue > 100){
// sensorValue = 100;
// }
lcd.setCursor(0, 0);
lcd.print("JARAK: " + String(sensorValue) + " CM ");
lcd.setCursor(0, 1);
lcd.print("SP:" + String(Setpoint));
if (digitalRead(PB1) == 1){
while (digitalRead(PB1) ==1){}
Setpoint +=2;
}
if (digitalRead(PB2) == 1){
while (digitalRead(PB2) ==1){}
EN = !EN;
}
if(EN){
lcd.setCursor(6, 1);
lcd.print("EN:ON ");
}
else{
lcd.setCursor(6, 1);
lcd.print("EN:OFF");
}
if (EN && sensorValue > Setpoint) {
tone(buzz, 1000);
lcd.setCursor(13, 1);
lcd.print("B:H");
}
else {
noTone(buzz);
lcd.setCursor(13, 1);
lcd.print("B:M");
}
}