user67074: This may sound like a simple question but I have tried to find the right solution but all I find is the usual make an led blink and even I can code that nugget
I am quite new to programming arduino but am a dab hand with a soldering iron.
What I would like to do is combine the attributes of an LDR to allow a lighting sketch to run if it is dark and go off in daylight, but all I seem to find is turn a pin high or low, this is no good as I already have a sketch I would like to run
I am using WS2811 led nodes housed in pinspot style housings so the sketch would need to check the light level and if it is below a set value run the rest of the sketch then check every hour to see if the light level has changed to be above a set level  if it is go into a check loop until the light level falls again to below the set value can anyone point me in the right direction I would be grateful for any assistance
Thanks
Dave

user2967: You're unlikely to find exactly what you want as existing code example. Break it into parts and solve it in parts.
You can't "Run a sketch" based on reading a value of an LDR. You can write a new sketch that reads an LDR, and calls a function if it's dark, or a different function if it is light. You will need to refactor your code from your other sketch into functions that you can integrate into your new combined sketch that uses the LDR. That means doing some original programming, not "finding the right solution" ready-made on the internet.
First, how do you use an LDR to determine if it's light or dark? Answer, use an analog input. Build a voltage divider with the LDR and a similar sized resistor, and feed the output into an analog pin. Say the LDR is between +5V and the pin, so more light results in higher voltage, hence a larger value.
Log the output of the LDR to the console in a loop, and decide on your threshold values for light and dark. Make the threshold value a constant. Let's call it DARK.
Now write a loop function. It might look something like this:
int lightValue;
#define ldr_pin xxx //your pin number
#define threshold xxx //your threshold value

void setup() {
   //set the ldr pin as an analog input
}

void loop() {
   lightValue = analogRead(ldr_pin);
   if (lightValue <= threshold) {
      doNightStuff();
   } else {
      stopDoingStuff();
   }
}

Edit:
As mentioned by Edgar, the code above will be subject to "jitter", where it will rapidly switch back and forth between the dark and light states.
The solution is to add a buffer range where once it's in one state, it doesn't change states until it leaves the buffer range.
The if statement might look like this:
#define buffer 5

if ((lightValue +buffer) <= threshold) 
    doNightStuff(); 
else if ((lightValue -buffer) >= threshold) 
    stopDoingStuff(); 
    


67074: <p>This may sound like a simple question but I have tried to find the right solution but all I find is the usual make an led blink and even I can code that nugget
I am quite new to programming arduino but am a dab hand with a soldering iron.
What I would like to do is combine the attributes of an LDR to allow a lighting sketch to run if it is dark and go off in daylight, but all I seem to find is turn a pin high or low, this is no good as I already have a sketch I would like to run</p>
<p>I am using WS2811 led nodes housed in pinspot style housings so the sketch would need to check the light level and if it is below a set value run the rest of the sketch then check every hour to see if the light level has changed to be above a set level  if it is go into a check loop until the light level falls again to below the set value can anyone point me in the right direction I would be grateful for any assistance</p>
<p>Thanks
Dave</p>
 2967: <p>You're unlikely to find exactly what you want as existing code example. Break it into parts and solve it in parts.</p>
<p>You can't &quot;Run a sketch&quot; based on reading a value of an LDR. You can write a new sketch that reads an LDR, and calls a function if it's dark, or a different function if it is light. You will need to refactor your code from your other sketch into functions that you can integrate into your new combined sketch that uses the LDR. That means doing some original programming, not &quot;finding the right solution&quot; ready-made on the internet.</p>
<p>First, how do you use an LDR to determine if it's light or dark? Answer, use an analog input. Build a voltage divider with the LDR and a similar sized resistor, and feed the output into an analog pin. Say the LDR is between +5V and the pin, so more light results in higher voltage, hence a larger value.</p>
<p>Log the output of the LDR to the console in a loop, and decide on your threshold values for light and dark. Make the threshold value a constant. Let's call it DARK.</p>
<p>Now write a loop function. It might look something like this:</p>
<pre><code>int lightValue;
#define ldr_pin xxx //your pin number
#define threshold xxx //your threshold value

void setup() {
   //set the ldr pin as an analog input
}

void loop() {
   lightValue = analogRead(ldr_pin);
   if (lightValue &lt;= threshold) {
      doNightStuff();
   } else {
      stopDoingStuff();
   }
}
</code></pre>
<h2>Edit:</h2>
<p>As mentioned by Edgar, the code above will be subject to &quot;jitter&quot;, where it will rapidly switch back and forth between the dark and light states.</p>
<p>The solution is to add a buffer range where once it's in one state, it doesn't change states until it leaves the buffer range.</p>
<p>The if statement might look like this:</p>
<pre><code>#define buffer 5

if ((lightValue +buffer) &lt;= threshold) 
    doNightStuff(); 
else if ((lightValue -buffer) &gt;= threshold) 
    stopDoingStuff(); 
    
</code></pre>
