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 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); }@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.
-
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? -
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.
-
@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.
-
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
-
@Dick you're welcome, the same to you
Back to buildning lighting for my wife's makeup mirror :)
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