Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
- *
- * DESCRIPTION
- * This sketch provides a Dimmable LED Light using PWM and based Henrik Ekblad
- * <henrik.ekblad@gmail.com> Vera Arduino Sensor project.
- * Developed by Bruce Lacey, inspired by Hek's MySensor's example sketches.
- *
- * The circuit uses a MOSFET for Pulse-Wave-Modulation to dim the attached LED or LED strip.
- * The MOSFET Gate pin is connected to Arduino pin 3 (LED_PIN), the MOSFET Drain pin is connected
- * to the LED negative terminal and the MOSFET Source pin is connected to ground.
- *
- * This sketch is extensible to support more than one MOSFET/PWM dimmer per circuit.
- *
- * REVISION HISTORY
- * Version 1.0 - February 15, 2014 - Bruce Lacey
- * Version 1.1 - August 13, 2014 - Converted to 1.4 (hek)
- * Version 1.1a - August 14, 2014 - Added MotionSensor (Ed)
- * Version 1.1b - December 22, 2014 - Added DHT-Sensor (Ed)
- * Version 1.1c - December 29, 2014 - Added LED timeout (Ed)
- * Version 1.1d - Januari 4, 2015 - Updated lib to 1.4.1 (Ed)
- *
- ***/
- #define SN "DimmableLED"
- #define SV "1.1d"
- #include <MySensors.h>
- #include <SPI.h>
- #include <DHT.h>
- #define Motion_PIN 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
- #define INTERRUPT Motion_PIN-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
- #define LED_PIN 5 // Arduino pin attached to MOSFET Gate pin
- #define DHT_PIN 4
- #define FADE_DELAY 30 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
- #define Motion_CHILD 1 // Id of the MotionSensor child
- #define Dimmer_CHILD 2 // Id of the DimmableLED child
- #define Hum_CHILD 3 // Id of the DHT-humidity child
- #define Temp_CHILD 4 // Id of the DHT-temperature child
- boolean lastMotion = false;
- MySensors gw(9,10);
- DHT dht;
- float lastTemp;
- float lastHum;
- static int currentLevel = 0; // Current dim level...
- long previousMillisDHT = 0; // stores last time DHT was updated
- long previousMillisLED = 0; // stores last time LED was updated
- long intervalDHT = 60000; // read DHT every ....
- long durationLED = 1800000; // Max 'On'-time for LED's (watchdog)
- MyMessage motionMsg(Motion_CHILD, V_TRIPPED);
- MyMessage dimmerMsg(Dimmer_CHILD, V_DIMMER);
- MyMessage lightMsg(Dimmer_CHILD, V_LIGHT);
- MyMessage humMsg(Hum_CHILD, V_HUM);
- MyMessage tempMsg(Temp_CHILD, V_TEMP);
- void setup()
- {
- // Serial.println( SN );
- gw.begin( incomingMessage ); // Listen to dimmer-info from Vera
- dht.setup(DHT_PIN);
- // Register all sensors to gw (they will be created as child devices)
- gw.present( Motion_CHILD, S_MOTION );
- gw.present( Dimmer_CHILD, S_DIMMER );
- gw.present( Hum_CHILD, S_HUM );
- gw.present( Temp_CHILD, S_TEMP );
- gw.sendSketchInfo(SN, SV);
- // Pull the gateway's current dim level - restore light level upon sender node power-up
- gw.request( Dimmer_CHILD, V_DIMMER );
- }
- void loop()
- {
- gw.process();
- // Read digital motion value
- boolean motion = digitalRead(Motion_PIN) == HIGH;
- if (lastMotion != motion) {
- lastMotion = motion;
- gw.send(motionMsg.set(motion ? "1" : "0" )); // Send motion value to gw
- }
- /*
- // DHT22:
- unsigned long currentMillisDHT = millis();
- if(currentMillisDHT - previousMillisDHT > intervalDHT) {
- previousMillisDHT = currentMillisDHT;
- float temperature = dht.getTemperature();
- if (isnan(temperature)) {
- Serial.println("Failed reading temperature from DHT");
- } else if (temperature != lastTemp) {
- lastTemp = temperature;
- gw.send(tempMsg.set(temperature, 1));
- Serial.print("T: ");
- Serial.println(temperature);
- }
- float humidity = dht.getHumidity();
- if (isnan(humidity)) {
- Serial.println("Failed reading humidity from DHT");
- } else if (humidity != lastHum) {
- lastHum = humidity;
- gw.send(humMsg.set(humidity, 1));
- Serial.print("H: ");
- Serial.println(humidity);
- }
- }
- */
- // Limit LED 'On'-time.
- unsigned long currentMillis = millis();
- if (( currentLevel > 0 ) && ((currentMillis - previousMillisLED) > durationLED)) {
- analogWrite( LED_PIN, 0 ); // Kill LED's after max 'On'-tijd
- gw.send(dimmerMsg.set(0));
- currentLevel=0;
- }
- }
- void incomingMessage(const MyMessage &message) {
- if (message.type == V_LIGHT || message.type == V_DIMMER) {
- // Retrieve the power or dim level from the incoming request message
- int requestedLevel = atoi( message.data );
- // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
- requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
- // Clip incoming level to valid range of 0 to 100
- requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
- requestedLevel = requestedLevel < 0 ? 0 : requestedLevel;
- // Serial.print( "Changing level from " );
- // Serial.print( currentLevel );
- // Serial.print( " to " );
- // Serial.println( requestedLevel );
- fadeToLevel( requestedLevel );
- // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
- // gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
- }
- }
- // This method provides a graceful fade up/down effect
- void fadeToLevel( int toLevel ) {
- if (toLevel > 0){ previousMillisLED = millis();}
- int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
- while ( currentLevel != toLevel ) {
- currentLevel += delta;
- analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
- delay( FADE_DELAY );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement