delay for LDR sensor

  • 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 MOTION_SENSOR_PIN    3
    #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)
      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];
    int lastLightLevel;
    unsigned long SLEEP_TIME = 6000; // Sleep time between reads (in milliseconds)
    void setup()
      gw.begin(incomingMessage, RADIO_ID, true);
      gw.sendSketchInfo("Multy-Relay&Pulsanti", "0.2");
      gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
      // 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
        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
    void loop()
      if ( motionsDebouncer.update()) {
        int value =;
        Serial.println( "Motion sensor is " + (String)value );
      for (byte i = 0; i < noRelays; i++)
        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; 
      if (lightLevel != lastLightLevel) {
          lastLightLevel = lightLevel;
          Serial.print("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.

  • Mod

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

    If so, try changing

    if (lightLevel != lastLightLevel)


    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.

  • 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?

  • Mod

    @Dick what numbers is it logging?

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

  • Mod

    @Dick Isn't that what you want?

  • 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.

  • Mod

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

  • 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.

  • Mod

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

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

  • 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

  • Mod

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

  • good luck and i advice you to do it wll otherwise you have an issue😓