thor94ben
Published © GPL3+

RememberAir - helps you remember to air your room

RememberAir utilizes humidity sensor and distance sensor to help you keep your room at safe humidity levels.

IntermediateWork in progress169
RememberAir - helps you remember to air your room

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
DHT11 Temperature & Humidity Sensor (3 pins)
DHT11 Temperature & Humidity Sensor (3 pins)
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
High Brightness LED, White
High Brightness LED, White
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Resistor 220 ohm
Resistor 220 ohm
×3
LED, Blue
LED, Blue
×1

Story

Read more

Schematics

Plan

how to place your parts

Code

code draft

C/C++
#include <DHT.h>

#define WINDOWPIN 2
#define CONTROLLED 13
#define DHTPIN 3
#define DHTTYPE DHT11
#define REDLED 10
#define YELLOWLED 11
#define GREENLED 12
#define TRIPWIRETRIGGER 7
#define TRIPWIREECHO 6
#define ACTIONLED 8

class Tripwire {
  public:
    Tripwire(){
      pinMode(TRIPWIRETRIGGER, OUTPUT);
      pinMode(TRIPWIREECHO, INPUT);
    };
    ~Tripwire(){};
    bool isTripped() {
      // sample 3 times to eliminate most noise
      bool wasTripped = 1;
      for(int i = 0; i < 3; i++) {
        // pulse to check obstacles
        digitalWrite(TRIPWIRETRIGGER, HIGH);
        delayMicroseconds(10);
        digitalWrite(TRIPWIRETRIGGER, LOW);
        // read to check echo time
        unsigned long duration = pulseIn(TRIPWIREECHO, HIGH);
//        Serial.print(duration); Serial.print("\n");
        // check if wire was tripped (aka lower duration than threshold)
        wasTripped &= duration <= tripDuration;      
      }
      Serial.print("\nTripwire tripped? "); Serial.print(wasTripped);
      return wasTripped;
    }
  private:
    unsigned long tripDuration = 3000;
};

class Window {
  public:
    Window() {
      pinMode(WINDOWPIN, INPUT);
    }
    ~Window(){};
    bool isClosed() {
      bool state = digitalRead(WINDOWPIN);
      for (int counter = 0; counter < stableTime; counter++) {
        delay(1);
        bool temp = digitalRead(WINDOWPIN);
        if (temp != state) counter = 0;
        state = temp;
      }
      if (state) {
        Serial.print("\nwindow is closed");
      } else {
        Serial.print("\nwindow is open");
      }
      return state;
    }
  private:
    int stableTime = 10;
};

class LED {
  public:
    LED(){
      randomSeed(analogRead(0));
      };
    ~LED(){};
    void setPin(byte pin) {
      ledPin = pin;
      pinMode(ledPin, OUTPUT);
    }
    byte getPin() {return ledPin;};
    void setOn() {
      isOn = 1;
      digitalWrite(ledPin, HIGH);
    }
    void setOff() {
      isOn = 0;
      digitalWrite(ledPin, LOW);
    }
    void randomBlink(int duration) {
      int counter = 0;
      while (counter < duration) {
        toggle();
        int randomTime = random(100, 1000);
        counter += randomTime;
        delay(randomTime);
      }
      setOff();
    }
    void toggle() {
//      Serial.print("toggling");Serial.print(isOn);
      if (isOn) {
        setOff();
      } else {
        setOn();
      }
    }
  private:
    byte ledPin;
    bool isOn = 0;
};

LED controlLed;
LED redLed;
LED yellowLed;
LED greenLed;
LED actionLed;
Window window;
DHT dht(DHTPIN, DHTTYPE);
const float upperHumidityBound = 80;
const float lowerHumidityBound = 60;
Tripwire wire;
// for indicating humidity, imaginary
LED humidityLed;
// bool for indicting if human intervention is requiredd to remedy humidity values
bool isActionRequired;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.print("setup\n");
  
  controlLed.setPin(CONTROLLED);
  redLed.setPin(REDLED);
  yellowLed.setPin(YELLOWLED);
  greenLed.setPin(GREENLED);
  actionLed.setPin(ACTIONLED);
  window = Window();

  dht.begin();
}

void loop() {
  // put your main code here, to run repeatedly:  
  if (window.isClosed()) {
    controlLed.setOff();
  } else {
    controlLed.setOn();
  }
  
  delay(2000); //sampling rate of DHT @0.5Hz
  //turn off current HumidityLed
  float humidity = dht.readHumidity();
  Serial.print("\nhumidity: "); Serial.print(humidity); 
//  float temperature = dht.readTemperature();
//  Serial.print(temperature);
  humidityLed.setOff();
  if (humidity >= upperHumidityBound) {
    humidityLed = redLed;  
    if (window.isClosed()) {
      isActionRequired = 1;
    } else {
      isActionRequired = 0;
    }
  } else if (humidity >= lowerHumidityBound) {
    humidityLed = yellowLed;
  } else {
    humidityLed = greenLed;
    if (!window.isClosed()) {
      isActionRequired = 1;
    } else {
      isActionRequired = 0;
    }
  }
  humidityLed.setOn();
  Serial.print("\n Action required? ");Serial.print(isActionRequired);
  if(wire.isTripped()&& isActionRequired) {
    Serial.print("\ntake Action!");
    actionLed.randomBlink(10000);
  }
  Serial.print("\n--------------------------------------");
}

Credits

thor94ben
1 project • 0 followers
Contact

Comments

Please log in or sign up to comment.