Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************************Bathroom Automation - By lmsvvavr v.1.9.3 ********************************/
- /* Bathroom automation, based on humidity, light and movement (PIR) readings.
- What you need:
- One Arduino Board, this has been based on the MEGA, you can use other boards, you need however to adapt the sketch and connections.
- 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!).
- One DHT humidity and temperature sensor, you need to adapt the sketch if not using the DHT22.
- One LDR (Light Dependent Resistor) sensor.
- One PIR movement sensor.
- One dual relay board, capable of handling with your FAN load, please make the necessary research to ensure of that.
- */
- #include "DHT.h" //DHT library, google to download it and install if you don't have it already.
- #include <LiquidCrystal.h> //LCD library, google to download it and install if you don't have it already.
- #define DHTPIN 50 //Defines the pin where the DATA is connected.
- #define DHTTYPE DHT22 // DHT 22 (AM2302), if you use DHT11 or DHT23 change it accordingly.
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- DHT dht(DHTPIN, DHTTYPE);
- const int FAN = 20; //Defines the pin where the Fan relay is connected.
- const int LEDs = 16; //Defines the pin where the LEDs relay is connected.
- const int LDR = (A8); //Defines the pin where the LDR is connected.
- const int PIR = 18; //Dedines the pin where the PIR is connected.
- float hm; // Measured humidity
- int hd = 85; // Desired humidity
- int lcd_key = 0;
- int adc_key_in = 0;
- int adc_key_prev = 0;
- int count = 0;
- int LDRvalue = 0;
- int LDRsetpoint = 750; //Desired LDR trigger value
- int PIRstatus;
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //Pins where the lcd is connected, change it accordingly if you are using different pin assignment.
- /************************************************ SETUP *************************************/
- void setup() {
- pinMode(FAN, OUTPUT); //Defines the pin as an output.
- pinMode(LEDs, OUTPUT); //Defines the pin as an output.
- pinMode(PIR, INPUT); //Defines the pin as input.0
- digitalWrite(LEDs, HIGH); //Keep LEDs relay off while the system is booting.
- digitalWrite(FAN, HIGH); //Keep FAN relay off while the system is booting.
- lcd.begin(16, 2); //Defines how many rows and columns the lcd has.
- lcd.setCursor(0,0);
- lcd.print(" Bathroom "); //Start-Up message line 0 (Top Line)
- lcd.setCursor(0,1);
- lcd.print(" Automation "); //Start-Up message line 1 (Bottom Line)
- delay(5000);
- lcd.setCursor(0,0);
- lcd.print("Created & Wroted"); //Start-Up message line 0 (Top Line)
- lcd.setCursor(0,1);
- lcd.print(" By lmsvvavr "); //Start-Up message line 1 (Bottom Line)
- delay(5000);
- lcd.setCursor(0,0);
- lcd.print("System Starting!");
- lcd.setCursor(0,1);
- lcd.print("Init Modules. ");
- delay(500);
- lcd.setCursor(0,1);
- lcd.print("Init Modules.. ");
- delay(500);
- lcd.setCursor(0,1);
- lcd.print("Init Modules... ");
- delay(500);
- lcd.setCursor(0,1);
- lcd.print("Init Modules....");
- delay(250);
- lcd.setCursor(0,1);
- lcd.print("Init RH% Sensor ");
- delay(2000);
- lcd.setCursor(0,1);
- lcd.print("Init LDR Sensor ");
- delay(2000);
- lcd.setCursor(0,1);
- lcd.print("Init PIR Sensor ");
- delay(2000);
- lcd.setCursor(0,0);
- lcd.print(" Initialization ");
- lcd.setCursor(0,1);
- lcd.print(" Completed! ");
- delay(2000);
- dht.begin();
- }
- /********************************************** LOOP ***************************************/
- void loop() {
- hm = dht.readHumidity(); // Read the humidity.
- if (hm > hd) //When the humidity measured is above the humidity desired, fan is turned on.
- {
- digitalWrite(FAN, LOW);
- }
- else //Anything else, fan is off.
- {
- digitalWrite(FAN, HIGH);
- }
- LDRvalue = analogRead(LDR); // Read the light sensor value.
- PIRstatus = digitalRead(PIR); // Read the movement sensor - digital values (zero or one)
- if (LDRvalue > LDRsetpoint && PIRstatus==HIGH) //Light measured above setpoint (lighter) and movement not detected, lights off.
- {
- digitalWrite(LEDs, LOW);
- }
- if (LDRvalue < LDRsetpoint || PIRstatus==LOW) //Light measured below setpoint (darker) and movement detected, lights on.
- {
- digitalWrite(LEDs, HIGH);
- }
- {
- lcd.setCursor(0,0); //Set where about on the lcd you want to print the values.
- lcd.print(hm); //To print on the lcd the measured humidity.
- lcd.print("RH% FAN "); //Message in front of the desired humidity.
- lcd.print(hd); //To print on the lcd the desired humidity.
- lcd.print("%");
- lcd.setCursor(0,1); //Set where about on the lcd you want to print the values.
- lcd.print(LDRvalue); //To print on the lcd the measured light.
- lcd.print(" Light "); //Message in front of the measured light.
- lcd.print(LDRsetpoint); //To print in the lcd the desired light.
- lcd.print(" M");
- lcd.print(PIRstatus); //To print on the lcd the movement sensor status.
- }
- adc_key_in = lcd_key;
- lcd_key = read_LCD_buttons(); // Read the buttons.
- if (adc_key_prev != lcd_key)
- switch (lcd_key) // Depending on which button was pushed, perform an action
- {
- case btnRIGHT:
- {
- break;
- }
- case btnLEFT:
- {
- lcd.setCursor(0,0);
- lcd.print(LDRvalue);
- lcd.print(" Light Sensor");
- lcd.setCursor(0,1);
- LDRsetpoint = (LDRsetpoint + 10); // Increase light setpoint.
- lcd.print(LDRsetpoint); // Print on the LCD the now light setpoint.
- lcd.print(" Light Set To");
- break;
- }
- case btnUP:
- {
- lcd.setCursor(0,0);
- lcd.print(hm);
- lcd.print("RH% In Room");
- lcd.setCursor(0,1);
- hd = (hd + 1); // Increase the FAN humidity setpoint.
- lcd.print(hd);
- lcd.print(" RH% Desired ");
- break;
- }
- case btnDOWN:
- {
- lcd.setCursor(0,0);
- lcd.print(hm);
- lcd.print("RH% In Room");
- lcd.setCursor(0,1);
- hd = (hd - 1); // Decrease the FAN humidity setpoint.
- lcd.print(hd);
- lcd.print(" RH% Desired ");
- break;
- }
- case btnSELECT:
- {
- lcd.setCursor(0,0);
- lcd.print(LDRvalue);
- lcd.print(" Light Sensor");
- lcd.setCursor(0,1);
- LDRsetpoint = (LDRsetpoint - 10); // Decrease the light setpoint.
- lcd.print(LDRsetpoint); // Print on the LCD the now light setpoint.
- lcd.print(" Light Set To");
- break;
- }
- case btnNONE:
- {
- break;
- }
- }
- delay(1000);
- }
- int read_LCD_buttons()
- {
- adc_key_in = analogRead(0); // read the value from the sensor
- delay(5); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
- int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
- 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.
- // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 250) return btnUP;
- if (adc_key_in < 450) return btnDOWN;
- if (adc_key_in < 650) return btnLEFT;
- if (adc_key_in < 850) return btnSELECT;
- return btnNONE; // when all others fail, return this...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement