LED ON/OFF at particular time without RTC

I found problem guys, maybe you could fix it.
So I decided to make LED OFF just 1 minutes after I give voltage to the arduino
by

   if(minutes > 0 && minutes < 1){
    digitalWrite(LED, LOW);
    delay(0);
    }
    else {

But it doesnt work.

Here is the full code

#define LDR 0
#define PIR 2
#define LED 3

int pirState;
int ldrValue;

void setup() {
  //Serial.begin(9600);
  pinMode(LED, OUTPUT);
  pinMode(PIR, INPUT);
  digitalWrite(LED, LOW);
}
  //thhis is a list of int variables used in this clock program
int s=0;
int sec=0;
int hrs=0;
int minutes=0;
int initialHours = 02;//variable to initiate hours
int initialMins = 0;//variable to initiate minutes
int initialSecs = 00;//variable to initiate seconds

//this method is for seconds
int seconds()
{
    s = initialHours*3600;
    s = s+(initialMins*60);
    s = s+initialSecs;
    s = s+(millis()/1000);
    return s;
}
//this method is for hours
int hours()
{
    hrs = seconds();
    hrs = hrs/3600;
    hrs = hrs%24;
    return hrs;
}
//this method is for minutes
int mins()
{
    minutes = seconds();
    minutes = minutes/60;
    minutes = minutes%60;
    return minutes;
}

int secs()
{
    sec = seconds();
    sec = sec%60;
    return sec;
}
//this loop will conintue to keep looping so that the time can go as follows

void loop(){
   if(minutes > 0 && minutes < 1){
    digitalWrite(LED, LOW);
    delay(0);
    }
    else {
      
    

  
  ldrValue = analogRead(LDR);
  Serial.println(ldrValue);
  if (ldrValue <= 10) { // dark
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED, LOW);
      delay(0);
  } 
  else { // ldrValue > 512
 
  pirState = digitalRead(PIR);
  Serial.println(pirState);
  if (pirState == HIGH) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
  else { // pirState == LOW
      digitalWrite(LED, LOW);
      delay(0);
  }
    }
    } 
  // The processing in the Arduino occurs faster
  // than the response from the PIR, and adding this delay
  // eliminated a flickering on the LED
  delay(0);
    }
  if(minutes > 0 && minutes < 1)

You think this might something to do with it ?

raschemmel:

  if(minutes > 0 && minutes < 1)

You think this might something to do with it ?

TT

if no, how is the right code?

makanbakso:
TT

if no, how is the right code?

We gave you the answer. Just stare at it for a while. It's fairly obvious.

and when you'll have the code corrected according to Raschemmel's post.....
.... it still won't work !

how does "minutes" increase ?
if you never call mins() in your loop() , minutes is always 0

OP, if you haven’t picked up on this yet, if(minutes > 0 && minutes < 1){ does not include 0 OR 1. You would need something like this:

if (minutes >= 0 && minutes <= 1) { // or something like this, someone else confirm?

also, there’s a much easier way to count seconds, minutes, and hours:

if (millis() - previousMillis >= oneSecond) {

    seconds++;
    if (seconds > 59) {

      minute++;
      seconds = 0;

    }

    if (minute > 59) {

      hours++;
      minute = 0;

    }

aarg:
We gave you the answer. Just stare at it for a while. It's fairly obvious.

alnath:
and when you'll have the code corrected according to Raschemmel's post.....
.... it still won't work !

how does "minutes" increase ?
if you never call mins() in your loop() , minutes is always 0

I have stare at it for 3 days to be honest, sir.
would you?

Make a list of all the possibilities and explain why each should or shouldn't work. Do that and we'll give you the answer.

makanbakso:
I have stare at it for 3 days to be honest, sir.
would you?

No, because I would analyze it.
The numbers greater than 0 are 1,2,3....
The numbers less than 1 are 0,-1,-2,-3...

There is no number that can be in both ranges.

@chummer1010 :

if (minutes >= 0 && minutes <= 1) { // or something like this, someone else confirm?

nope, this is true for minutes=0 too, but you’re not too far :wink:

and in the easier “solution” you gave, there are some mistakes :

  • where does ‘oneSecond’ come from and where is it initialized ?
  • where do you initialize and then change the ‘previousMillis’ value ?
  • ‘seconds’ , ‘minute’ and ‘hours’ should also be set to 0

Allow me to explain your real problem. You are not following the standard plan for development. First you should completely specify the problem. Then you should decide on the best general methods to solve it. Finally you should write code to implement those methods.

chummer1010:
OP, if you haven’t picked up on this yet, if(minutes > 0 && minutes < 1){ does not include 0 OR 1. You would need something like this:

if (minutes >= 0 && minutes <= 1) { // or something like this, someone else confirm?

also, there’s a much easier way to count seconds, minutes, and hours:

if (millis() - previousMillis >= oneSecond) {

seconds++;
   if (seconds > 59) {

minute++;
     seconds = 0;

}

if (minute > 59) {

hours++;
     minute = 0;

}

Where I can place the code? Sorry, i didnt get what you mean :frowning:

raschemmel:
Make a list of all the possibilities and explain why each should or shouldn’t work. Do that and we’ll give you the answer.

Here
First, I place it inside the code at the top of the loop (before LDR and PIR sensor code) like this

void loop(){
   if(minutes < 1){
    digitalWrite(LED, LOW);
    delay(0);
    }
    else {
      
    digitalWrite(LED, HIGH);
    delay(0);
    }
     
  ldrValue = analogRead(LDR);
  Serial.println(ldrValue);
  if (ldrValue <= 10) { // dark
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED, LOW);
      delay(0);
  } 
  else { // ldrValue > 512
 
  pirState = digitalRead(PIR);
  Serial.println(pirState);
  if (pirState == HIGH) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
  else { // pirState == LOW
      digitalWrite(LED, LOW);
      delay(0);
  
    }
    } 
  delay(0);
    }

Second, I have tried to place it between LDR and PIR

void loop(){


     
  ldrValue = analogRead(LDR);
  Serial.println(ldrValue);
  if (ldrValue <= 10) { // dark
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED, LOW);
      delay(0);
  } 
  else { // ldrValue > 512
   
   if(minutes < 1){
    digitalWrite(LED, LOW);
    delay(0);
    digitalWrite(LED, HIGH);
    delay(0);
    }
    else {
      
 
  pirState = digitalRead(PIR);
  Serial.println(pirState);
  if (pirState == HIGH) {
      digitalWrite(LED, HIGH);
      delay(1000);
    }
  else { // pirState == LOW
      digitalWrite(LED, LOW);
      delay(0);
  
    }
    } }
  delay(0);
    }

The third, I placed it below LDR and PIR code.
I think the problem is not about where I place the code. I dont know the additional code.
Comment above said it will never work if I not call mins(), but tbh I dont know where to place it. Seriously I am new in programming

alnath:
@chummer1010 :
nope, this is true for minutes=0 too, but you’re not too far :wink:

and in the easier “solution” you gave, there are some mistakes :

  • where does ‘oneSecond’ come from and where is it initialized ?
  • where do you initialize and then change the ‘previousMillis’ value ?
  • ‘seconds’ , ‘minute’ and ‘hours’ should also be set to 0

I should have just posted my whole code:

#include <LiquidCrystal.h>

byte seconds = 0;
byte minute = 0;
byte hours = 0; // minutes, secs, hours,

unsigned long previousMillis = 0;
const long oneSecond = 1000UL; // for timing

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // for an lcd

void setup() {

  Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.clear();
  lcd.print("clock");
  delay(1000);
  lcd.clear();

}

void loop() {

  if (millis() - previousMillis >= oneSecond) {

    seconds++;
    if (seconds > 59) {

      minute++;
      seconds = 0;

    }

    if (minute > 59) {

      hours++;
      minute = 0;

    } // actual counter

    
    char currentTime[10] = "";
    snprintf(currentTime, sizeof(currentTime), "%02d:%02d:%02d", hours, minute, seconds);
    Serial.println(currentTime); // print to serial monitor
    lcd.setCursor(0, 0);
    lcd.print(currentTime); // print time to lcd screen
    previousMillis += oneSecond; // reset for next loop
  }
}

If you want to see this quickly, just comment out anything that has to do with an LCD screen, upload this, and check serial monitor.

Here
First, I place it inside the code at the top of the loop (before LDR and PIR sensor code) like this

Is that what I said ?

Possibilities:
1-minutes <0
2- minutes = 0
3- minutes >0
4- minutes > 0 && minutes < 1
5- minutes > 0 && minutes = 1
6 - minutes > 0 && minutes >1

Think about it .
Which one do you want ?

aarg:
No, because I would analyze it.
The numbers greater than 0 are 1,2,3…
The numbers less than 1 are 0,-1,-2,-3…

There is no number that can be in both ranges.

Oh ya -_-
but, if I change

if (minutes >= 0 && minutes <= 1)

to

if (minutes >= 0 && minutes <= 4) // or 5 or 6 until infinitife

the code still wont work

aarg:
Allow me to explain your real problem. You are not following the standard plan for development. First you should completely specify the problem. Then you should decide on the best general methods to solve it. Finally you should write code to implement those methods.

Yes sir, I know the logic, but the problem is the sentence that I bold above

raschemmel:
Is that what I said ?

Possibilities:
1-minutes <0
2- minutes = 0
3- minutes >0
4- minutes > 0 && minutes < 1
5- minutes > 0 && minutes = 1
6 - minutes > 0 && minutes >1

Think about it .
Which one do you want ?

3 because higher than 3, the LED should OFF

1 is not possible bcs there is no -1, -2 minutes
2 possible but that is not what I need
4 and 5 is not possible bcs there is no 0,1;0,2;0,3 minutes even in real world it is possible
6 :smiley:

@chummer1010

const long oneSecond = 1000UL; // for timing
:wink:
const unsigned long oneSecond = 1000UL; // for timing

previousMillis += oneSecond; // reset for next loop
FYI:

@chummer1010 yes, now, it's OK :slight_smile: - it is always better to post the full code, especially if OP is a beginner :wink:

@makanbakso, writing the code for you is not the way to help you, is it ? As a beginner, you need to understand how your code works - One way to do that is probably to try the examples in the IDE one by one, and really understand it before you try the next one.

Possibilities:
1-minutes <0
2- minutes = 0
3- minutes >0
4- minutes > 0 && minutes < 1
5- minutes > 0 && minutes = 1
6 - minutes > 0 && minutes >1

3 because higher than 3, the LED should OFF

So I decided to make LED OFF just 1 minutes after I give voltage to the arduino

Does 3 meet your required time ?

1 is not possible bcs there is no -1, -2 minutes
2 possible but that is not what I need
4 and 5 is not possible bcs there is no 0,1;0,2;0,3 minutes even in real world it is possible
4- minutes > 0 && minutes < 1 (minutes , for the purpose of this discussion is an integer and thus must be a whole number , either 0,1, 2 , 3 etc so <1 is the same as 0

5- minutes > 0 && minutes = 1
Is 1 > 0 ?
Does 1 minute meet your required time stated here ?

So I decided to make LED OFF just 1 minutes after I give voltage to the arduino

LarryD:
@chummer1010

const long oneSecond = 1000UL; // for timing
:wink:
const unsigned long oneSecond = 1000UL; // for timing

previousMillis += oneSecond; // reset for next loop
FYI:
http://forum.arduino.cc/index.php?topic=396591.0

Thank you for presenting me with this information!