Advertisement
EmptySet5150

Final - Bathroom fan relay

Aug 20th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <TM1637.h> // include TM1637 libary "DigitalTube"
  2. #include "DHT.h" // include dht libary
  3. const int SO = 9; // setting pin 9 for trouble shooting
  4. const int CLK = 2; // pin CLK is connected to
  5. const int DIO = 3; // pin DIO is connected to
  6. const int DHTpin = 5; // pin dht is connected to
  7. #define DHTTYPE DHT11 // the type of DHT sensor
  8. TM1637 tm1637(CLK,DIO); // initalize TM1637 to pin
  9. DHT dht(DHTpin, DHTTYPE); // initalize dht to pin and type
  10. const int relay = 4; // pin relay is on
  11. void setup() {
  12.   dht.begin(); // initalize dht
  13.   tm1637.init(); // initalize tm1637
  14.   tm1637.set(2); // set brightness of tm1637
  15.   pinMode(relay, OUTPUT); // set relay pin as output
  16.   pinMode(SO, INPUT_PULLUP); // setting TS pin as a input
  17.   Serial.begin(9600); // start serial at 9600 baud
  18. }
  19. void loop() {
  20.   int humidity = dht.readHumidity(); // set humidity to the sensor humidity reading
  21.   int temp = dht.readTemperature(true); // set temp to temperature reading from sensor - true is added for Fahrenheit setting
  22.   int d1t = temp / 10; // set d1t to first digit of temp
  23.   int d2t = temp % 10; // set d2t to second digit of temp
  24.   int d1h = humidity / 10; // set d1h to first digit of humidity
  25.   int d2h = humidity % 10; // set d2h to second digit of humidity
  26.   if (humidity >= 55) digitalWrite(relay, HIGH); // if humidity is at or over 55 turn on relay
  27.   if (humidity <= 50) digitalWrite(relay, LOW); // if humidity is at or less than 50 turn off relay
  28.   tm1637.display(0,d1t); // display first temp digit on segment 0
  29.   tm1637.display(1,d2t); // display second temp digit on segment 1
  30.   tm1637.display(2,d1h); // display first humidity digit on segment 2
  31.   tm1637.display(3,d2h); // display second humidity digit on segment 3
  32.   if (temp == 00 || humidity == 00) { // if sensor is showing 00 prints EEEE
  33.     tm1637.display(0,14);
  34.     tm1637.display(1,14);
  35.     tm1637.display(2,14);
  36.     tm1637.display(3,14);
  37.   }
  38.   if (digitalRead(SO) == LOW) { // if SO is high we will start a serial display
  39.     Serial.println(" START DIAG SCREEN ");
  40.     Serial.print(dht.readHumidity()); // read humidity data from sensor
  41.     Serial.println(" Humidity from sensor ");
  42.     Serial.print(humidity); // read humidity data from humidity
  43.     Serial.println(" Humidity value stored ");
  44.     Serial.print(dht.readTemperature(true)); // read temp from sensor
  45.     Serial.println(" Temp from sensor ");
  46.     Serial.print(temp); // read temp data from temp
  47.     Serial.println(" Temp value stored ");
  48.     Serial.print(digitalRead(relay)); // read if relay is high or low
  49.     Serial.println(" Relay value ");
  50.     Serial.print(digitalRead(SO)); // read if SO is high or low
  51.     Serial.println(" SO value ");
  52.     Serial.println(" END DIAG SCREEN ");
  53.   }
  54.   delay(60000); // 60 second delay
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement