#define sw1 3
#define sw2 2
#define sw3 4
#define sw4 5
#include <EEPROM.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int timer = 0;
int s;
int m;
int h;
const int pinBuzzer = 12;
//button
int up, down, enter, back;
// menu
int menu = 0;
int start = 0;
int mode = 0;
int setting = 0;
// sub menu
int CountDown = 0;
int LedLcd = 0;
int Buzzer = 0;
// Untuk Menyimpan Suatu Data
int dataMode = 1;
int dataCountDown = 1;
int dataLedLcd = 1;
int dataBuzzer = 1;
void setup() {
pinMode(sw1, INPUT_PULLUP);
pinMode(sw2, INPUT_PULLUP);
pinMode(sw3, INPUT_PULLUP);
pinMode(sw4, INPUT_PULLUP);
pinMode(pinBuzzer, OUTPUT);
lcd.init();
lcd.backlight();
}
void loop() {
tombolMenu();
while (start >= 1) {
tombolStart();
}
while (mode >= 1) {
tombolMode();
while (CountDown >= 1) {
tombolCountDown();
tampilCountDown();
}
tampilMode();
}
while (setting >= 1) {
tombolSetting();
while (LedLcd >= 1) {
tombolLedLcd();
tampilLedLcd();
}
while (Buzzer >= 1) {
tombolBuzzer();
tampilBuzzer();
}
tampilSetting();
}
tampilMenu();
}
//==============TOMBOL==============
int tombol(int data, int nilaiAwal, int nilaiAkhir) {
down = digitalRead(sw1);
up = digitalRead(sw2);
enter = digitalRead(sw3);
back = digitalRead(sw4);
if (up == 0)
{
delay(200);
data++;
lcd.clear();
// return data;
}
if (up == 0 && data > nilaiAkhir)
{
data = nilaiAwal;
}
if (down == 0)
{
delay(200);
data--;
lcd.clear();
}
if (down == 0 && data < nilaiAwal)
{
data = nilaiAkhir;
}
return data;
}
void tombolMenu() {
menu = tombol(menu, 0, 4);
if (enter == 0 && menu == 0)
{
delay(200);
start++;
lcd.clear();
}
if (enter == 0 && menu == 3)
{
delay(200);
mode++;
lcd.clear();
}
if (enter == 0 && menu == 4)
{
delay(200);
setting++;
lcd.clear();
}
}
void tombolStart() {
if (dataMode == 1) {
for (s = 0 ; s < 60 ; s++) {
for (m = 0 ; m < 60 ; m++) {
for (h = 0 ; h < 60 ; h++) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Stopwatch :");
lcd.setCursor(2, 1);
lcd.print(timer / 3600);
lcd.setCursor(4, 1);
lcd.println(" : ");
lcd.setCursor(7, 1);
lcd.println(timer / 60 % 60);
lcd.setCursor(9, 1);
lcd.println(" : ");
lcd.setCursor(12, 1);
lcd.println(timer % 60);
delay(1000);
timer++;
}
}
}
}
if (dataMode == 2) {
if (dataCountDown == 1) {
for (s = 30 ; s > 0 ; s--) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Countdown :");
lcd.setCursor(2, 1);
lcd.print("0");
lcd.setCursor(4, 1);
lcd.println(" : ");
lcd.setCursor(7, 1);
lcd.println("0");
lcd.setCursor(9, 1);
lcd.println(" : ");
lcd.setCursor(12, 1);
lcd.println(s);
delay(1000);
}
menu = 0;
}
else if (dataCountDown == 2) {
for (s = 60 ; s > 0 ; s--) {
lcd.clear();
lcd.setCursor(2, 0);
lcd.print("Countdown :");
lcd.setCursor(2, 1);
lcd.print("0");
lcd.setCursor(4, 1);
lcd.println(" : ");
lcd.setCursor(7, 1);
lcd.println("0");
lcd.setCursor(9, 1);
lcd.println(" : ");
lcd.setCursor(12, 1);
lcd.println(s);
delay(1000);
}
menu = 0;
}
menu = 0;
}
menu = 0;
}
void tombolMode() {
dataMode = tombol(dataMode, 1, 2);
if (back == 0) {
delay(200);
mode = mode - mode;
lcd.clear();
menu = 3;
}
// Menuju ke sub menu
if (enter == 0 && dataMode == 2)
{
delay(200);
CountDown++;
lcd.clear();
}
}
void tombolSetting() {
setting = tombol(setting, 1, 2);
if (back == 0) {
delay(200);
setting = setting - setting;
lcd.clear();
menu = 4;
tampilMenu();
}
// Menuju ke sub menu
if (enter == 0 && setting == 1)
{
delay(200);
LedLcd++;
lcd.clear();
}
if (enter == 0 && setting == 2)
{
delay(200);
Buzzer++;
lcd.clear();
}
}
//==========TOMBOL SUB MENU==========
void tombolCountDown() {
dataCountDown = tombol(dataCountDown, 1, 2);
if (back == 0) {
delay(200);
CountDown = CountDown - CountDown;
mode = 2;
}
}
void tombolLedLcd() {
dataLedLcd = tombol(dataLedLcd, 1, 2);
if (back == 0) {
delay(200);
LedLcd = LedLcd - LedLcd;
setting = 1;
}
}
void tombolBuzzer() {
dataBuzzer = tombol(dataBuzzer, 1, 2);
if (back == 0) {
delay(200);
Buzzer = Buzzer - Buzzer;
setting = 2;
}
}
//==============TAMPILAN==============
void tampilMenu() {
if (menu == 0) {
lcd.setCursor(0, 0);
lcd.print("-> 0.START ");
lcd.setCursor(0, 1);
lcd.print(" 1.STOP ");
} else if (menu == 1) {
lcd.setCursor(0, 0);
lcd.print(" 0.START ");
lcd.setCursor(0, 1);
lcd.print("-> 1.STOP ");
} else if (menu == 2) {
lcd.setCursor(0, 0);
lcd.print("-> 2.RESET ");
lcd.setCursor(0, 1);
lcd.print(" 3.MODE ");
} else if (menu == 3) {
lcd.setCursor(0, 0);
lcd.print(" 2.RESET ");
lcd.setCursor(0, 1);
lcd.print("-> 3.MODE ");
} else if (menu == 4) {
lcd.setCursor(0, 0);
lcd.print("-> 4.SETTING ");
}
}
void tampilMode() {
if (dataMode == 1) {
lcd.setCursor(0, 0);
lcd.print("-> 1.STOP WATCH ");
lcd.setCursor(0, 1);
lcd.print(" 2.COUNT DOWN ");
} else if (dataMode == 2) {
lcd.setCursor(0, 0);
lcd.print(" 1.STOP WATCH ");
lcd.setCursor(0, 1);
lcd.print("-> 2.COUNT DOWN ");
}
}
void tampilSetting() {
if (setting == 1) {
lcd.setCursor(0, 0);
lcd.print("-> 1.LED LCD ");
lcd.setCursor(0, 1);
lcd.print(" 2.BUZZER ");
} else if (setting == 2) {
lcd.setCursor(0, 0);
lcd.print(" 1.LED LCD ");
lcd.setCursor(0, 1);
lcd.print("-> 2.BUZZER ");
}
}
//==========TAMPIL SUB MENU==========
void tampilCountDown() {
if (dataCountDown == 1) {
lcd.setCursor(0, 0);
lcd.print("-> 1.30 Detik ");
lcd.setCursor(0, 1);
lcd.print(" 2. 1 Menit ");
} else if (dataCountDown == 2) {
lcd.setCursor(0, 0);
lcd.print(" 1.30 Detik ");
lcd.setCursor(0, 1);
lcd.print("-> 2. 1 Menit ");
}
}
void tampilLedLcd() {
if (dataLedLcd == 1) {
lcd.setCursor(0, 0);
lcd.print("LED LCD :-> ON ");
lcd.setCursor(0, 1);
lcd.print(" OFF ");
lcd.backlight();
} else if (dataLedLcd == 2) {
lcd.setCursor(0, 0);
lcd.print("LED LCD : ON ");
lcd.setCursor(0, 1);
lcd.print(" -> OFF ");
lcd.noBacklight();
}
}
void tampilBuzzer() {
if (dataBuzzer == 1) {
lcd.setCursor(0, 0);
lcd.print("BUZZER :-> OFF ");
lcd.setCursor(0, 1);
lcd.print(" ON ");
noTone(pinBuzzer);
} else if (dataBuzzer == 2) {
lcd.setCursor(0, 0);
lcd.print("BUZZER : OFF ");
lcd.setCursor(0, 1);
lcd.print(" -> ON ");
tone(pinBuzzer, 1000);
delay(1000);
// Memainkan nada kedua dalam polisi sirene selama 100ms
tone(pinBuzzer, 1200);
delay(500);
}
}