#include <LCD_I2C.h>
LCD_I2C display(0x27, 16, 2);
#define POT_PIN A0
#define ENABLE_PIN 3
#define SETPOINT_PIN 2
#define BUZZER_PIN 4
int sensorValue;
int setPoint = 50;
bool en = false;
void setup() {
display.begin();
display.backlight();
pinMode(ENABLE_PIN, INPUT_PULLUP);
pinMode(SETPOINT_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
//menampilkan nilai baca sensor
sensorValue = map(analogRead(POT_PIN), 0, 1023, 0, 100);
char buffer[16];
sprintf(buffer, "Sensor: %3d", sensorValue);
display.setCursor(0, 0);
display.print(buffer);
//menampilkan nilai set point
if (digitalRead(SETPOINT_PIN) == 0) {
while (digitalRead(SETPOINT_PIN) == 0) {}
setPoint += 2;
if (setPoint > 80) setPoint = 50;
}
char buffer2[6];
sprintf(buffer2, "Sp:%d", setPoint);
display.setCursor(0, 1);
display.print(buffer2);
//menampilkan status enable
if (digitalRead(ENABLE_PIN) == 0) {
while (digitalRead(ENABLE_PIN) == 0) {}
en = !en;
}
if (en) {
display.setCursor(6, 1);
display.print("eN:ON ");
} else {
display.setCursor(6, 1);
display.print("eN:OFF");
}
//menampilkan status buzzer
if (en && sensorValue > setPoint) {
display.setCursor(13, 1);
display.print("B:H");
alarm(3, 100);
delay(1000);
} else {
display.setCursor(13, 1);
display.print("B:M");
}
}
void alarm(int times, long time) {
for (int i = 0; i < times; i++) {
tone(BUZZER_PIN, 2500);
delay(time);
noTone(BUZZER_PIN);
delay(time);
}
}