Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <dht.h>
- /////////////////////////////
- //VARS
- //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
- int calibrationTime = 30;
- //the time when the sensor outputs a low impulse
- long unsigned int lowIn;
- //the amount of milliseconds the sensor has to be low
- //before we assume all motion has stopped
- long unsigned int pause = 5000;
- unsigned long pmc = 0; //timer var for CTS
- unsigned long pmd = 0; //timer var for DHT sensor
- boolean lockLow = true;
- boolean takeLowTime;
- //PIN VARS
- dht DHT;
- int ctsPin = 2; //the digital pin connected to the capacitive touch sensor's output
- int laserPin = 3; //the digital pin connected to the laser's input
- int pirPin = 4; //the digital pin connected to the PIR sensor's output
- int dhtPin = 5; //the digital pin connected to the DHT sensor's output
- int buzzerPin = 6; //the digital pin connected to the buzzer's input
- int piroffledPin = 7; //the digital pin connected to the led that will be active when NO motion detected
- int pironledPin = 8; //the digital pin connected to the led that will be active when motion IS detected
- int ctsoffledPin = 9; //the digital pin connected to the led that will be active when the capacitive touch sensor is NOT pressed
- int ctsonledPin = 10; //the digital pin connected to the led that will be active when the capacitive touch sensor IS pressed
- int phhighPin = 11; //the digital pin connected to the led that will be active when the lightning is bright
- int phlowPin = 12; //the digital pin connected to the led that will be active when the lightning is dim
- int ledPin = 13; //the arduino light pin
- #define DHT11_PIN dhtPin
- //SENSOR VARS
- int ctsValue; //the value for the capacitive touch sensor will be stored here
- /////////////////////////////
- //SETUP
- void setup() {
- Serial.begin(9600); //enabling serial
- pinMode(ctsPin, INPUT);
- pinMode(laserPin, OUTPUT);
- pinMode(pirPin, INPUT);
- pinMode(dhtPin, INPUT);
- pinMode(buzzerPin, OUTPUT);
- pinMode(piroffledPin, OUTPUT);
- pinMode(pironledPin, OUTPUT);
- pinMode(ctsoffledPin, OUTPUT);
- pinMode(ctsonledPin, OUTPUT);
- pinMode(phhighPin, OUTPUT);
- pinMode(phlowPin, OUTPUT);
- pinMode(ledPin, OUTPUT);
- pinMode(A0, INPUT);
- digitalWrite(pirPin, LOW);
- digitalWrite(piroffledPin, HIGH);
- digitalWrite(pironledPin, LOW);
- digitalWrite(ctsoffledPin, HIGH);
- digitalWrite(ctsonledPin, LOW);
- digitalWrite(laserPin, LOW);
- digitalWrite(phhighPin, HIGH);
- digitalWrite(phlowPin, LOW);
- //give the sensor some time to calibrate
- Serial.print("calibrating sensor ");
- for (int i = 0; i < calibrationTime; i++) {
- Serial.print(".");
- delay(1000);
- }
- Serial.println(" done");
- Serial.println("SENSOR ACTIVE");
- delay(50);
- }
- ////////////////////////////
- //LOOP
- void loop() {
- if (analogRead(A0) < 700) { //if the lightning level is dim, turn on one of the red leds
- digitalWrite(phhighPin, LOW);
- digitalWrite(phlowPin, HIGH);
- }
- else { //turn on one of the green leds otherwise
- digitalWrite(phhighPin, HIGH);
- digitalWrite(phlowPin, LOW);
- }
- if (millis() - pmd >= 2500) { //DHT and photoresistor readings are here
- int chk = DHT.read11(DHT11_PIN);
- Serial.println("----------");
- Serial.print("Temperature = ");
- Serial.println(DHT.temperature);
- Serial.print("Humidity = ");
- Serial.println(DHT.humidity);
- Serial.print("Lightning level: ");
- Serial.println(analogRead(A0));
- Serial.println("----------");
- pmd = millis();
- }
- ctsValue = digitalRead(ctsPin);
- if (ctsValue == HIGH) { //if the capacitive touch sensor button is being touched, turn on one of the red leds
- digitalWrite(ctsonledPin, HIGH);
- digitalWrite(ctsoffledPin, LOW);
- if (millis() - pmc >= 1000) {
- Serial.println("Capacitive touch sensor is being touched!");
- pmc = millis();
- }
- }
- else { //turn on one of the green leds otherwise
- digitalWrite(ctsoffledPin, HIGH);
- digitalWrite(ctsonledPin, LOW);
- }
- if (digitalRead(pirPin) == HIGH) { //if the PIR sensor detects movement, turn on one of the red leds, turn on the laser and the buzzer
- digitalWrite(ledPin, HIGH); //the led visualizes the sensors output pin state
- if (lockLow) {
- //makes sure we wait for a transition to LOW before any further output is made:
- lockLow = false;
- digitalWrite(laserPin, HIGH);
- digitalWrite(pironledPin, HIGH);
- digitalWrite(piroffledPin, LOW);
- tone(buzzerPin, 1000);
- Serial.println("---");
- Serial.print("motion detected at ");
- Serial.print(millis() / 1000);
- Serial.println(" sec");
- delay(50);
- }
- takeLowTime = true;
- }
- if (digitalRead(pirPin) == LOW) {
- digitalWrite(ledPin, LOW); //the led visualizes the sensors output pin state
- if (takeLowTime) {
- lowIn = millis(); //save the time of the transition from high to LOW
- takeLowTime = false; //make sure this is only done at the start of a LOW phase
- }
- //if the sensor is low for more than the given pause,
- //we assume that no more motion is going to happen
- if (!lockLow && millis() - lowIn > pause) { // after the motion ended - turn off the laser, turn on one of the green leds, and disable the buzzer
- //makes sure this block of code is only executed again after
- //a new motion sequence has been detected
- lockLow = true;
- digitalWrite(laserPin, LOW);
- digitalWrite(pironledPin, LOW);
- digitalWrite(piroffledPin, HIGH);
- noTone(buzzerPin);
- Serial.print("motion ended at "); //output
- Serial.print((millis() - pause) / 1000);
- Serial.println(" sec");
- delay(50);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement