Advertisement
seston

dht-motion-fadeled

Dec 14th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. /***
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * version 2 as published by the Free Software Foundation.
  5. *
  6. * DESCRIPTION
  7. * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
  8. * <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
  9. * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
  10. *
  11. * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
  12. * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
  13. * to the LED negative terminal and the MOSFET Source pin is connected to ground.
  14. *
  15. * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
  16. *
  17. * REVISION HISTORY
  18. * Version 1.0 - February 15, 2014 - Bruce Lacey
  19. * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
  20. * Version 1.1a - August 14, 2014 - Added MotionSensor (Ed)
  21. * Version 1.1b - December 22, 2014 - Added DHT-Sensor (Ed)
  22. * Version 1.1c - December 29, 2014 - Added LED timeout (Ed)
  23. * Version 1.1d - Januari 4, 2015 - Updated lib to 1.4.1 (Ed)
  24. *
  25. ***/
  26. #define SN "DimmableLED"
  27. #define SV "1.1d"
  28.  
  29. #include <MySensors.h>
  30. #include <SPI.h>
  31. #include <DHT.h>
  32.  
  33. #define Motion_PIN 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
  34. #define INTERRUPT Motion_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
  35. #define LED_PIN 5 // Arduino pin attached to MOSFET Gate pin
  36. #define DHT_PIN 4
  37. #define FADE_DELAY 30 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
  38. #define Motion_CHILD 1 // Id of the MotionSensor child
  39. #define Dimmer_CHILD 2 // Id of the DimmableLED child
  40. #define Hum_CHILD 3 // Id of the DHT-humidity child
  41. #define Temp_CHILD 4 // Id of the DHT-temperature child
  42. boolean lastMotion = false;
  43.  
  44. MySensors gw(9,10);
  45. DHT dht;
  46. float lastTemp;
  47. float lastHum;
  48. static int currentLevel = 0; // Current dim level...
  49. long previousMillisDHT = 0; // stores last time DHT was updated
  50. long previousMillisLED = 0; // stores last time LED was updated
  51. long intervalDHT = 60000; // read DHT every ....
  52. long durationLED = 1800000; // Max 'On'-time for LED's (watchdog)
  53. MyMessage motionMsg(Motion_CHILD, V_TRIPPED);
  54. MyMessage dimmerMsg(Dimmer_CHILD, V_DIMMER);
  55. MyMessage lightMsg(Dimmer_CHILD, V_LIGHT);
  56. MyMessage humMsg(Hum_CHILD, V_HUM);
  57. MyMessage tempMsg(Temp_CHILD, V_TEMP);
  58.  
  59. void setup()
  60. {
  61. // Serial.println( SN );
  62. gw.begin( incomingMessage ); // Listen to dimmer-info from Vera
  63. dht.setup(DHT_PIN);
  64.  
  65. // Register all sensors to gw (they will be created as child devices)
  66. gw.present( Motion_CHILD, S_MOTION );
  67. gw.present( Dimmer_CHILD, S_DIMMER );
  68. gw.present( Hum_CHILD, S_HUM );
  69. gw.present( Temp_CHILD, S_TEMP );
  70.  
  71. gw.sendSketchInfo(SN, SV);
  72. // Pull the gateway's current dim level - restore light level upon sender node power-up
  73. gw.request( Dimmer_CHILD, V_DIMMER );
  74. }
  75.  
  76.  
  77. void loop()
  78. {
  79. gw.process();
  80.  
  81. // Read digital motion value
  82. boolean motion = digitalRead(Motion_PIN) == HIGH;
  83. if (lastMotion != motion) {
  84. lastMotion = motion;
  85. gw.send(motionMsg.set(motion ? "1" : "0" )); // Send motion value to gw
  86. }
  87.  
  88. /*
  89. // DHT22:
  90. unsigned long currentMillisDHT = millis();
  91. if(currentMillisDHT - previousMillisDHT > intervalDHT) {
  92. previousMillisDHT = currentMillisDHT;
  93.  
  94. float temperature = dht.getTemperature();
  95. if (isnan(temperature)) {
  96. Serial.println("Failed reading temperature from DHT");
  97. } else if (temperature != lastTemp) {
  98. lastTemp = temperature;
  99.  
  100. gw.send(tempMsg.set(temperature, 1));
  101. Serial.print("T: ");
  102. Serial.println(temperature);
  103. }
  104.  
  105. float humidity = dht.getHumidity();
  106. if (isnan(humidity)) {
  107. Serial.println("Failed reading humidity from DHT");
  108. } else if (humidity != lastHum) {
  109. lastHum = humidity;
  110. gw.send(humMsg.set(humidity, 1));
  111. Serial.print("H: ");
  112. Serial.println(humidity);
  113. }
  114. }
  115. */
  116.  
  117. // Limit LED 'On'-time.
  118. unsigned long currentMillis = millis();
  119.  
  120. if (( currentLevel > 0 ) && ((currentMillis - previousMillisLED) > durationLED)) {
  121. analogWrite( LED_PIN, 0 ); // Kill LED's after max 'On'-tijd
  122. gw.send(dimmerMsg.set(0));
  123. currentLevel=0;
  124. }
  125.  
  126. }
  127.  
  128.  
  129. void incomingMessage(const MyMessage &message) {
  130. if (message.type == V_LIGHT || message.type == V_DIMMER) {
  131.  
  132. // Retrieve the power or dim level from the incoming request message
  133. int requestedLevel = atoi( message.data );
  134.  
  135. // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
  136. requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
  137.  
  138. // Clip incoming level to valid range of 0 to 100
  139. requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
  140. requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
  141.  
  142. // Serial.print( "Changing level from " );
  143. // Serial.print( currentLevel );
  144. // Serial.print( " to " );
  145. // Serial.println( requestedLevel );
  146.  
  147. fadeToLevel( requestedLevel );
  148.  
  149. // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
  150. // gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
  151.  
  152. }
  153. }
  154.  
  155. // This method provides a graceful fade up/down effect
  156. void fadeToLevel( int toLevel ) {
  157. if (toLevel > 0){ previousMillisLED = millis();}
  158. int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
  159.  
  160. while ( currentLevel != toLevel ) {
  161. currentLevel += delta;
  162. analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
  163. delay( FADE_DELAY );
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement