Advertisement
safwan092

project_3679

Sep 26th, 2021
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.91 KB | None | 0 0
  1. #include <Keypad.h>
  2. //#include <LiquidCrystal.h>
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6.  
  7. #include<stdio.h>
  8. const byte ROWS = 4;
  9. const byte COLS = 3;
  10. char keys[ROWS][COLS] = {
  11.   {'1', '2', '3'},
  12.   {'4', '5', '6'},
  13.   {'7', '8', '9'},
  14.   {'*', '0', '#'}
  15. };
  16. byte rowPins[ROWS] = {4, 5, 6, 7};
  17. byte colPins[COLS] = {8, 9, 10};
  18.  
  19. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  20.  
  21.  
  22.  
  23.  
  24. int buzzer = 11;
  25. int relay  = 12;
  26.  
  27. int counter = 0;
  28. int attempts = 0;
  29. int max_attempts = 3;
  30.  
  31. String mymints;
  32. int minutes = 0;
  33.  
  34. String mysecs;
  35. int seconds = 0;
  36. long int total_seconds = 0;
  37. int secflag = 0;
  38. int timer_started_flag = 0;
  39.  
  40.  
  41.  
  42. unsigned long previousMillis = 0;
  43. unsigned long int previoussecs = 0;
  44. unsigned long int currentsecs = 0;
  45. unsigned long currentMillis = 0;
  46. int interval = 1 ;
  47. int tsecs = 0;
  48.  
  49. void setup() {
  50.  
  51.   Serial.begin(9600);
  52.  
  53.   lcd.init();
  54.   lcd.backlight();
  55.  
  56.   pinMode(buzzer, OUTPUT);
  57.   pinMode(relay, OUTPUT);
  58.  
  59.   digitalWrite(buzzer, LOW);
  60.   digitalWrite(relay, LOW);
  61.  
  62.   lcd.print("Countdown Timer");
  63.  
  64.   delay(2000);
  65.   lcd.clear();
  66.   lcd.print("Enter Minutes:");
  67. }
  68.  
  69. void loop()
  70. {
  71.  
  72.   keypadfunction();
  73.  
  74. }
  75.  
  76. void keypadfunction()
  77. {
  78.   char key = keypad.getKey();
  79.  
  80.   if (key) {
  81.     Serial.println(key);
  82.     counter = counter + 1;
  83.     lcd.setCursor(counter, 1);
  84.     lcd.print(key);
  85.   }
  86.  
  87.   if (key == '1')
  88.   {
  89.  
  90.     mymints = mymints + 1;
  91.   }
  92.  
  93.   if (key == '2')
  94.   {
  95.  
  96.     mymints = mymints + 2;
  97.   }
  98.  
  99.   if (key == '3')
  100.   {
  101.  
  102.     mymints = mymints + 3;
  103.   }
  104.  
  105.   if (key == '4')
  106.   {
  107.  
  108.     mymints = mymints + 4;
  109.   }
  110.  
  111.   if (key == '5')
  112.   {
  113.  
  114.     mymints = mymints + 5;
  115.   }
  116.  
  117.   if (key == '6')
  118.   {
  119.  
  120.     mymints = mymints + 6;
  121.   }
  122.  
  123.   if (key == '7')
  124.   {
  125.  
  126.     mymints = mymints + 7;
  127.   }
  128.  
  129.   if (key == '8')
  130.   {
  131.  
  132.     mymints = mymints + 8;
  133.   }
  134.  
  135.   if (key == '9')
  136.   {
  137.  
  138.     mymints = mymints + 9;
  139.   }
  140.  
  141.   if (key == '0')
  142.   {
  143.  
  144.     mymints = mymints + 0;
  145.   }
  146.  
  147.   if (key == '#')
  148.   {
  149.  
  150.     counter = 0;
  151.     mymints = "";
  152.     minutes = 0;
  153.     mysecs = "";
  154.     seconds = 0;
  155.     secflag = 0;
  156.  
  157.     lcd.clear();
  158.     lcd.backlight();
  159.     lcd.setCursor(0, 0);
  160.     lcd.print("Enter Minutes:");
  161.   }
  162.  
  163.  
  164.   if (key == '*')
  165.   {
  166.     lcd.clear();
  167.     minutes = mymints.toInt();
  168.     Serial.println(minutes);
  169.     lcd.clear();
  170.     lcd.print("Minutes: ");
  171.     lcd.setCursor(0, 1);
  172.     lcd.print(minutes);
  173.     mymints = ""; // empty the string
  174.     delay(2000);
  175.     lcd.clear();
  176.     lcd.setCursor(0, 0);
  177.     lcd.print("Enter Seconds:");
  178.     counter = 0;
  179.     secflag = 1;
  180.  
  181.     while (secflag == 1)
  182.     {
  183.       forSeconds();
  184.     }
  185.   }
  186.  
  187. }
  188.  
  189. void forSeconds()
  190. {
  191.   char key = keypad.getKey();
  192.  
  193.   if (key) {
  194.     Serial.println(key);
  195.     counter = counter + 1;
  196.     lcd.setCursor(counter, 1);
  197.     lcd.print(key);
  198.   }
  199.  
  200.   if (key == '1')
  201.   {
  202.  
  203.     mysecs = mysecs + 1;
  204.   }
  205.  
  206.   if (key == '2')
  207.   {
  208.  
  209.     mysecs = mysecs + 2;
  210.   }
  211.  
  212.   if (key == '3')
  213.   {
  214.  
  215.     mysecs = mysecs + 3;
  216.   }
  217.  
  218.   if (key == '4')
  219.   {
  220.  
  221.     mysecs = mysecs + 4;
  222.   }
  223.  
  224.   if (key == '5')
  225.   {
  226.  
  227.     mysecs = mysecs + 5;
  228.   }
  229.  
  230.   if (key == '6')
  231.   {
  232.  
  233.     mysecs = mysecs + 6;
  234.   }
  235.  
  236.   if (key == '7')
  237.   {
  238.  
  239.     mysecs = mysecs + 7;
  240.   }
  241.  
  242.   if (key == '8')
  243.   {
  244.  
  245.     mysecs = mysecs + 8;
  246.   }
  247.  
  248.   if (key == '9')
  249.   {
  250.  
  251.     mysecs = mysecs + 9;
  252.   }
  253.  
  254.   if (key == '0')
  255.   {
  256.  
  257.     mysecs = mysecs + 0;
  258.   }
  259.  
  260.   if (key == '#')
  261.   {
  262.  
  263.     counter = 0;
  264.     mymints = "";
  265.     minutes = 0;
  266.     mysecs = "";
  267.     seconds = 0;
  268.     secflag = 0;
  269.  
  270.     lcd.clear();
  271.     lcd.setCursor(0, 0);
  272.     lcd.print("Enter Minutes:");
  273.   }
  274.  
  275.  
  276.  
  277.   if (key == '*')
  278.   {
  279.  
  280.     lcd.clear();
  281.     seconds = mysecs.toInt();
  282.     Serial.println(seconds);
  283.     lcd.clear();
  284.     lcd.setCursor(0, 0);
  285.     lcd.print("Seconds: ");
  286.     lcd.setCursor(0, 1);
  287.     lcd.print(seconds);
  288.     mysecs = ""; // empty the string
  289.     delay(2000);
  290.     lcd.clear();
  291.     lcd.setCursor(0, 0);
  292.  
  293.     lcd.print("Mins      Secs");
  294.     lcd.setCursor(1, 1);
  295.     lcd.print(minutes);
  296.     lcd.setCursor(10, 1);
  297.     lcd.print(seconds);
  298.     total_seconds = (minutes * 60) + seconds ;
  299.     counter = 0;
  300.     secflag = 0;
  301.     timer_started_flag = 1;
  302.  
  303.     lcd.clear();
  304.     lcd.print("T Seconds:");
  305.     lcd.setCursor(11, 0);
  306.     lcd.print( total_seconds );
  307.     delay(3000);
  308.     while ( timer_started_flag == 1)
  309.     {
  310.       char key = keypad.getKey();
  311.  
  312.       if (key) {
  313.         Serial.println(key);
  314.         counter = counter + 1;
  315.         lcd.setCursor(counter, 1);
  316.  
  317.       }
  318.  
  319.       if (key == '#')
  320.       {
  321.  
  322.         counter = 0;
  323.         mymints = "";
  324.         minutes = 0;
  325.         mysecs = "";
  326.         seconds = 0;
  327.         secflag = 0;
  328.         total_seconds = 0;
  329.         timer_started_flag = 0;
  330.         lcd.clear();
  331.         lcd.setCursor(0, 0);
  332.         lcd.print("Enter Minutes:");
  333.       }
  334.  
  335.       //  lcd.clear();
  336.       lcd.setCursor(0, 0);
  337.       lcd.print("T Seconds:");
  338.       lcd.setCursor(11, 0);
  339.       lcd.print( total_seconds );
  340.       lcd.setCursor(0, 1);
  341.       if ( total_seconds > 0)
  342.       {
  343.         digitalWrite(relay, HIGH);
  344.         lcd.print("load ON ");
  345.       }
  346.  
  347.       if ( total_seconds <= 0)
  348.       {
  349.         total_seconds = 0;
  350.         digitalWrite(relay, LOW);
  351.         lcd.print("load OFF");
  352.         digitalWrite(buzzer, HIGH);
  353.         delay(2000);
  354.         lcd.noBacklight();
  355.         digitalWrite(buzzer, LOW);
  356.         lcd.clear();
  357.         counter = 0;
  358.         mymints = "";
  359.         minutes = 0;
  360.         mysecs = "";
  361.         seconds = 0;
  362.         secflag = 0;
  363.         total_seconds = 0;
  364.         timer_started_flag = 0;
  365.  
  366.       }
  367.  
  368.       currentMillis = millis();
  369.       currentsecs = currentMillis / 1000;
  370.       if ((unsigned long)(currentsecs - previoussecs) >= interval) {
  371.         total_seconds = total_seconds - 1;
  372.         lcd.clear();
  373.         previoussecs = currentsecs;
  374.       }
  375.     }
  376.  
  377.   }
  378.  
  379.  
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement