Advertisement
safwan092

Untitled

Mar 30th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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. int uvSensor = A0;
  11.  
  12. String str;
  13. int blueToothVal;
  14. int sensorValue = 0;
  15. float Vout = 0;
  16. float uvIntensity = 0;
  17.  
  18. int timer_started_flag = 0;
  19. long int total_Seconds = 0;
  20.  
  21. // Tracks the time since last event fired
  22. unsigned long int previoussecs = 0;
  23. unsigned long int currentsecs = 0;
  24. unsigned long currentMillis = 0;
  25. int interval = 1 ; // updated every 1 second
  26.  
  27. void setup()
  28. {
  29. Serial.begin(9600);
  30. pinMode(relay, OUTPUT);
  31. pinMode(relay2, OUTPUT);
  32. pinMode(buzzer, OUTPUT);
  33. pinMode(uvSensor,INPUT);
  34. digitalWrite(relay, HIGH);// relay is OFF
  35. digitalWrite(relay2, HIGH);// relay is OFF
  36. digitalWrite(buzzer, LOW);
  37.  
  38. lcd.init();
  39. lcd.init();
  40. lcd.backlight();
  41.  
  42. lcd.setCursor(0, 0);
  43. lcd.print("UVC Kit");
  44. lcd.setCursor(0, 1);
  45. lcd.print("Intensity Sensor");
  46. }
  47.  
  48.  
  49. void loop()
  50. {
  51. bluetooth();
  52. countdown();
  53. }
  54.  
  55.  
  56. void bluetooth()
  57. {
  58. while (Serial.available() && timer_started_flag == 0)
  59. {
  60. {
  61. str = Serial.readStringUntil('\n');
  62. }
  63.  
  64. blueToothVal = (str.toInt());
  65. if (blueToothVal > 30) {
  66. blueToothVal = 30;
  67. }
  68. total_Seconds = blueToothVal * 60;// convert to sec
  69. timer_started_flag = 1;
  70. //UVsensor(); // only take 1 reading when recieved order from bluetooth
  71. //Serial.write(abs(uvIntensity));
  72. lcd.clear();
  73. lcd.print("T Seconds:");
  74. lcd.setCursor(11, 0);
  75. lcd.print( total_Seconds );
  76. delay(1000);
  77. }
  78. }
  79.  
  80.  
  81. void countdown() {
  82. while (timer_started_flag == 1) {
  83. UVsensor();
  84. lcd.setCursor(0, 0);
  85. lcd.print("T Seconds:");
  86. lcd.setCursor(11, 0);
  87. lcd.print( total_Seconds );
  88. lcd.setCursor(0, 1);
  89. if ( total_Seconds > 0)
  90. {
  91. digitalWrite(relay, LOW);// turn ON relay 1
  92. digitalWrite(relay2, LOW);// turn ON relay 2
  93. //lcd.print("load ON ");
  94. lcd.print(abs(uvIntensity));
  95. }
  96.  
  97. if ( total_Seconds <= 0)
  98. {
  99. total_Seconds = 0;
  100. timer_started_flag = 0;
  101. digitalWrite(relay, HIGH);// turn OFF Relay 1
  102. digitalWrite(relay2, HIGH);// turn OFF Relay 2
  103. //lcd.print("load OFF");
  104. lcd.print(abs(uvIntensity));
  105. //------------------------------Buzzer Tone
  106. digitalWrite(buzzer, HIGH);
  107. delay(500);
  108. digitalWrite(buzzer, LOW);
  109. delay(500);
  110. digitalWrite(buzzer, HIGH);
  111. delay(1500);
  112. digitalWrite(buzzer, LOW);
  113. delay(500);
  114. /////////////////////////////////////////////
  115. }
  116.  
  117. currentMillis = millis();
  118. currentsecs = currentMillis / 1000;
  119. if ((unsigned long)(currentsecs - previoussecs) >= interval) {
  120. total_Seconds = total_Seconds - 1;
  121. lcd.clear();
  122. previoussecs = currentsecs;
  123. }
  124. }//end of while loop
  125. }
  126.  
  127. void UVsensor(){
  128. int uvLevel = analogRead(uvSensor);//averageAnalogRead
  129. float outputVoltage = (( uvLevel * 5000 ) / 1023.0); //0-1023 ---> [x] mV
  130. float index = mapfloat(outputVoltage, 0.0, 1000, 0.0, 11.0);// UV index
  131. uvIntensity = mapfloat(outputVoltage/10, 1, 2.8, 0.0, 15.0); //Convert the voltage to a UV intensity level
  132. }
  133.  
  134. float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  135. {
  136. if (x == 0) {
  137. return 0;
  138. }
  139. //mapfloat(outputVoltage/1000.0, 1, 2.8, 0.0, 15.0);
  140. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement