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
}
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.
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 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.
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
}
}