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. General Discussion
  3. delay for LDR sensor

delay for LDR sensor

Scheduled Pinned Locked Moved General Discussion
13 Posts 2 Posters 3.1k Views 2 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.
  • DickD Offline
    DickD Offline
    Dick
    wrote on last edited by Dick
    #1

    I am a newbi and I have a working sensor build. A 4 button, 4 Relay and Pir. I added an LDR and that works only it keep on logging. How to stop this, perhaps a delay or something like that and where to place in the script?
    The Serial.println(lightLevel);
    delay(2500); //just here to slow down the output for easier reading does not work

    #include <MySensor.h>
    #include <SPI.h>
    #include "Bounce2.h"
    #define USE_MOTION_SENSOR
    #define MOTION_SENSOR_PIN    3
    #define LIGHT_SENSOR_ANALOG_PIN A0
    #define CHILD_ID_LIGHT 6
    #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
    #define RELAY_OFF 1
    
    Bounce motionsDebouncer = Bounce();
    
    MySensor gw;
    
    #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
    #define noRelays 4
    const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
    const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public: 
      int buttonPin;                    // physical pin number of button
      int relayPin;                     // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays];
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    MyMessage msgLDR(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
    
    int lastLightLevel;
    unsigned long SLEEP_TIME = 6000; // Sleep time between reads (in milliseconds)
    
    void setup()
    {
      gw.begin(incomingMessage, RADIO_ID, true);
      delay(250);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      delay(250);
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
    
      pinMode( MOTION_SENSOR_PIN, INPUT_PULLUP );
      motionsDebouncer.attach(MOTION_SENSOR_PIN);
      motionsDebouncer.interval(50);
    
      // Initialize Relays with corresponding buttons
      for (int i = 0; i < noRelays; i++)
      {
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_LIGHT;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
        gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
        gw.present(i, S_LIGHT);                               // present sensor to gateway
        delay(250);
    
      }
    }
    
    void loop()
    {
      if ( motionsDebouncer.update()) {
        int value = motionsDebouncer.read();
        Serial.println( "Motion sensor is " + (String)value );
      }
    {
      gw.process();
      for (byte i = 0; i < noRelays; i++)
      {
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value == 0)
        {
          Relays[i].relayState = !Relays[i].relayState;
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
          gw.send(msg[i].set(Relays[i].relayState ? true : false));
          gw.saveState( i, Relays[i].relayState );
        }                 // save sensor state in EEPROM (location == sensor number)
    
        Relays[i].oldValue = value;
      }
       int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
      Serial.println(lightLevel);
      if (lightLevel != lastLightLevel) {
           gw.send(msgLDR.set(lightLevel));
    
          lastLightLevel = lightLevel;
          Serial.print("Lightlevel =");
          
          Serial.println(lightLevel);
    delay(2500); //just here to slow down the output for easier reading
      }
    }
    }
    // process incoming message
    void incomingMessage(const MyMessage &message)
    {
    
      if (message.type == V_LIGHT)
      {
        if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
        {
          Relays[message.sensor].relayState = message.getBool();
          digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
        }
      }
     // delay( 50 ); // give the input pins some rest. Incomming messages are still being processed.
    gw.wait(50);
    
    }
    
    mfalkviddM 1 Reply Last reply
    0
    • DickD Dick

      I am a newbi and I have a working sensor build. A 4 button, 4 Relay and Pir. I added an LDR and that works only it keep on logging. How to stop this, perhaps a delay or something like that and where to place in the script?
      The Serial.println(lightLevel);
      delay(2500); //just here to slow down the output for easier reading does not work

      #include <MySensor.h>
      #include <SPI.h>
      #include "Bounce2.h"
      #define USE_MOTION_SENSOR
      #define MOTION_SENSOR_PIN    3
      #define LIGHT_SENSOR_ANALOG_PIN A0
      #define CHILD_ID_LIGHT 6
      #define RELAY_ON 0                      // switch around for realy HIGH/LOW state
      #define RELAY_OFF 1
      
      Bounce motionsDebouncer = Bounce();
      
      MySensor gw;
      
      #define RADIO_ID 11                    // radio Id, whatever channel you assigned to
      #define noRelays 4
      const int relayPin[] = {A1, A2, A3, A4}; //  switch around pins to your desire
      const int buttonPin[] = {A5, 6, 7, 8}; //  switch around pins to your desire
      
      class Relay             // relay class, store all relevant data (equivalent to struct)
      {
      public: 
        int buttonPin;                    // physical pin number of button
        int relayPin;                     // physical pin number of relay
        byte oldValue;                    // last Values for key (debounce)
        boolean relayState;               // relay status (also stored in EEPROM)
      };
      
      Relay Relays[noRelays];
      Bounce debouncer[noRelays];
      MyMessage msg[noRelays];
      MyMessage msgLDR(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
      
      int lastLightLevel;
      unsigned long SLEEP_TIME = 6000; // Sleep time between reads (in milliseconds)
      
      void setup()
      {
        gw.begin(incomingMessage, RADIO_ID, true);
        delay(250);
        gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
        delay(250);
        gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
      
        pinMode( MOTION_SENSOR_PIN, INPUT_PULLUP );
        motionsDebouncer.attach(MOTION_SENSOR_PIN);
        motionsDebouncer.interval(50);
      
        // Initialize Relays with corresponding buttons
        for (int i = 0; i < noRelays; i++)
        {
          Relays[i].buttonPin = buttonPin[i];              // assign physical pins
          Relays[i].relayPin = relayPin[i];
          msg[i].sensor = i;                                   // initialize messages
          msg[i].type = V_LIGHT;
          debouncer[i] = Bounce();                        // initialize debouncer
          debouncer[i].attach(buttonPin[i]);
          debouncer[i].interval(5);
          pinMode(Relays[i].buttonPin, INPUT_PULLUP);
          pinMode(Relays[i].relayPin, OUTPUT);
          Relays[i].relayState = gw.loadState(i);                               // retrieve last values from EEPROM
          digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
          gw.send(msg[i].set(Relays[i].relayState ? true : false));                 // make controller aware of last status
          gw.present(i, S_LIGHT);                               // present sensor to gateway
          delay(250);
      
        }
      }
      
      void loop()
      {
        if ( motionsDebouncer.update()) {
          int value = motionsDebouncer.read();
          Serial.println( "Motion sensor is " + (String)value );
        }
      {
        gw.process();
        for (byte i = 0; i < noRelays; i++)
        {
          debouncer[i].update();
          byte value = debouncer[i].read();
          if (value != Relays[i].oldValue && value == 0)
          {
            Relays[i].relayState = !Relays[i].relayState;
            digitalWrite(Relays[i].relayPin, Relays[i].relayState ? RELAY_ON : RELAY_OFF);
            gw.send(msg[i].set(Relays[i].relayState ? true : false));
            gw.saveState( i, Relays[i].relayState );
          }                 // save sensor state in EEPROM (location == sensor number)
      
          Relays[i].oldValue = value;
        }
         int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23; 
        Serial.println(lightLevel);
        if (lightLevel != lastLightLevel) {
             gw.send(msgLDR.set(lightLevel));
      
            lastLightLevel = lightLevel;
            Serial.print("Lightlevel =");
            
            Serial.println(lightLevel);
      delay(2500); //just here to slow down the output for easier reading
        }
      }
      }
      // process incoming message
      void incomingMessage(const MyMessage &message)
      {
      
        if (message.type == V_LIGHT)
        {
          if (message.sensor < noRelays)            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
          {
            Relays[message.sensor].relayState = message.getBool();
            digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState ? RELAY_ON : RELAY_OFF); // and set relays accordingly
            gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
          }
        }
       // delay( 50 ); // give the input pins some rest. Incomming messages are still being processed.
      gw.wait(50);
      
      }
      
      mfalkviddM Offline
      mfalkviddM Offline
      mfalkvidd
      Mod
      wrote on last edited by mfalkvidd
      #2

      @Dick my guess is that the light level changes slightly each time. Is that correct?

      If so, try changing

      if (lightLevel != lastLightLevel)
      

      to

      if ((lightLevel > 1.05 * lastLightLevel)||(lightLevel < 0.95 * lastLightLevel))
      

      This will prevent the light level from being reported unless it has changed more than 5%.

      You can then remove the delay(2500). It might prevent the sensor from receiving incoming messages.

      1 Reply Last reply
      0
      • DickD Offline
        DickD Offline
        Dick
        wrote on last edited by
        #3

        thanks for the advise. I changed the If into you sugested (what is much better that 5% difference) but the ldr is still logging (10 per second or so).
        any Idea?

        mfalkviddM 1 Reply Last reply
        0
        • DickD Dick

          thanks for the advise. I changed the If into you sugested (what is much better that 5% difference) but the ldr is still logging (10 per second or so).
          any Idea?

          mfalkviddM Offline
          mfalkviddM Offline
          mfalkvidd
          Mod
          wrote on last edited by
          #4

          @Dick what numbers is it logging?

          DickD 1 Reply Last reply
          0
          • mfalkviddM mfalkvidd

            @Dick what numbers is it logging?

            DickD Offline
            DickD Offline
            Dick
            wrote on last edited by
            #5

            1 to 100 depends on the light intencity and it changes the value with more and less light.

            mfalkviddM 1 Reply Last reply
            0
            • DickD Dick

              1 to 100 depends on the light intencity and it changes the value with more and less light.

              mfalkviddM Offline
              mfalkviddM Offline
              mfalkvidd
              Mod
              wrote on last edited by
              #6

              @Dick Isn't that what you want?

              DickD 1 Reply Last reply
              0
              • mfalkviddM mfalkvidd

                @Dick Isn't that what you want?

                DickD Offline
                DickD Offline
                Dick
                wrote on last edited by
                #7

                that is what I want but it continue with logging (> 1000 per minute).So the sensor is very busy with the LDR only. a delay should be the best or your solution of 5% difference.

                mfalkviddM 1 Reply Last reply
                0
                • DickD Dick

                  that is what I want but it continue with logging (> 1000 per minute).So the sensor is very busy with the LDR only. a delay should be the best or your solution of 5% difference.

                  mfalkviddM Offline
                  mfalkviddM Offline
                  mfalkvidd
                  Mod
                  wrote on last edited by
                  #8

                  @Dick does the number vary wildly even if the same amout of light is shining on the ldr?

                  DickD 1 Reply Last reply
                  0
                  • mfalkviddM mfalkvidd

                    @Dick does the number vary wildly even if the same amout of light is shining on the ldr?

                    DickD Offline
                    DickD Offline
                    Dick
                    wrote on last edited by
                    #9

                    no it is steady with the same amount of light, changing it it keeps steady on the new value but with a hi speed logging. , I checked it just now. The LDR is connected to the +5v and A0 with a resister of 10k to gnd.

                    mfalkviddM 1 Reply Last reply
                    0
                    • DickD Dick

                      no it is steady with the same amount of light, changing it it keeps steady on the new value but with a hi speed logging. , I checked it just now. The LDR is connected to the +5v and A0 with a resister of 10k to gnd.

                      mfalkviddM Offline
                      mfalkviddM Offline
                      mfalkvidd
                      Mod
                      wrote on last edited by
                      #10

                      @Dick now I see the problem! There are two Serial.println(lightLevel);

                      Remove the Serial.println(lightLevel); that sits just before the if clause.

                      1 Reply Last reply
                      0
                      • DickD Offline
                        DickD Offline
                        Dick
                        wrote on last edited by
                        #11

                        YES yes yes, I did not see that one. Your solution is working perfect also with the difference in light (5%).

                        Thank you for the support and have a nice weekend

                        mfalkviddM 1 Reply Last reply
                        1
                        • DickD Dick

                          YES yes yes, I did not see that one. Your solution is working perfect also with the difference in light (5%).

                          Thank you for the support and have a nice weekend

                          mfalkviddM Offline
                          mfalkviddM Offline
                          mfalkvidd
                          Mod
                          wrote on last edited by
                          #12

                          @Dick you're welcome, the same to you
                          Back to buildning lighting for my wife's makeup mirror :)

                          DickD 1 Reply Last reply
                          0
                          • mfalkviddM mfalkvidd

                            @Dick you're welcome, the same to you
                            Back to buildning lighting for my wife's makeup mirror :)

                            DickD Offline
                            DickD Offline
                            Dick
                            wrote on last edited by
                            #13

                            good luck and i advice you to do it wll otherwise you have an issue:sweat:

                            1 Reply Last reply
                            0

                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                            With your input, this post could be even better 💗

                            Register Login
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            32

                            Online

                            12.0k

                            Users

                            11.2k

                            Topics

                            113.4k

                            Posts


                            Copyright 2025 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