Advertisement
seston

Bathroom Automation

Oct 19th, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1.  
  2.  
  3. /*******************************************************************Bathroom Automation - By lmsvvavr v.1.9.3 ********************************/
  4. /* Bathroom automation, based on humidity, light and movement (PIR) readings.
  5. What you need:
  6. One Arduino Board, this has been based on the MEGA, you can use other boards, you need however to adapt the sketch and connections.
  7. One LCD, this has been based on the DFROBOT 16X2, the LCD is however optional, the system will work just on its own, (possible adjustments required!).
  8. One DHT humidity and temperature sensor, you need to adapt the sketch if not using the DHT22.
  9. One LDR (Light Dependent Resistor) sensor.
  10. One PIR movement sensor.
  11. One dual relay board, capable of handling with your FAN load, please make the necessary research to ensure of that.
  12. */
  13.  
  14.  
  15.  
  16. #include "DHT.h" //DHT library, google to download it and install if you don't have it already.
  17. #include <LiquidCrystal.h> //LCD library, google to download it and install if you don't have it already.
  18. #define DHTPIN 50 //Defines the pin where the DATA is connected.
  19. #define DHTTYPE DHT22 // DHT 22 (AM2302), if you use DHT11 or DHT23 change it accordingly.
  20. #define btnRIGHT 0
  21. #define btnUP 1
  22. #define btnDOWN 2
  23. #define btnLEFT 3
  24. #define btnSELECT 4
  25. #define btnNONE 5
  26.  
  27. DHT dht(DHTPIN, DHTTYPE);
  28.  
  29. const int FAN = 20; //Defines the pin where the Fan relay is connected.
  30. const int LEDs = 16; //Defines the pin where the LEDs relay is connected.
  31. const int LDR = (A8); //Defines the pin where the LDR is connected.
  32. const int PIR = 18; //Dedines the pin where the PIR is connected.
  33. float hm; // Measured humidity
  34. int hd = 85; // Desired humidity
  35. int lcd_key = 0;
  36. int adc_key_in = 0;
  37. int adc_key_prev = 0;
  38. int count = 0;
  39. int LDRvalue = 0;
  40. int LDRsetpoint = 750; //Desired LDR trigger value
  41. int PIRstatus;
  42.  
  43. LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Pins where the lcd is connected, change it accordingly if you are using different pin assignment.
  44.  
  45. /************************************************ SETUP *************************************/
  46.  
  47. void setup() {
  48.  
  49. pinMode(FAN, OUTPUT); //Defines the pin as an output.
  50. pinMode(LEDs, OUTPUT); //Defines the pin as an output.
  51. pinMode(PIR, INPUT); //Defines the pin as input.0
  52. digitalWrite(LEDs, HIGH); //Keep LEDs relay off while the system is booting.
  53. digitalWrite(FAN, HIGH); //Keep FAN relay off while the system is booting.
  54. lcd.begin(16, 2); //Defines how many rows and columns the lcd has.
  55. lcd.setCursor(0,0);
  56. lcd.print(" Bathroom "); //Start-Up message line 0 (Top Line)
  57. lcd.setCursor(0,1);
  58. lcd.print(" Automation "); //Start-Up message line 1 (Bottom Line)
  59. delay(5000);
  60. lcd.setCursor(0,0);
  61. lcd.print("Created & Wroted"); //Start-Up message line 0 (Top Line)
  62. lcd.setCursor(0,1);
  63. lcd.print(" By lmsvvavr "); //Start-Up message line 1 (Bottom Line)
  64. delay(5000);
  65. lcd.setCursor(0,0);
  66. lcd.print("System Starting!");
  67. lcd.setCursor(0,1);
  68. lcd.print("Init Modules. ");
  69. delay(500);
  70. lcd.setCursor(0,1);
  71. lcd.print("Init Modules.. ");
  72. delay(500);
  73. lcd.setCursor(0,1);
  74. lcd.print("Init Modules... ");
  75. delay(500);
  76. lcd.setCursor(0,1);
  77. lcd.print("Init Modules....");
  78. delay(250);
  79. lcd.setCursor(0,1);
  80. lcd.print("Init RH% Sensor ");
  81. delay(2000);
  82. lcd.setCursor(0,1);
  83. lcd.print("Init LDR Sensor ");
  84. delay(2000);
  85. lcd.setCursor(0,1);
  86. lcd.print("Init PIR Sensor ");
  87. delay(2000);
  88. lcd.setCursor(0,0);
  89. lcd.print(" Initialization ");
  90. lcd.setCursor(0,1);
  91. lcd.print(" Completed! ");
  92. delay(2000);
  93. dht.begin();
  94. }
  95.  
  96. /********************************************** LOOP ***************************************/
  97.  
  98. void loop() {
  99.  
  100. hm = dht.readHumidity(); // Read the humidity.
  101.  
  102. if (hm > hd) //When the humidity measured is above the humidity desired, fan is turned on.
  103.  
  104. {
  105. digitalWrite(FAN, LOW);
  106. }
  107. else //Anything else, fan is off.
  108. {
  109. digitalWrite(FAN, HIGH);
  110. }
  111.  
  112. LDRvalue = analogRead(LDR); // Read the light sensor value.
  113. PIRstatus = digitalRead(PIR); // Read the movement sensor - digital values (zero or one)
  114.  
  115. if (LDRvalue > LDRsetpoint && PIRstatus==HIGH) //Light measured above setpoint (lighter) and movement not detected, lights off.
  116. {
  117. digitalWrite(LEDs, LOW);
  118. }
  119.  
  120. if (LDRvalue < LDRsetpoint || PIRstatus==LOW) //Light measured below setpoint (darker) and movement detected, lights on.
  121. {
  122. digitalWrite(LEDs, HIGH);
  123. }
  124.  
  125. {
  126. lcd.setCursor(0,0); //Set where about on the lcd you want to print the values.
  127. lcd.print(hm); //To print on the lcd the measured humidity.
  128. lcd.print("RH% FAN "); //Message in front of the desired humidity.
  129. lcd.print(hd); //To print on the lcd the desired humidity.
  130. lcd.print("%");
  131. lcd.setCursor(0,1); //Set where about on the lcd you want to print the values.
  132. lcd.print(LDRvalue); //To print on the lcd the measured light.
  133. lcd.print(" Light "); //Message in front of the measured light.
  134. lcd.print(LDRsetpoint); //To print in the lcd the desired light.
  135. lcd.print(" M");
  136. lcd.print(PIRstatus); //To print on the lcd the movement sensor status.
  137. }
  138.  
  139. adc_key_in = lcd_key;
  140. lcd_key = read_LCD_buttons(); // Read the buttons.
  141.  
  142. if (adc_key_prev != lcd_key)
  143. switch (lcd_key) // Depending on which button was pushed, perform an action
  144. {
  145. case btnRIGHT:
  146. {
  147. break;
  148. }
  149. case btnLEFT:
  150. {
  151. lcd.setCursor(0,0);
  152. lcd.print(LDRvalue);
  153. lcd.print(" Light Sensor");
  154. lcd.setCursor(0,1);
  155. LDRsetpoint = (LDRsetpoint + 10); // Increase light setpoint.
  156. lcd.print(LDRsetpoint); // Print on the LCD the now light setpoint.
  157. lcd.print(" Light Set To");
  158. break;
  159. }
  160. case btnUP:
  161. {
  162. lcd.setCursor(0,0);
  163. lcd.print(hm);
  164. lcd.print("RH% In Room");
  165. lcd.setCursor(0,1);
  166. hd = (hd + 1); // Increase the FAN humidity setpoint.
  167. lcd.print(hd);
  168. lcd.print(" RH% Desired ");
  169. break;
  170. }
  171. case btnDOWN:
  172. {
  173. lcd.setCursor(0,0);
  174. lcd.print(hm);
  175. lcd.print("RH% In Room");
  176. lcd.setCursor(0,1);
  177. hd = (hd - 1); // Decrease the FAN humidity setpoint.
  178. lcd.print(hd);
  179. lcd.print(" RH% Desired ");
  180. break;
  181. }
  182. case btnSELECT:
  183. {
  184. lcd.setCursor(0,0);
  185. lcd.print(LDRvalue);
  186. lcd.print(" Light Sensor");
  187. lcd.setCursor(0,1);
  188. LDRsetpoint = (LDRsetpoint - 10); // Decrease the light setpoint.
  189. lcd.print(LDRsetpoint); // Print on the LCD the now light setpoint.
  190. lcd.print(" Light Set To");
  191. break;
  192. }
  193. case btnNONE:
  194. {
  195. break;
  196. }
  197. }
  198. delay(1000);
  199. }
  200.  
  201. int read_LCD_buttons()
  202. {
  203. adc_key_in = analogRead(0); // read the value from the sensor
  204. delay(5); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
  205. int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
  206. if (5 < abs(k)) return btnNONE; // double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
  207. // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
  208. // we add approx 50 to those values and check to see if we are close
  209. if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  210. if (adc_key_in < 50) return btnRIGHT;
  211. if (adc_key_in < 250) return btnUP;
  212. if (adc_key_in < 450) return btnDOWN;
  213. if (adc_key_in < 650) return btnLEFT;
  214. if (adc_key_in < 850) return btnSELECT;
  215. return btnNONE; // when all others fail, return this...
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement