Making a Sensor Light With Preset Timer Function

Hey guys,

So as you can tell I am a complete novice when it comes to the arduino and any sort of programming! Essentially what I want to do is create a circuit that uses an LDR threshold that controls an LED so that when the level is below the threshold, the LED is off and when the level passes above the threshold, the LED turns on. Additionally, I want the LED to remain on when the level is above the threshold for 30 minutes before turning off again. I’ve got my circuit set up ok , the issues I’m having are bringing in the timing function. If anyone could give any advice on how to incorporate that into the following sketch I’d be very grateful!

Here is the simple sketch I’m working on:

const int analogPin = A0; // pin that the sensor is attached to
const int ledPin = 9; // pin that the LED is attached to
const int threshold = 550; // an arbitrary threshold level that’s in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);

}

void loop() {
// read the value of the sensor:
int analogValue = analogRead(analogPin);

// if the analog value is high enough, turn on the LED:
if (analogValue > threshold) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin,LOW);
}

}

The easiest way would be to use delay after you turn the led on. If your system will expand later to do other things, you'll want to do your timing with millis. Look at the blink without delay example sketch provided with the IDE to see a simple example of controlling a led using millis.

Thanks very much for the reply Wildbill. I was actually messing around with adding a delay. It's just that if the analogValue is above the threshold, the LED is on and it stays on after the delay time is up. I'm wondering if there is a way for the light to be switched off after the set time (30 mins) is up even if the analogValue is above the threshold.. Thanks

I’m wondering if there is a way for the light to be switched off after the set time (30 mins) is up even if the analogValue is above the threshold.

Yes. You need to separate the function of reading the light state and deciding whether the LED should be on from the function of turning the LED on or off.

Then, you can turn the LED on if it needs to be on (it’s dark). Record when that happened.

Then, you can turn the LED off it is on and has been on for the required time.

Looking at the blink without delay and the state change detection examples should give you enough clues (that is, you only need to turn the LED on when the light level is low but was not).

Thanks very much for the info Paul, it's much appreciated! I've been studying the examples that you directed me to, I'm just such a noob that I still can't quite figure out how to adapt it to my needs..

This is the code that I've got so far (it may be way off!)

const int ledPin = 9; const int sensePin = A0; const int threshold = 300;

int ledState = LOW;

long previousMillis = 0;

long interval = 10000;

void setup() {

pinMode(ledPin, OUTPUT); pinMode(sensePin, INPUT); Serial.begin(9600);

}

void loop() {

int senseValue = analogRead(sensePin);

if (senseValue > threshold) { digitalWrite(ledPin, HIGH); } else{ digitalWrite(ledPin, LOW); } unsigned long currentMillis = millis();

if(senseValue > threshold && currentMillis - previousMillis > interval) {

previousMillis = currentMillis;

if (ledState == HIGH) ledState = LOW;

digitalWrite(ledPin, ledState); } Serial.println(senseValue); delay(100); }

Note that I'm just using the 10000 milliseconds as a arbitrary number to check the code. I feel like I am getting somewhat closer to what I would like to achieve. Now when the senseValue is above the threshold and the 10000 millisecond interval is reached, the LED turns off for a split second before turning back on again. Can anyone tell me how I can keep the LED off after the 10000 milliseconds is reached AND the senseValue is above the threshold? Thanks!

if (senseValue > threshold) {
  digitalWrite(ledPin, HIGH);
    }
    else{
    digitalWrite(ledPin, LOW);
    }

If it is light turn the LED on. If not, turn it off. How does that match up with your requirement to have the light remain on for some time after the light level changes?

You are not separating the need to turn the light on from the act of turning the light on.

Thanks Paul. Yes, I realise that first part makes no sense at all!

You are not separating the need to turn the light on from the act of turning the light on.

Sorry I know I should understand how to demonstrate this but I do not. Can you please explain or give an example of this in very simple terms. Thanks.

This code determines whether the light needs to be on, or not. It does NOT turn the light on or off, since the light level is only one factor in determining whether the light should be turned on or off.

bool lowLight = true;

void loop()
{
   int senseValue = analogRead(sensePin);
   if (senseValue > threshold) 
   {
     lowLight = false;
   }
   else
   {
     lowLight = true;
   }
}

See if you can add to this code. You need to keep track of when the light was turned on, if it is turned on. You need to decide whether to turn the light on or off. You know that one of the factors is lowLight. What are the others? Time that the light has been on, for one thing.

Also, the code above does not detect a transition from dark to light or light to dark. That may, or may not, matter in your project.