Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <RCSwitch.h>
  2. #include <LowPower.h>
  3. #include <DHT.h> // DHT11 Temperature and Humidity Sensors Example include DHT library
  4. #define DHTPIN 10 //define as DHTPIN the Pin 3 used to connect the Sensor
  5. #define DHTTYPE DHT11 //define the sensor used(DHT11)
  6. DHT dht(DHTPIN, DHTTYPE); //create an instance of DHT
  7.  
  8. int led = 13; // the pin that the LED is atteched to
  9. int sensor = 12; // the pin that the sensor is atteched to
  10. int state = LOW; // by default, no motion detected
  11. int val = 0; // variable to store the sensor status (value)
  12. int device = 00;
  13. int counter= 0;
  14.  
  15.  
  16. RCSwitch mySwitch = RCSwitch();
  17.  
  18. void readPir();
  19. void readSensors();
  20. long readVoltage();
  21.  
  22. void setup() {
  23. Serial.begin(9600); // initialize serial
  24. mySwitch.enableTransmit(10);
  25. pinMode(led, OUTPUT); // initalize LED as an output
  26. pinMode(sensor, INPUT); // initialize sensor as an input
  27. dht.begin(); //initialize the Serial communication
  28. delay(500);
  29. }
  30.  
  31. void loop(){
  32. readPir();
  33. if(counter == 900){
  34. readSensors();
  35. counter = 0;
  36. }
  37. counter++;
  38. LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); // sleepmode 1 seconds
  39. }
  40.  
  41. void readPir(){
  42. val = digitalRead(sensor); // read sensor value
  43.  
  44. if (val == HIGH) { // check if the sensor is HIGH
  45. digitalWrite(led, HIGH); // turn LED ON
  46. delay(100); // delay 100 milliseconds
  47.  
  48. if (state == LOW) {
  49. digitalWrite(led, HIGH);
  50. mySwitch.send(device, 0);
  51. state = HIGH; // update variable state to HIGH
  52. }
  53. }
  54. else {
  55. digitalWrite(led, LOW); // turn LED OFF
  56.  
  57. if (state == HIGH){
  58. state = LOW; // update variable state to LOW
  59. }
  60. }
  61. }
  62.  
  63. void readSensors(){
  64. int h = average(0); // reading Humidity
  65. int t = average(1); // read Temperature as Celsius (the default)
  66. int l = average(2);
  67. int v = average(3);
  68.  
  69.  
  70. mySwitch.send(device << h << t << l << v, 24);
  71. std::cout << to_string(device) << to_string(h) << to_string(t) << to_string(l) << to_string(v) << std::endl;
  72. }
  73.  
  74. long readVoltage(){
  75. long result;
  76. // Read 1.1V reference against AVcc
  77. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  78. delay(2); // Wait for Vref to settle
  79. ADCSRA |= _BV(ADSC); // Convert
  80. while (bit_is_set(ADCSRA,ADSC));
  81. result = ADCL;
  82. result |= ADCH<<8;
  83. result = 1125300L / result; // Back-calculate AVcc in mV
  84. return result;
  85. }
  86.  
  87. int average(int value){
  88. int averArray[5];
  89. int sum;
  90.  
  91. for (int i = 0; i < 5; i = i + 1 ){
  92. if(value == 0)
  93. averArray[i] = dht.readHumidity();
  94. if (value = 1)
  95. averArray[i] = dht.readTemperature();
  96. if (value = 2)
  97. averArray[i] = analogRead(A1);
  98. if (value = 3)
  99. averArray[i] = readVoltage();
  100.  
  101. delay(250);
  102. sum = sum + averArray[i];
  103. }
  104.  
  105. return sum = sum / 5;
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122. #include <avr/io.h>
  123. #include <avr/interrupt.h>
  124. #include <thread>
  125.  
  126. int couter =0;
  127. int main(void)
  128. {
  129. // this code sets up timer1 for a 1s @ 16Mhz Clock (mode 4)
  130. OCR1A = 0x3D08;
  131. TCCR1B |= (1 << WGM12);// Mode 4, CTC on OCR1A
  132. TIMSK1 |= (1 << OCIE1A);//Set interrupt on compare match
  133. TCCR1B |= (1 << CS12) | (1 << CS10);// set prescaler to 1024 and start the timer
  134. sei();// enable interrupts
  135. while (1)
  136. {
  137. // we have a working Timer
  138. if(couter == 1800){
  139. setup();
  140. readSensors();
  141. delay(2000);
  142. readVoltage();
  143. counter = 0;
  144. }
  145. }
  146. }
  147.  
  148. ISR (TIMER1_COMPA_vect)
  149. {
  150. couter++;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement