Measure light and holding the LED on when intensity is exceeding .

Dear Forum Members,

I am very new to the Arduino with very little of background and knowledge and kindly asking you for help...

I am building a simple light detector / measurement, using LDR which. I am sure that most of you know it as it is very popular in Google search.

On the original sketch, the light value are continuously and fastly updated to the serial monitor. I modify the sketch, so the light value is updated very fast, e.g. 50msec and when the light intensity is exceeding the limit, it will turn the LED on and hold the LED at least for 3 seconds, without affecting (or adding more delay) to the 50msec update rate. I am trying this sketch but it does not work: on previous version, it add 3second delay to the update rate and on this version, the LED is not turn on;

/* The circuit requires LDR and 10k fixed resistor

  • The resistor connects to +5V and LDR connects to GND
  • LDR and pin junction connected to A0
    */

int led = 13; //LED, digital pin 13
int senRead = 0; //Readings from sensor to analog pin 0
int limit = 100; //Light threshold
void setup()
{
pinMode(led, OUTPUT);
Serial.begin(115200); //Set serial monitor at baud rate of 115200
}
void loop()
{
int val = analogRead(senRead); //Values from the sensor
Serial.println(val); //Prints the values from the sensor in serial monitor
delay(50); //Update rate sent to serial monitor
}
void HoldLED (int val)
{
if (val < limit) //If the light falls below the threshold it should turns the LED on for 1.5sec
{
digitalWrite(led, HIGH); //LED ON
delay(1000);
digitalWrite(led, LOW); //Then turns the LED back off
delay(3000); //And prevent the LED turned on for at least 3seconds
}
else if (val > limit) //If the light level is higher then threshold the LED should remain off
{
digitalWrite(led, LOW); //LED OFF

}
}

Forgive me should I make any mistakes on this posting.

Your help and attention is greatly appreciated.

Best Regards,

Welcome to the Forum. Of course you are forgiven, but you should have read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question ... and
Planning and Implementing an Arduino Program

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

Many questions can be answered by reading the documentation which is provided with the IDE, available under the help tab, or online here.

After you have corrected the format of your code for presentation here, you could also look at where your function HoldLED() is called.

Thank you very much for giving me some guide on using this forum. Next time should be better to me. Now I try to fix the code format.

/* The circuit requires LDR and 10k fixed resistor
 *  The resistor connects to +5V and LDR connects to GND
 *  LDR and pin junction connected to A0
 */


int led = 13; //LED, digital pin 13
int senRead = 0; //Readings from sensor to analog pin 0
int limit = 100; //Light threshold
void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(115200); //Set serial monitor at baud rate of 115200
}
void loop()
{
  int val = analogRead(senRead); //Values from the sensor
  Serial.println(val); //Prints the values from the sensor in serial monitor
  delay(50); //Update rate sent to serial monitor
}
void HoldLED (int val)
{
  if (val > limit) //If the light exceed the threshold it should turns the LED on for 1.5sec
  {
    digitalWrite(led, HIGH); //LED ON
    delay(1500);
    digitalWrite(led, LOW); //Then turns the LED back off
    delay(3000); //And prevent the LED turned on for at least 3seconds
  }
  else if (val < limit) //If the light level is lower than threshold the LED should remain off
  {
    digitalWrite(led, LOW); //LED OFF
    
  }
}

*Some correction is made on the code comments.

I hope this can help me. Thank you very much for your help and attention.

Best regards,

In the version that worked, did you have a call to the function HoldLED() in the main loop? As you have currently structured the program, this function is never called.

The previous version is not worked, but it add 1500 plus 3000msec and the new version, the LED never goes high...

6v6gt:
In the version that worked, did you have a call to the function HoldLED() in the main loop? As you have currently structured the program, this function is never called.

You gave me an enlightenment. Almost understand. I'll try...

Thank you very much.

@Synnod, you need to take the delay() functions out of your code and use millis() to manage the timing as illustrated in Several Things at a Time and in Planning and Implementing a Program which you have already been given the link to.

The Arduino can do nothing else during a delay() period.

...R

@Robin2, very interesting and thank you very much for your suggestion. As a very new to Arduino, I have to learn more on how to make delay using millis (). May be you could give me suggestion too :slight_smile:

Synnod:
May be you could give me suggestion too :slight_smile:

That is the purpose of the links that I gave you.

If there is something about them that you don't understand please ask a specific question.

...R