Advertisement
Guest User

arduino evsey a code

a guest
Mar 26th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. #include <dht.h>
  2.  
  3. /////////////////////////////
  4. //VARS
  5. //the time we give the sensor to calibrate (10-60 secs according to the datasheet)
  6. int calibrationTime = 30;
  7.  
  8. //the time when the sensor outputs a low impulse
  9. long unsigned int lowIn;
  10.  
  11. //the amount of milliseconds the sensor has to be low
  12. //before we assume all motion has stopped
  13. long unsigned int pause = 5000;
  14.  
  15. unsigned long pmc = 0; //timer var for CTS
  16. unsigned long pmd = 0; //timer var for DHT sensor
  17.  
  18. boolean lockLow = true;
  19. boolean takeLowTime;
  20.  
  21. //PIN VARS
  22. dht DHT;
  23. int ctsPin = 2;         //the digital pin connected to the capacitive touch sensor's output
  24. int laserPin = 3;       //the digital pin connected to the laser's input
  25. int pirPin = 4;         //the digital pin connected to the PIR sensor's output
  26. int dhtPin = 5;         //the digital pin connected to the DHT sensor's output
  27. int buzzerPin = 6;      //the digital pin connected to the buzzer's input
  28. int piroffledPin = 7;   //the digital pin connected to the led that will be active when NO motion detected
  29. int pironledPin = 8;    //the digital pin connected to the led that will be active when motion IS detected
  30. int ctsoffledPin = 9;   //the digital pin connected to the led that will be active when the capacitive touch sensor is NOT pressed
  31. int ctsonledPin = 10;   //the digital pin connected to the led that will be active when the capacitive touch sensor IS pressed
  32. int phhighPin = 11;     //the digital pin connected to the led that will be active when the lightning is bright
  33. int phlowPin = 12;      //the digital pin connected to the led that will be active when the lightning is dim
  34. int ledPin = 13;        //the arduino light pin
  35. #define DHT11_PIN dhtPin
  36. //SENSOR VARS
  37. int ctsValue; //the value for the capacitive touch sensor will be stored here
  38.  
  39.  
  40. /////////////////////////////
  41. //SETUP
  42. void setup() {
  43.   Serial.begin(9600); //enabling serial
  44.   pinMode(ctsPin, INPUT);
  45.   pinMode(laserPin, OUTPUT);
  46.   pinMode(pirPin, INPUT);
  47.   pinMode(dhtPin, INPUT);
  48.   pinMode(buzzerPin, OUTPUT);
  49.   pinMode(piroffledPin, OUTPUT);
  50.   pinMode(pironledPin, OUTPUT);
  51.   pinMode(ctsoffledPin, OUTPUT);
  52.   pinMode(ctsonledPin, OUTPUT);
  53.   pinMode(phhighPin, OUTPUT);
  54.   pinMode(phlowPin, OUTPUT);
  55.   pinMode(ledPin, OUTPUT);
  56.   pinMode(A0, INPUT);
  57.   digitalWrite(pirPin, LOW);
  58.   digitalWrite(piroffledPin, HIGH);
  59.   digitalWrite(pironledPin, LOW);
  60.   digitalWrite(ctsoffledPin, HIGH);
  61.   digitalWrite(ctsonledPin, LOW);
  62.   digitalWrite(laserPin, LOW);
  63.   digitalWrite(phhighPin, HIGH);
  64.   digitalWrite(phlowPin, LOW);
  65.  
  66.   //give the sensor some time to calibrate
  67.   Serial.print("calibrating sensor ");
  68.   for (int i = 0; i < calibrationTime; i++) {
  69.     Serial.print(".");
  70.     delay(1000);
  71.   }
  72.   Serial.println(" done");
  73.   Serial.println("SENSOR ACTIVE");
  74.   delay(50);
  75. }
  76.  
  77.  
  78. ////////////////////////////
  79. //LOOP
  80. void loop() {
  81.   if (analogRead(A0) < 700) { //if the lightning level is dim, turn on one of the red leds
  82.     digitalWrite(phhighPin, LOW);
  83.     digitalWrite(phlowPin, HIGH);
  84.   }
  85.   else { //turn on one of the green leds otherwise
  86.     digitalWrite(phhighPin, HIGH);
  87.     digitalWrite(phlowPin, LOW);
  88.   }
  89.   if (millis() - pmd >= 2500) { //DHT and photoresistor readings are here
  90.     int chk = DHT.read11(DHT11_PIN);
  91.     Serial.println("----------");
  92.     Serial.print("Temperature = ");
  93.     Serial.println(DHT.temperature);
  94.     Serial.print("Humidity = ");
  95.     Serial.println(DHT.humidity);
  96.     Serial.print("Lightning level: ");
  97.     Serial.println(analogRead(A0));
  98.     Serial.println("----------");
  99.     pmd = millis();
  100.   }
  101.   ctsValue = digitalRead(ctsPin);
  102.   if (ctsValue == HIGH) { //if the capacitive touch sensor button is being touched, turn on one of the red leds
  103.     digitalWrite(ctsonledPin, HIGH);
  104.     digitalWrite(ctsoffledPin, LOW);
  105.     if (millis() - pmc >= 1000) {
  106.       Serial.println("Capacitive touch sensor is being touched!");
  107.       pmc = millis();
  108.     }
  109.   }
  110.   else { //turn on one of the green leds otherwise
  111.     digitalWrite(ctsoffledPin, HIGH);
  112.     digitalWrite(ctsonledPin, LOW);
  113.   }
  114.   if (digitalRead(pirPin) == HIGH) { //if the PIR sensor detects movement, turn on one of the red leds, turn on the laser and the buzzer
  115.     digitalWrite(ledPin, HIGH);   //the led visualizes the sensors output pin state
  116.     if (lockLow) {
  117.       //makes sure we wait for a transition to LOW before any further output is made:
  118.       lockLow = false;
  119.       digitalWrite(laserPin, HIGH);
  120.       digitalWrite(pironledPin, HIGH);
  121.       digitalWrite(piroffledPin, LOW);
  122.       tone(buzzerPin, 1000);
  123.       Serial.println("---");
  124.       Serial.print("motion detected at ");
  125.       Serial.print(millis() / 1000);
  126.       Serial.println(" sec");
  127.       delay(50);
  128.     }
  129.     takeLowTime = true;
  130.   }
  131.  
  132.   if (digitalRead(pirPin) == LOW) {
  133.     digitalWrite(ledPin, LOW);  //the led visualizes the sensors output pin state
  134.  
  135.     if (takeLowTime) {
  136.       lowIn = millis();          //save the time of the transition from high to LOW
  137.       takeLowTime = false;       //make sure this is only done at the start of a LOW phase
  138.     }
  139.     //if the sensor is low for more than the given pause,
  140.     //we assume that no more motion is going to happen
  141.     if (!lockLow && millis() - lowIn > pause) { // after the motion ended - turn off the laser, turn on one of the green leds, and disable the buzzer
  142.       //makes sure this block of code is only executed again after
  143.       //a new motion sequence has been detected
  144.       lockLow = true;
  145.       digitalWrite(laserPin, LOW);
  146.       digitalWrite(pironledPin, LOW);
  147.       digitalWrite(piroffledPin, HIGH);
  148.       noTone(buzzerPin);
  149.       Serial.print("motion ended at ");      //output
  150.       Serial.print((millis() - pause) / 1000);
  151.       Serial.println(" sec");
  152.       delay(50);
  153.     }
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement