Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- const byte ROWS = 4;
- const byte COLS = 3;
- char hexaKeys[ROWS][COLS] = {
- {1, 2, 3},
- {4, 5, 6},
- {7, 8, 9},
- {'*', '0', '#'}
- };
- byte rowPins[ROWS] = {6, 7, 8, 9};
- byte colPins[COLS] = {10, 11, 12};
- Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
- #define BINTANG 42 // nilai BINTANG
- #define PAGAR 35 // nilai pagar
- long dtKey = 0;
- int key;
- String line1;
- String line2;
- #include <LiquidCrystal_I2C.h>
- Z lcd(0x27, 16, 2);
- #define rel_lampu 5
- #define rel_lampu_kerja 4
- int state = 0;
- #define STOP 0
- #define RUN 1
- #define FINISH 2
- long set_waktu = 0;
- long menit = 0, detik = 0;
- unsigned long cur_time, old_time;
- int buf_detik = 60, count_detik = 0;
- String str_menit, str_detik;
- void setup() {
- delay(100);
- pinMode(rel_lampu, OUTPUT);
- pinMode(rel_lampu_kerja, OUTPUT);
- Serial.begin(115200);
- lcd.init();
- lcd.backlight();
- }
- void loop() {
- key = keypad.getKey();
- if (key) {
- baca_key(); lcd.clear(); delay(20);
- }
- control();
- update_lcd();
- }
- void baca_key() {
- switch (state) {
- case STOP:
- if ( key == 48 )key = 0;
- if ( key < 10) {
- dtKey = dtKey * 10 + key;
- set_waktu = dtKey;
- menit = dtKey;
- }
- if (key == BINTANG) {
- dtKey = 0;
- set_waktu = 0;
- }
- if (key == PAGAR) {
- state = RUN;
- set_waktu = set_waktu * 60;
- menit = menit - 1;
- }
- break;
- case RUN:
- if (key == BINTANG) {
- set_waktu = 0;
- menit = 0;
- buf_detik = 60;
- count_detik = 0;
- state = STOP;
- }
- break;
- case FINISH:
- if (key == BINTANG) {
- set_waktu = 0;
- menit = 0;
- buf_detik = 60;
- count_detik = 0;
- state = STOP;
- }
- break;
- }
- }
- void get_text() {
- switch (state) {
- case STOP:
- line1 = "set waktu";
- line2 = "set :" + String(set_waktu) + " menit";
- break;
- case RUN:
- line1 = "RUNNING...";
- str_menit = menit < 10 ? "0" + String(menit) : String(menit);
- str_detik = buf_detik < 10 ? "0" + String(buf_detik) : String(buf_detik);
- line2 = "waktu: " + str_menit + ":" + str_detik;
- break;
- case FINISH:
- line1 = "FINISH...";
- line2 = "PRESS * BACK";
- break;
- }
- }
- void control() {
- switch (state) {
- case STOP:
- lampu_off();
- lampu_kerja_on();
- break;
- case RUN:
- lampu_on();
- lampu_kerja_on();
- cur_time = millis();
- if (cur_time - old_time >= 1000) {
- lcd.clear();
- if (count_detik == 60) {
- count_detik = 0;
- menit--;
- }
- if (buf_detik == 0) {
- buf_detik = 60;
- }
- count_detik ++;
- buf_detik--;
- set_waktu--;
- if (set_waktu == 0) {
- set_waktu = 0;
- menit = 0;
- state = FINISH;
- }
- old_time = cur_time;
- }
- break;
- case FINISH:
- lampu_off();
- lampu_kerja_on();
- break;
- }
- }
- void update_lcd() {
- get_text();
- lcd.setCursor(0, 0);
- lcd.print(line1);
- lcd.setCursor(0, 1);
- lcd.print(line2);
- }
- void lampu_on() {
- digitalWrite(rel_lampu, HIGH);
- }
- void lampu_off() {
- digitalWrite(rel_lampu, LOW);
- }
- void lampu_kerja_on(){
- digitalWrite(rel_lampu_kerja, HIGH);
- }
- void lampu_kerja_off(){
- digitalWrite(rel_lampu_kerja, LOW);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement