Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. My Project
  3. Backlit Dimmable LED Mirror with Motion Sensor

Backlit Dimmable LED Mirror with Motion Sensor

Scheduled Pinned Locked Moved My Project
43 Posts 12 Posters 30.9k Views 14 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • petewillP Offline
    petewillP Offline
    petewill
    Admin
    wrote on last edited by
    #1

    Backlit-Mirror.jpg
    Hi Everyone,

    I have created backlit mirror based on Bruce Lacey's dimmable LED sketch. I have added a motion sensor and some on/off and fade up/down buttons. There is some logic in the code to save the dim level when the buttons are pressed. I have all my motion turn on/off logic in my Vera controller using PLEG. It could be easily adapted to control the on/off functionality in the Arduino code but I like to have my automation logic in one place (my Vera).

    This was a fun project to work on and although it looks pretty ugly from the back my wife loves the way it looks in our master bathroom. I tried to document as best I could but please let me know if you have any questions.

    Here is a list of the parts I used

    • Items from MySensors Store http://www.mysensors.org/store/
      • Female Pin Header Connector Strip
      • Prototype Universal Printed Circuit Boards (PCB)
      • NRF24L01 Radio
      • Arduino Pro Mini
      • FTDI USB to TTL Serial Adapter
      • Capacitors (10uf and .1uf)
      • 3.3v voltage regulator
      • 5v voltage regulator
      • IRLZ44N Logic Level Transistor MOSFET
      • 12v Transformer (power supply)
      • 5 Meter LED Strip (I used 3528)
      • HC-SR501 PIR Motion Sensor Module
    • 22-24 gauge wire or similar (I used Cat5/Cat6 cable)
    • 2 Pole 5mm Pitch PCB Mount Screw Terminal Block

    Here is a video explaining how to build it yourself.
    http://youtu.be/jblaMddRDpc

    Mirror LED Wiring Diagram.png
    Here is the Fritzing (http://fritzing.org/) wiring diagram file if you want to view in more detail: Mirror LED Wiring Diagram.fzz
    https://codebender.cc/sketch:81486

    20150203_201003-web.jpg
    20150203_195706-web.jpg

    My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

    DwaltD RJ_MakeR H 3 Replies Last reply
    6
    • petewillP petewill

      Backlit-Mirror.jpg
      Hi Everyone,

      I have created backlit mirror based on Bruce Lacey's dimmable LED sketch. I have added a motion sensor and some on/off and fade up/down buttons. There is some logic in the code to save the dim level when the buttons are pressed. I have all my motion turn on/off logic in my Vera controller using PLEG. It could be easily adapted to control the on/off functionality in the Arduino code but I like to have my automation logic in one place (my Vera).

      This was a fun project to work on and although it looks pretty ugly from the back my wife loves the way it looks in our master bathroom. I tried to document as best I could but please let me know if you have any questions.

      Here is a list of the parts I used

      • Items from MySensors Store http://www.mysensors.org/store/
        • Female Pin Header Connector Strip
        • Prototype Universal Printed Circuit Boards (PCB)
        • NRF24L01 Radio
        • Arduino Pro Mini
        • FTDI USB to TTL Serial Adapter
        • Capacitors (10uf and .1uf)
        • 3.3v voltage regulator
        • 5v voltage regulator
        • IRLZ44N Logic Level Transistor MOSFET
        • 12v Transformer (power supply)
        • 5 Meter LED Strip (I used 3528)
        • HC-SR501 PIR Motion Sensor Module
      • 22-24 gauge wire or similar (I used Cat5/Cat6 cable)
      • 2 Pole 5mm Pitch PCB Mount Screw Terminal Block

      Here is a video explaining how to build it yourself.
      http://youtu.be/jblaMddRDpc

      Mirror LED Wiring Diagram.png
      Here is the Fritzing (http://fritzing.org/) wiring diagram file if you want to view in more detail: Mirror LED Wiring Diagram.fzz
      https://codebender.cc/sketch:81486

      20150203_201003-web.jpg
      20150203_195706-web.jpg

      DwaltD Offline
      DwaltD Offline
      Dwalt
      wrote on last edited by
      #2

      @petewill
      Nice!

      I am curious about your power supply setup, do you mind sharing? e.g. Are you are running DC line through the attic (or crawlspace) and dropping within the walls? 12VDC? How many feeds, how long are your runs and what gauge wire are you using? I keep looking at my long dormant telephony wiring and wondering "what if...?"

      -Dwalt

      Veralite UI5 :: IBoard Ethernet GW :: MyS 1.5

      petewillP 1 Reply Last reply
      0
      • DwaltD Dwalt

        @petewill
        Nice!

        I am curious about your power supply setup, do you mind sharing? e.g. Are you are running DC line through the attic (or crawlspace) and dropping within the walls? 12VDC? How many feeds, how long are your runs and what gauge wire are you using? I keep looking at my long dormant telephony wiring and wondering "what if...?"

        -Dwalt

        petewillP Offline
        petewillP Offline
        petewill
        Admin
        wrote on last edited by
        #3

        @Dwalt Thanks. Yep, I'm running DC through the walls/attic with 16 and 18 gauge wire (depending on amperage requirements). I think I have around 18 feeds of either 5v or 12v right now. I'm powering lights, blinds and MySensors devices. The longest length of wire is around 60 ft. I am lucky to have access to almost all my rooms via the basement or attic so for the most part it's relatively easy to pull lines.

        What gauge are your phone wires? Depending on gauge you could probably run some low draw sensors like motion, temp/humidity or door/window sensors.

        My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

        1 Reply Last reply
        0
        • FrancoisF Offline
          FrancoisF Offline
          Francois
          wrote on last edited by Francois
          #4

          @petewill under what version of mysensor library did you compile this sketch library 1.4 or 1.4.1? I compile you sketch under 1.4.1 and cannot get it to work

          Francois

          petewillP hekH 2 Replies Last reply
          0
          • FrancoisF Francois

            @petewill under what version of mysensor library did you compile this sketch library 1.4 or 1.4.1? I compile you sketch under 1.4.1 and cannot get it to work

            Francois

            petewillP Offline
            petewillP Offline
            petewill
            Admin
            wrote on last edited by
            #5

            @Francois I'm still using 1.4.

            My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

            1 Reply Last reply
            0
            • FrancoisF Francois

              @petewill under what version of mysensor library did you compile this sketch library 1.4 or 1.4.1? I compile you sketch under 1.4.1 and cannot get it to work

              Francois

              hekH Online
              hekH Online
              hek
              Admin
              wrote on last edited by
              #6

              @Francois said:

              I compile you sketch under 1.4.1 and cannot get it to work

              What compile error do you see? Did you use 1.4.1 on both GW and sensor?

              1 Reply Last reply
              0
              • petewillP petewill

                Backlit-Mirror.jpg
                Hi Everyone,

                I have created backlit mirror based on Bruce Lacey's dimmable LED sketch. I have added a motion sensor and some on/off and fade up/down buttons. There is some logic in the code to save the dim level when the buttons are pressed. I have all my motion turn on/off logic in my Vera controller using PLEG. It could be easily adapted to control the on/off functionality in the Arduino code but I like to have my automation logic in one place (my Vera).

                This was a fun project to work on and although it looks pretty ugly from the back my wife loves the way it looks in our master bathroom. I tried to document as best I could but please let me know if you have any questions.

                Here is a list of the parts I used

                • Items from MySensors Store http://www.mysensors.org/store/
                  • Female Pin Header Connector Strip
                  • Prototype Universal Printed Circuit Boards (PCB)
                  • NRF24L01 Radio
                  • Arduino Pro Mini
                  • FTDI USB to TTL Serial Adapter
                  • Capacitors (10uf and .1uf)
                  • 3.3v voltage regulator
                  • 5v voltage regulator
                  • IRLZ44N Logic Level Transistor MOSFET
                  • 12v Transformer (power supply)
                  • 5 Meter LED Strip (I used 3528)
                  • HC-SR501 PIR Motion Sensor Module
                • 22-24 gauge wire or similar (I used Cat5/Cat6 cable)
                • 2 Pole 5mm Pitch PCB Mount Screw Terminal Block

                Here is a video explaining how to build it yourself.
                http://youtu.be/jblaMddRDpc

                Mirror LED Wiring Diagram.png
                Here is the Fritzing (http://fritzing.org/) wiring diagram file if you want to view in more detail: Mirror LED Wiring Diagram.fzz
                https://codebender.cc/sketch:81486

                20150203_201003-web.jpg
                20150203_195706-web.jpg

                RJ_MakeR Offline
                RJ_MakeR Offline
                RJ_Make
                Hero Member
                wrote on last edited by
                #7

                @petewill Somehow I missed this.. NICE JOB Pete!! I love it!!

                RJ_Make

                1 Reply Last reply
                0
                • RJ_MakeR Offline
                  RJ_MakeR Offline
                  RJ_Make
                  Hero Member
                  wrote on last edited by
                  #8

                  @petewill Also what kind of heat are you getting off that FET?

                  RJ_Make

                  1 Reply Last reply
                  0
                  • petewillP Offline
                    petewillP Offline
                    petewill
                    Admin
                    wrote on last edited by
                    #9

                    @ServiceXp thanks! In my setup I'm not getting any noticeable heat on any of my LED projects. I have run it for hours at 100% and it's not even warm to the touch.

                    My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                    1 Reply Last reply
                    0
                    • BulldogLowellB Offline
                      BulldogLowellB Offline
                      BulldogLowell
                      Contest Winner
                      wrote on last edited by
                      #10

                      this is one of those "I want one" projects, yes?

                      a lot of cool ways to apply this.

                      1 Reply Last reply
                      0
                      • FrancoisF Offline
                        FrancoisF Offline
                        Francois
                        wrote on last edited by
                        #11

                        @hek Yes I am using 1.4.1 on both the sensor. This the error I am getting on the sensor node: The sensor was working on a different node it also has cap on VCC and GRD.

                        sensor started, id=25, parent=0, distance=2
                        send: 25-25-0-0 s=255,c=0,t=17,pt=0,l=5,st=fail:1.4.1
                        send: 25-25-0-0 s=255,c=3,t=6,pt=1,l=1,st=fail:0
                        read: 0-0-25 s=255,c=3,t=6,pt=0,l=1:M
                        send: 25-25-0-0 s=0,c=0,t=4,pt=0,l=5,st=fail:1.4.1
                        send: 25-25-0-0 s=1,c=0,t=1,pt=0,l=5,st=fail:1.4.1
                        send: 25-25-0-0 s=255,c=3,t=11,pt=0,l=10,st=fail:Mirror LED
                        lost parent
                        find parent
                        send: 25-25-255-255 s=255,c=3,t=7,pt=0,l=0,st=bc:
                        read: 0-0-25 s=255,c=3,t=8,pt=1,l=1:1
                        new parent=0, d=2

                        1 Reply Last reply
                        0
                        • hekH Online
                          hekH Online
                          hek
                          Admin
                          wrote on last edited by
                          #12

                          Strange. Looks like distance to gw is 2 but you're actually communicating to it directly.

                          1 Reply Last reply
                          0
                          • FrancoisF Offline
                            FrancoisF Offline
                            Francois
                            wrote on last edited by Francois
                            #13

                            @hek this is the sketch from about that that copy and recompile on 1.4.1. I did not used the codebender to compile the sketch.

                            This is the serial info on the MQTT Gateway:
                            0;0;3;0;9;read: 25-25-0 s=255,c=0,t=17,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=255,c=3,t=6,pt=1,l=1:0
                            0;0;3;0;9;send: 0-0-25-25 s=255,c=3,t=6,pt=0,l=1,st=ok:M
                            0;0;3;0;9;read: 25-25-0 s=0,c=0,t=4,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=1,c=0,t=1,pt=0,l=5:1.4.1
                            0;0;3;0;9;read: 25-25-0 s=255,c=3,t=11,pt=0,l=10:Mirror LED
                            MyMQTT/25/255/V_SKETCH_NAME
                            30 27 00 1B 4D 79 4D 51 54 54 2F 32 35 2F 32 35 35 2F 56 5F 53 4B 45 54 43 48 5F 4E 41 4D 45 4D 69 72 72 6F 72 20 4C 45 44
                            0;0;3;0;9;read: 25-25-255 s=255,c=3,t=7,pt=0,l=0:
                            0;0;3;0;9;send: 0-0-25-25 s=255,c=3,t=8,pt=1,l=1,st=ok:1
                            0;0;3;0;9;read: 0-25-25 s=255,c=3,t=8,pt=1,l=1:1
                            0;0;3;0;9;send: 0-0-25-25 s=255,c=3,t=8,pt=1,l=1,st=ok:1

                            1 Reply Last reply
                            0
                            • FrancoisF Offline
                              FrancoisF Offline
                              Francois
                              wrote on last edited by Francois
                              #14

                              Hi I got this sketch to work. There must have being something wrong with MySensor library 1.4.1 once I re-install it and recompile the sketch it is working.

                              petewillP 1 Reply Last reply
                              0
                              • FrancoisF Francois

                                Hi I got this sketch to work. There must have being something wrong with MySensor library 1.4.1 once I re-install it and recompile the sketch it is working.

                                petewillP Offline
                                petewillP Offline
                                petewill
                                Admin
                                wrote on last edited by
                                #15

                                @Francois Great to hear and thanks for letting us know!

                                My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                                1 Reply Last reply
                                0
                                • FrancoisF Offline
                                  FrancoisF Offline
                                  Francois
                                  wrote on last edited by Francois
                                  #16

                                  @petewill I add a light sensor to your sketch and this now slow everything down what will the reason for this be? The motion sensor will not always pickup motion. I have another node with light and motion sensor and that is working right.

                                  Here is the code:

                                  /***
                                   * 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 (MIRROR_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 - February 4, 2014 - Pete B - added buttons to control on/off and dim level and a motion sensor
                                   ***/
                                   
                                   
                                  #define SN "Mirror LED with PIR and LDR"
                                  #define SV "1.2"
                                  #define NODE_ID 50  //change to a number to assign a specific ID
                                  
                                  #include <SPI.h>
                                  #include <MySensor.h>
                                  #include <Bounce2.h>
                                  #include <MySigningNone.h>
                                  #include <MyTransportNRF24.h>
                                  #include <MyHwATMega328.h>
                                  
                                  #define MIRROR_LED_CHILD 0    //ID of the LED child
                                  #define MOTION_CHILD 1  //ID of the motion sensor child
                                  #define CHILD_ID_LIGHT 2 //ID for LDR sensor
                                  
                                  #define LIGHT_SENSOR_ANALOG_PIN 0
                                  
                                  // NRFRF24L01 radio driver (set low transmit power by default) 
                                  MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
                                  
                                  #define MIRROR_LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
                                  #define UP_BUTTON_PIN 8  // Arduino Digital I/O pin number for the fade up button 
                                  #define DOWN_BUTTON_PIN 7  // Arduino Digital I/O pin number for the fade down button 
                                  #define POWER_BUTTON_PIN 4  // Arduino Digital I/O pin number for the power button 
                                  #define MOTION_PIN  6  // Arduino pin tied to trigger pin on the motion sensor.
                                  
                                  #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
                                  #define FADE_PERCENTAGE 10 //The percentage the fade level will be changed when a button is pressed
                                  
                                  MyHwATMega328 hw;
                                  // Construct MySensors library
                                  MySensor gw(radio, hw);
                                  
                                  //MySensor gw; //Don't need to define pins unless they are changing from the default
                                  
                                  static int currentLevel = 0;  // Current dim level...
                                  uint8_t fadeLevel = 0; //used to store the fade level when using the buttons
                                  uint8_t upPreviousValue;
                                  uint8_t downPreviousValue;
                                  uint8_t powerPreviousValue;
                                  
                                  Bounce upDebouncer = Bounce();
                                  Bounce downDebouncer = Bounce();
                                  Bounce powerDebouncer = Bounce();
                                  
                                  //motion sensor
                                  uint8_t lastMotion = 0;
                                  
                                  
                                  unsigned long previousMillis = 0; // last time update //see http://stackoverflow.com/questions/10773425/performing-a-function-after-x-time for more details on this
                                  unsigned long motionDelay = 10000; // interval at which to keep motion sensor trippped (milliseconds).  Used to prevent too frequent updates to Vera. 
                                  unsigned long upPreviousMillis = 0;
                                  unsigned long downPreviousMillis = 0; 
                                  unsigned long buttonFadeDelay = 200; 
                                  int lastLightLevel;
                                  
                                  boolean metric = true; 
                                  
                                  MyMessage motionMsg(MOTION_CHILD, V_TRIPPED);
                                  MyMessage dimmerMsg(MIRROR_LED_CHILD, V_DIMMER);
                                  MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                  //MyMessage lightMsg(LED_CHILD, V_LIGHT);  removed, updating with dimmer values only
                                  
                                  /***
                                   * Dimmable LED initialization method
                                   */
                                  void setup()  
                                  { 
                                    Serial.println( SN ); 
                                  
                                    gw.begin( incomingMessage,  NODE_ID);
                                  
                                    // Register the LED Dimmable Light with the gateway
                                    gw.present(MIRROR_LED_CHILD, S_DIMMER );
                                    gw.present(MOTION_CHILD, S_MOTION);
                                    gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                    
                                    // Send the sketch version information to the gateway and Controller
                                    gw.sendSketchInfo(SN, SV);
                                    
                                    metric = gw.getConfig().isMetric;
                                    
                                   
                                     // Setup the button
                                    pinMode(UP_BUTTON_PIN,INPUT);
                                    pinMode(DOWN_BUTTON_PIN,INPUT);
                                    pinMode(POWER_BUTTON_PIN,INPUT);
                                    
                                    // Activate internal pull-up
                                    digitalWrite(UP_BUTTON_PIN,HIGH);
                                    digitalWrite(DOWN_BUTTON_PIN,HIGH);
                                    digitalWrite(POWER_BUTTON_PIN,HIGH);
                                    
                                    // After setting up the button, setup debouncer
                                    upDebouncer.attach(UP_BUTTON_PIN);
                                    downDebouncer.attach(DOWN_BUTTON_PIN);
                                    powerDebouncer.attach(POWER_BUTTON_PIN);
                                    upDebouncer.interval(5);
                                    downDebouncer.interval(5);
                                    powerDebouncer.interval(5);
                                    
                                  
                                  }
                                  
                                  /***
                                   *  Dimmable LED main processing loop 
                                   */
                                  void loop() 
                                  {
                                    
                                    
                                    
                                    //process the LED commands from gateway
                                    gw.process();
                                    
                                   int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
                                   // Serial.println(lightLevel);
                                    if (lightLevel != lastLightLevel) {
                                        gw.send(msg.set(lightLevel));
                                        lastLightLevel = lightLevel;
                                    }  
                                   
                                   
                                    //motion sensor code
                                    unsigned long currentMillis = millis();
                                    
                                       if(currentMillis - previousMillis > motionDelay){
                                        uint8_t motionDetect = digitalRead(MOTION_PIN);
                                    
                                        if(motionDetect != lastMotion){
                                  //        Serial.print("motionDetect Value: ");
                                  //        Serial.println(motionDetect);
                                          gw.send(motionMsg.set(motionDetect));  // Send tripped value to gw
                                          
                                          if(motionDetect == 1){
                                            previousMillis = currentMillis;  //"Tripped" delay 
                                          }
                                          else{
                                            previousMillis = currentMillis - motionDelay + 1000; //"Not tripped" delay for 1 second to stop rapid "not tripped" and "tripped" updates to Vera
                                          }
                                    
                                           lastMotion = motionDetect; 
                                        }    
                                      }
                                  
                                    
                                  /*
                                  There are 3 buttons attached to the mirror.  One is an on/off button and the other two will fade up/fade down.  
                                  The sensor will remember the last fade state and fade the lights to that level next time they are turned on.
                                  
                                  If fade up or fade down button is pressed it should store that value into a variable and when the ON button is pressed
                                  it will fade to that previous value.
                                   */ 
                                  
                                      upDebouncer.update();
                                      // Get the update value
                                      uint8_t upValue = upDebouncer.read();
                                      
                                      unsigned long upCurrentMillis = millis();
                                      
                                      if(upCurrentMillis - upPreviousMillis > buttonFadeDelay){
                                        if ((upValue == LOW) && (fadeLevel<100)) { //Because of the internal pullup resistors LOW = button is presssed
                                        
                                          fadeLevel += FADE_PERCENTAGE;
                                          fadeLevel = fadeLevel > 100 ? 100 : fadeLevel;
                                          fadeToLevel( fadeLevel );
                                          
                                        }
                                        upPreviousMillis = upCurrentMillis;
                                      }
                                    
                                      downDebouncer.update();
                                      // Get the update value
                                      uint8_t downValue = downDebouncer.read();
                                      
                                      unsigned long downCurrentMillis = millis();
                                      
                                      if(downCurrentMillis - downPreviousMillis > buttonFadeDelay){
                                        if ((downValue == LOW) && (fadeLevel>0)) {
                                          
                                          fadeLevel -= FADE_PERCENTAGE;
                                          fadeLevel = fadeLevel < 0 ? 0 : fadeLevel;
                                          //powerState = fadeLevel; //Remember fade level for when power button is pressed
                                          fadeToLevel( fadeLevel );
                                          
                                        }
                                        downPreviousMillis = downCurrentMillis;
                                      }
                                        
                                      powerDebouncer.update();
                                      // Get the update value
                                      uint8_t powerValue = powerDebouncer.read();
                                      
                                      if(powerValue != powerPreviousValue){
                                        if (powerValue == LOW) {
                                        Serial.print("Power Button Pressed. fadeLevel is ");
                                        Serial.println(fadeLevel);
                                          if (currentLevel > 0) {
                                            fadeToLevel( 0 );
                                          }
                                          else{
                                            if (fadeLevel == 0) {
                                              fadeToLevel(50);
                                              fadeLevel = 50;
                                            }
                                            else{
                                              fadeToLevel(fadeLevel);
                                            }
                                          }
                                        }
                                        powerPreviousValue = powerValue;
                                      }
                                    
                                     
                                  }
                                  
                                  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 to " );
                                  //    Serial.print( requestedLevel );
                                  //    Serial.print( ", from " ); 
                                  //    Serial.println( currentLevel );
                                  
                                      fadeToLevel( requestedLevel );
                                      }
                                  }
                                  
                                  /***
                                   *  This method provides a graceful fade up/down effect
                                   */
                                  void fadeToLevel( int toLevel ) {
                                  
                                  //  Serial.print("toLevel Value: ");
                                  //  Serial.println(toLevel);
                                  //  Serial.print("currentLevel Value: ");
                                  //  Serial.println(currentLevel);
                                    
                                    int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
                                    
                                    while ( currentLevel != toLevel ) {
                                      currentLevel += delta;
                                      analogWrite( MIRROR_LED_PIN, (int)(currentLevel / 100. * 255) );
                                      delay( FADE_DELAY );
                                    }
                                  
                                     // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                                    //gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));  //used to send status of light (on/off) to Vera
                                    gw.send( dimmerMsg.set(currentLevel) );
                                  }
                                  
                                  petewillP 1 Reply Last reply
                                  0
                                  • FrancoisF Francois

                                    @petewill I add a light sensor to your sketch and this now slow everything down what will the reason for this be? The motion sensor will not always pickup motion. I have another node with light and motion sensor and that is working right.

                                    Here is the code:

                                    /***
                                     * 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 (MIRROR_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 - February 4, 2014 - Pete B - added buttons to control on/off and dim level and a motion sensor
                                     ***/
                                     
                                     
                                    #define SN "Mirror LED with PIR and LDR"
                                    #define SV "1.2"
                                    #define NODE_ID 50  //change to a number to assign a specific ID
                                    
                                    #include <SPI.h>
                                    #include <MySensor.h>
                                    #include <Bounce2.h>
                                    #include <MySigningNone.h>
                                    #include <MyTransportNRF24.h>
                                    #include <MyHwATMega328.h>
                                    
                                    #define MIRROR_LED_CHILD 0    //ID of the LED child
                                    #define MOTION_CHILD 1  //ID of the motion sensor child
                                    #define CHILD_ID_LIGHT 2 //ID for LDR sensor
                                    
                                    #define LIGHT_SENSOR_ANALOG_PIN 0
                                    
                                    // NRFRF24L01 radio driver (set low transmit power by default) 
                                    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
                                    
                                    #define MIRROR_LED_PIN 3      // Arduino pin attached to MOSFET Gate pin
                                    #define UP_BUTTON_PIN 8  // Arduino Digital I/O pin number for the fade up button 
                                    #define DOWN_BUTTON_PIN 7  // Arduino Digital I/O pin number for the fade down button 
                                    #define POWER_BUTTON_PIN 4  // Arduino Digital I/O pin number for the power button 
                                    #define MOTION_PIN  6  // Arduino pin tied to trigger pin on the motion sensor.
                                    
                                    #define FADE_DELAY 10  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
                                    #define FADE_PERCENTAGE 10 //The percentage the fade level will be changed when a button is pressed
                                    
                                    MyHwATMega328 hw;
                                    // Construct MySensors library
                                    MySensor gw(radio, hw);
                                    
                                    //MySensor gw; //Don't need to define pins unless they are changing from the default
                                    
                                    static int currentLevel = 0;  // Current dim level...
                                    uint8_t fadeLevel = 0; //used to store the fade level when using the buttons
                                    uint8_t upPreviousValue;
                                    uint8_t downPreviousValue;
                                    uint8_t powerPreviousValue;
                                    
                                    Bounce upDebouncer = Bounce();
                                    Bounce downDebouncer = Bounce();
                                    Bounce powerDebouncer = Bounce();
                                    
                                    //motion sensor
                                    uint8_t lastMotion = 0;
                                    
                                    
                                    unsigned long previousMillis = 0; // last time update //see http://stackoverflow.com/questions/10773425/performing-a-function-after-x-time for more details on this
                                    unsigned long motionDelay = 10000; // interval at which to keep motion sensor trippped (milliseconds).  Used to prevent too frequent updates to Vera. 
                                    unsigned long upPreviousMillis = 0;
                                    unsigned long downPreviousMillis = 0; 
                                    unsigned long buttonFadeDelay = 200; 
                                    int lastLightLevel;
                                    
                                    boolean metric = true; 
                                    
                                    MyMessage motionMsg(MOTION_CHILD, V_TRIPPED);
                                    MyMessage dimmerMsg(MIRROR_LED_CHILD, V_DIMMER);
                                    MyMessage msg(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
                                    //MyMessage lightMsg(LED_CHILD, V_LIGHT);  removed, updating with dimmer values only
                                    
                                    /***
                                     * Dimmable LED initialization method
                                     */
                                    void setup()  
                                    { 
                                      Serial.println( SN ); 
                                    
                                      gw.begin( incomingMessage,  NODE_ID);
                                    
                                      // Register the LED Dimmable Light with the gateway
                                      gw.present(MIRROR_LED_CHILD, S_DIMMER );
                                      gw.present(MOTION_CHILD, S_MOTION);
                                      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
                                      
                                      // Send the sketch version information to the gateway and Controller
                                      gw.sendSketchInfo(SN, SV);
                                      
                                      metric = gw.getConfig().isMetric;
                                      
                                     
                                       // Setup the button
                                      pinMode(UP_BUTTON_PIN,INPUT);
                                      pinMode(DOWN_BUTTON_PIN,INPUT);
                                      pinMode(POWER_BUTTON_PIN,INPUT);
                                      
                                      // Activate internal pull-up
                                      digitalWrite(UP_BUTTON_PIN,HIGH);
                                      digitalWrite(DOWN_BUTTON_PIN,HIGH);
                                      digitalWrite(POWER_BUTTON_PIN,HIGH);
                                      
                                      // After setting up the button, setup debouncer
                                      upDebouncer.attach(UP_BUTTON_PIN);
                                      downDebouncer.attach(DOWN_BUTTON_PIN);
                                      powerDebouncer.attach(POWER_BUTTON_PIN);
                                      upDebouncer.interval(5);
                                      downDebouncer.interval(5);
                                      powerDebouncer.interval(5);
                                      
                                    
                                    }
                                    
                                    /***
                                     *  Dimmable LED main processing loop 
                                     */
                                    void loop() 
                                    {
                                      
                                      
                                      
                                      //process the LED commands from gateway
                                      gw.process();
                                      
                                     int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
                                     // Serial.println(lightLevel);
                                      if (lightLevel != lastLightLevel) {
                                          gw.send(msg.set(lightLevel));
                                          lastLightLevel = lightLevel;
                                      }  
                                     
                                     
                                      //motion sensor code
                                      unsigned long currentMillis = millis();
                                      
                                         if(currentMillis - previousMillis > motionDelay){
                                          uint8_t motionDetect = digitalRead(MOTION_PIN);
                                      
                                          if(motionDetect != lastMotion){
                                    //        Serial.print("motionDetect Value: ");
                                    //        Serial.println(motionDetect);
                                            gw.send(motionMsg.set(motionDetect));  // Send tripped value to gw
                                            
                                            if(motionDetect == 1){
                                              previousMillis = currentMillis;  //"Tripped" delay 
                                            }
                                            else{
                                              previousMillis = currentMillis - motionDelay + 1000; //"Not tripped" delay for 1 second to stop rapid "not tripped" and "tripped" updates to Vera
                                            }
                                      
                                             lastMotion = motionDetect; 
                                          }    
                                        }
                                    
                                      
                                    /*
                                    There are 3 buttons attached to the mirror.  One is an on/off button and the other two will fade up/fade down.  
                                    The sensor will remember the last fade state and fade the lights to that level next time they are turned on.
                                    
                                    If fade up or fade down button is pressed it should store that value into a variable and when the ON button is pressed
                                    it will fade to that previous value.
                                     */ 
                                    
                                        upDebouncer.update();
                                        // Get the update value
                                        uint8_t upValue = upDebouncer.read();
                                        
                                        unsigned long upCurrentMillis = millis();
                                        
                                        if(upCurrentMillis - upPreviousMillis > buttonFadeDelay){
                                          if ((upValue == LOW) && (fadeLevel<100)) { //Because of the internal pullup resistors LOW = button is presssed
                                          
                                            fadeLevel += FADE_PERCENTAGE;
                                            fadeLevel = fadeLevel > 100 ? 100 : fadeLevel;
                                            fadeToLevel( fadeLevel );
                                            
                                          }
                                          upPreviousMillis = upCurrentMillis;
                                        }
                                      
                                        downDebouncer.update();
                                        // Get the update value
                                        uint8_t downValue = downDebouncer.read();
                                        
                                        unsigned long downCurrentMillis = millis();
                                        
                                        if(downCurrentMillis - downPreviousMillis > buttonFadeDelay){
                                          if ((downValue == LOW) && (fadeLevel>0)) {
                                            
                                            fadeLevel -= FADE_PERCENTAGE;
                                            fadeLevel = fadeLevel < 0 ? 0 : fadeLevel;
                                            //powerState = fadeLevel; //Remember fade level for when power button is pressed
                                            fadeToLevel( fadeLevel );
                                            
                                          }
                                          downPreviousMillis = downCurrentMillis;
                                        }
                                          
                                        powerDebouncer.update();
                                        // Get the update value
                                        uint8_t powerValue = powerDebouncer.read();
                                        
                                        if(powerValue != powerPreviousValue){
                                          if (powerValue == LOW) {
                                          Serial.print("Power Button Pressed. fadeLevel is ");
                                          Serial.println(fadeLevel);
                                            if (currentLevel > 0) {
                                              fadeToLevel( 0 );
                                            }
                                            else{
                                              if (fadeLevel == 0) {
                                                fadeToLevel(50);
                                                fadeLevel = 50;
                                              }
                                              else{
                                                fadeToLevel(fadeLevel);
                                              }
                                            }
                                          }
                                          powerPreviousValue = powerValue;
                                        }
                                      
                                       
                                    }
                                    
                                    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 to " );
                                    //    Serial.print( requestedLevel );
                                    //    Serial.print( ", from " ); 
                                    //    Serial.println( currentLevel );
                                    
                                        fadeToLevel( requestedLevel );
                                        }
                                    }
                                    
                                    /***
                                     *  This method provides a graceful fade up/down effect
                                     */
                                    void fadeToLevel( int toLevel ) {
                                    
                                    //  Serial.print("toLevel Value: ");
                                    //  Serial.println(toLevel);
                                    //  Serial.print("currentLevel Value: ");
                                    //  Serial.println(currentLevel);
                                      
                                      int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
                                      
                                      while ( currentLevel != toLevel ) {
                                        currentLevel += delta;
                                        analogWrite( MIRROR_LED_PIN, (int)(currentLevel / 100. * 255) );
                                        delay( FADE_DELAY );
                                      }
                                    
                                       // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
                                      //gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));  //used to send status of light (on/off) to Vera
                                      gw.send( dimmerMsg.set(currentLevel) );
                                    }
                                    
                                    petewillP Offline
                                    petewillP Offline
                                    petewill
                                    Admin
                                    wrote on last edited by
                                    #17

                                    @Francois I usually put a "millis" delay for all of my sensors that don't need instant updates (temperature, humidity, light, etc). I have had some of my sensors updating too frequently and causing communication issues for the rest of my sensors. Maybe that's what's happening to you? Try adding some code so the light sensor only updates once per 10 seconds or what ever interval you feel is good. You can use my motion sensor code as an example for adding a delay with millis.

                                    My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                                    1 Reply Last reply
                                    0
                                    • petewillP petewill

                                      Backlit-Mirror.jpg
                                      Hi Everyone,

                                      I have created backlit mirror based on Bruce Lacey's dimmable LED sketch. I have added a motion sensor and some on/off and fade up/down buttons. There is some logic in the code to save the dim level when the buttons are pressed. I have all my motion turn on/off logic in my Vera controller using PLEG. It could be easily adapted to control the on/off functionality in the Arduino code but I like to have my automation logic in one place (my Vera).

                                      This was a fun project to work on and although it looks pretty ugly from the back my wife loves the way it looks in our master bathroom. I tried to document as best I could but please let me know if you have any questions.

                                      Here is a list of the parts I used

                                      • Items from MySensors Store http://www.mysensors.org/store/
                                        • Female Pin Header Connector Strip
                                        • Prototype Universal Printed Circuit Boards (PCB)
                                        • NRF24L01 Radio
                                        • Arduino Pro Mini
                                        • FTDI USB to TTL Serial Adapter
                                        • Capacitors (10uf and .1uf)
                                        • 3.3v voltage regulator
                                        • 5v voltage regulator
                                        • IRLZ44N Logic Level Transistor MOSFET
                                        • 12v Transformer (power supply)
                                        • 5 Meter LED Strip (I used 3528)
                                        • HC-SR501 PIR Motion Sensor Module
                                      • 22-24 gauge wire or similar (I used Cat5/Cat6 cable)
                                      • 2 Pole 5mm Pitch PCB Mount Screw Terminal Block

                                      Here is a video explaining how to build it yourself.
                                      http://youtu.be/jblaMddRDpc

                                      Mirror LED Wiring Diagram.png
                                      Here is the Fritzing (http://fritzing.org/) wiring diagram file if you want to view in more detail: Mirror LED Wiring Diagram.fzz
                                      https://codebender.cc/sketch:81486

                                      20150203_201003-web.jpg
                                      20150203_195706-web.jpg

                                      H Offline
                                      H Offline
                                      henno
                                      wrote on last edited by
                                      #18

                                      @petewill Just wanted to let you know that I've built two of these for my daughters but instead of putting them behind a mirror I put it under their bed. Works really good, thanks for sharing!

                                      petewillP hekH DrJeffD 3 Replies Last reply
                                      1
                                      • H henno

                                        @petewill Just wanted to let you know that I've built two of these for my daughters but instead of putting them behind a mirror I put it under their bed. Works really good, thanks for sharing!

                                        petewillP Offline
                                        petewillP Offline
                                        petewill
                                        Admin
                                        wrote on last edited by
                                        #19

                                        @henno Cool idea!

                                        My "How To" home automation video channel: https://www.youtube.com/channel/UCq_Evyh5PQALx4m4CQuxqkA

                                        1 Reply Last reply
                                        0
                                        • H henno

                                          @petewill Just wanted to let you know that I've built two of these for my daughters but instead of putting them behind a mirror I put it under their bed. Works really good, thanks for sharing!

                                          hekH Online
                                          hekH Online
                                          hek
                                          Admin
                                          wrote on last edited by
                                          #20

                                          @henno said:

                                          I've built two of these for my daughters but instead of putting them behind a mirror I put it under their bed.

                                          Scares away all the monsters? :trollface:

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          15

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular