Ldr sensor problem

Hi ı am new on aurdino ı havent any experince about.I I found this code in this form.I I tried its working but when ldr sensor read <50 its waiting 5 sec that arrival time.When lights on its waiting
10 sec that ontime value.This problem here I would like when become dark ı mean <50 milis timer enable 10 sec or more when lights on its wait arrival time or none.Can you help me about that problem thank you.

int sensorPin = A1; // ldr connected to A1
int sensorValue; // variable to store the value coming from the ldr
int testLED = 2;
boolean timing = false;
boolean lightsOnCycleActivated = false;
unsigned long startTime;
unsigned long onTime = 10000;//
unsigned long analogReadInterval = 5000;
unsigned long lastTimeRead = millis();

void setup()
{
Serial.begin(9600); // initialize the serial port
pinMode(testLED, OUTPUT); // sets digital pin 9 to be an output
}

void loop()
{
if (millis() - lastTimeRead >= analogReadInterval)//read ldr periodically
{
lastTimeRead = millis();
sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
Serial.println(sensorValue);
}

if (sensorValue <= 50 && lightsOnCycleActivated == false) //sunset first reading
{
lightsOnCycleActivated = true;
timing = true; //start lights on timing
startTime = millis();
digitalWrite(testLED, HIGH); //turns on led connected to digital pin 9
}

if (timing && millis() - startTime >= onTime)
{
digitalWrite(testLED, LOW);
timing = false;
}

if (sensorValue >= 50 && lightsOnCycleActivated == true) // next sunrise
{
lightsOnCycleActivated = false; //re-enable light cycle for next sunset
}
}

Hi, @lenovo13

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you please post a circuit diagram of your project?

Tom... :grinning: :+1: :coffee: :australia:

Could you please explain your requirement, but without any programming references such as talking about millis() etc.? Please do that in your native language and then use Google translate to translate to English.

Yes it will, because that is what is written in the code.
So I guess you have no idea what the code is doing.

The lines

Set the time in milliseconds for the onTime 10000 mS is the same as 10 seconds, and the read interval is set to 5000 mS or 5 seconds.

Change these values for other times.

Thank you for massages ı would like to work reverse .I mean when the sun down(become dark) aurdino wait 10 sec then led on.When the sun up aurdino wait 5 sec then led off.Hoe can ı change the code for this stiuation.İts true ıam native :smiley: I dont want to use delay because ı tried not work well because ı will change sec to hour or more.So that ı save energy for led,leds.

When is the code due to be handed in? Do you need notes on how it works provided as well?

[SOLVED] Timer problem - Programming - #9 by cattledog ı change only pin but not solved for me:D

delay() can work for hours, and using it may be fine for your code depending what else is going on.

Please post the code you tried using delay(). Explain in detail what it did not do correctly regarding the timings of the on/off of the leds.

1 Like

Thank you for post.I tried with delay common code which is on the internet but delay is working only one value for all loop.So that ı use your code.Thanks for code and I change code below.Now works fine with computer.İf countinue 49 day enough for me.

Code

int sensorPin = A1; // ldr connected to A1
int sensorValue; // variable to store the value coming from the ldr
int testLED = 2;
boolean timing = false;
boolean lightsOnCycleActivated = false;
unsigned long startTime;
unsigned long onTime = 7500000  ;
unsigned long analogReadInterval = 300000  ;
unsigned long lastTimeRead = millis();

void setup()
{
  Serial.begin(9600); // initialize the serial port
  pinMode(testLED, OUTPUT); // sets digital pin 2 to be an output
}

void loop()
{
  if (millis() - lastTimeRead >= analogReadInterval)//read ldr periodically
  {
    lastTimeRead = millis();
    sensorValue = analogRead(sensorPin); // read the value from the voltage sensor
    Serial.println(sensorValue);
  }

  if (sensorValue >= 20 && lightsOnCycleActivated == false) //sunset first reading
  {
    lightsOnCycleActivated = false;
    timing = true; //start lights on timing
    startTime = millis();
    digitalWrite(testLED, LOW); //turns on led connected to digital pin 2
  }

  if (timing && millis() - startTime >= onTime)
  {
    digitalWrite(testLED, HIGH);
    timing = false;
  }

  if (sensorValue <= 20 && lightsOnCycleActivated == true) // next sunrise 
  {
    lightsOnCycleActivated = false; //re-enable light cycle for next sunset
  }
}

Hi, @lenovo13

To add code please click this link;

Before posting please press < CTRL > and < T > to auto format your code?

Tom... :grinning: :+1: :coffee: :australia:

1 Like

I'm sure that will be appreciated. :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.