Advertisement
mikroavr

timer_lamp_arduino_nano

Jun 18th, 2021
1,425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Keypad.h>
  2.  
  3. const byte ROWS = 4;
  4. const byte COLS = 3;
  5.  
  6. char hexaKeys[ROWS][COLS] = {
  7.   {1, 2, 3},
  8.   {4, 5, 6},
  9.   {7, 8, 9},
  10.   {'*', '0', '#'}
  11. };
  12.  
  13. byte rowPins[ROWS] = {6, 7, 8, 9};
  14. byte colPins[COLS] = {10, 11, 12};
  15.  
  16. Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
  17. #define BINTANG 42 // nilai BINTANG
  18. #define PAGAR 35 // nilai pagar
  19. long dtKey = 0;
  20. int key;
  21. String line1;
  22. String line2;
  23.  
  24.  
  25. #include <LiquidCrystal_I2C.h>
  26. Z lcd(0x27, 16, 2);
  27.  
  28. #define rel_lampu 5
  29. #define rel_lampu_kerja 4
  30.  
  31. int state = 0;
  32.  
  33. #define STOP 0
  34. #define RUN 1
  35. #define FINISH 2
  36.  
  37. long set_waktu = 0;
  38. long menit = 0, detik = 0;
  39. unsigned long cur_time, old_time;
  40. int buf_detik = 60, count_detik = 0;
  41. String str_menit, str_detik;
  42.  
  43. void setup() {
  44.   delay(100);
  45.   pinMode(rel_lampu, OUTPUT);
  46.   pinMode(rel_lampu_kerja, OUTPUT);
  47.  
  48.   Serial.begin(115200);
  49.   lcd.init();
  50.  
  51.   lcd.backlight();
  52. }
  53.  
  54. void loop() {
  55.   key = keypad.getKey();
  56.   if (key) {
  57.     baca_key(); lcd.clear(); delay(20);
  58.   }
  59.   control();
  60.   update_lcd();
  61. }
  62.  
  63. void baca_key() {
  64.   switch (state) {
  65.     case STOP:
  66.       if ( key == 48 )key = 0;
  67.       if ( key < 10) {
  68.         dtKey = dtKey * 10 + key;
  69.         set_waktu = dtKey;
  70.         menit = dtKey;
  71.       }
  72.       if (key == BINTANG) {
  73.         dtKey = 0;
  74.         set_waktu = 0;
  75.       }
  76.       if (key == PAGAR) {
  77.         state = RUN;
  78.         set_waktu = set_waktu * 60;
  79.         menit = menit - 1;
  80.       }
  81.       break;
  82.     case RUN:
  83.       if (key == BINTANG) {
  84.         set_waktu = 0;
  85.         menit = 0;
  86.         buf_detik = 60;
  87.         count_detik = 0;
  88.         state = STOP;
  89.       }
  90.       break;
  91.     case FINISH:
  92.       if (key == BINTANG) {
  93.         set_waktu = 0;
  94.         menit = 0;
  95.         buf_detik = 60;
  96.         count_detik = 0;
  97.         state = STOP;
  98.       }
  99.       break;
  100.   }
  101. }
  102.  
  103.  
  104. void get_text() {
  105.   switch (state) {
  106.     case STOP:
  107.       line1 = "set waktu";
  108.       line2 = "set :" + String(set_waktu) + " menit";
  109.       break;
  110.     case RUN:
  111.       line1 = "RUNNING...";
  112.       str_menit = menit < 10 ? "0" + String(menit) : String(menit);
  113.       str_detik = buf_detik < 10 ? "0" + String(buf_detik) : String(buf_detik);
  114.       line2 = "waktu: " + str_menit + ":" + str_detik;
  115.       break;
  116.     case FINISH:
  117.       line1 = "FINISH...";
  118.       line2 = "PRESS * BACK";
  119.       break;
  120.   }
  121. }
  122.  
  123. void control() {
  124.   switch (state) {
  125.     case STOP:
  126.       lampu_off();
  127.       lampu_kerja_on();
  128.       break;
  129.     case RUN:
  130.       lampu_on();
  131.       lampu_kerja_on();
  132.      
  133.       cur_time = millis();
  134.       if (cur_time - old_time >= 1000) {
  135.         lcd.clear();
  136.         if (count_detik == 60) {
  137.           count_detik = 0;
  138.           menit--;
  139.         }
  140.         if (buf_detik == 0) {
  141.           buf_detik = 60;
  142.         }
  143.        
  144.         count_detik ++;
  145.         buf_detik--;
  146.         set_waktu--;
  147.         if (set_waktu == 0) {
  148.           set_waktu = 0;
  149.           menit = 0;
  150.           state = FINISH;
  151.         }
  152.         old_time = cur_time;
  153.       }
  154.       break;
  155.  
  156.     case FINISH:
  157.       lampu_off();
  158.       lampu_kerja_on();
  159.       break;
  160.   }
  161. }
  162.  
  163. void update_lcd() {
  164.   get_text();
  165.   lcd.setCursor(0, 0);
  166.   lcd.print(line1);
  167.   lcd.setCursor(0, 1);
  168.   lcd.print(line2);
  169. }
  170.  
  171. void lampu_on() {
  172.   digitalWrite(rel_lampu, HIGH);
  173. }
  174.  
  175. void lampu_off() {
  176.   digitalWrite(rel_lampu, LOW);
  177. }
  178.  
  179. void lampu_kerja_on(){
  180.   digitalWrite(rel_lampu_kerja, HIGH);
  181. }
  182.  
  183. void lampu_kerja_off(){
  184.   digitalWrite(rel_lampu_kerja, LOW);
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement