Advertisement
safwan092

Untitled

Jan 26th, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h>
  3. //#include <stdio.h>
  4.  
  5. LiquidCrystal_I2C lcd(0x27, 16, 2);
  6.  
  7. int relay = 8;
  8. int relay2 = 9;
  9. int buzzer = 10;
  10.  
  11. String str;
  12. int blueToothVal;
  13.  
  14. int timer_started_flag = 0;
  15. long int total_Seconds = 0;
  16.  
  17. // Tracks the time since last event fired
  18. unsigned long int previoussecs = 0;
  19. unsigned long int currentsecs = 0;
  20. unsigned long currentMillis = 0;
  21. int interval = 1 ; // updated every 1 second
  22.  
  23. void setup()
  24. {
  25. Serial.begin(9600);
  26. pinMode(relay, OUTPUT);
  27. pinMode(relay2, OUTPUT);
  28. pinMode(buzzer, OUTPUT);
  29. digitalWrite(relay, HIGH);// relay is OFF
  30. digitalWrite(relay2, HIGH);// relay is OFF
  31. digitalWrite(buzzer, LOW);
  32.  
  33. lcd.init();
  34. lcd.init();
  35. lcd.backlight();
  36.  
  37. lcd.setCursor(0, 0);
  38. lcd.print("Hello, world!");
  39. lcd.setCursor(0, 1);
  40. lcd.print("Ywrobot Arduino!");
  41. }
  42.  
  43.  
  44. void loop()
  45. {
  46. bluetooth();
  47. countdown();
  48. }
  49.  
  50.  
  51. void bluetooth()
  52. {
  53. while (Serial.available() && timer_started_flag == 0)
  54. {
  55. {
  56. str = Serial.readStringUntil('\n');
  57. }
  58.  
  59. blueToothVal = (str.toInt());
  60. if (blueToothVal > 12) {
  61. blueToothVal = 12;
  62. }
  63. total_Seconds = blueToothVal * 60;// convert to sec
  64. timer_started_flag = 1;
  65. lcd.clear();
  66. lcd.print("T Seconds:");
  67. lcd.setCursor(11, 0);
  68. lcd.print( total_Seconds );
  69. delay(1000);
  70. }
  71. }
  72.  
  73.  
  74. void countdown() {
  75. while (timer_started_flag == 1) {
  76. lcd.setCursor(0, 0);
  77. lcd.print("T Seconds:");
  78. lcd.setCursor(11, 0);
  79. lcd.print( total_Seconds );
  80. lcd.setCursor(0, 1);
  81. if ( total_Seconds > 0)
  82. {
  83. digitalWrite(relay, LOW);// turn ON relay 1
  84. digitalWrite(relay2, LOW);// turn ON relay 2
  85. lcd.print("load ON ");
  86. }
  87.  
  88. if ( total_Seconds <= 0)
  89. {
  90. total_Seconds = 0;
  91. timer_started_flag = 0;
  92. digitalWrite(relay, HIGH);// turn OFF Relay 1
  93. digitalWrite(relay2, HIGH);// turn OFF Relay 2
  94. lcd.print("load OFF");
  95. //------------------------------Buzzer Tone
  96. digitalWrite(buzzer, HIGH);
  97. delay(500);
  98. digitalWrite(buzzer, LOW);
  99. delay(500);
  100. digitalWrite(buzzer, HIGH);
  101. delay(1500);
  102. digitalWrite(buzzer, LOW);
  103. delay(500);
  104. /////////////////////////////////////////////
  105. }
  106.  
  107. currentMillis = millis();
  108. currentsecs = currentMillis / 1000;
  109. if ((unsigned long)(currentsecs - previoussecs) >= interval) {
  110. total_Seconds = total_Seconds - 1;
  111. lcd.clear();
  112. previoussecs = currentsecs;
  113. }
  114. }//end of while loop
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement