If statement help pls

Hey all,
Im having a little trouble trying to get an IF statement working properly. It was working fine but only when the program had run a full 24hrs then all settings were good, but if the power went out or I turn it on after an IF statement time that If statement will not work until the next time around.
So I added in >= and it worked until the the next IF statement when it needs to turn off. Can someone please help me with the next if statement.
EDIT: So what I want to happen is Pin1 to be on between a time. So the code here says from 1030 to 1236. With this code the 2 If statements counteract each other and the pin1 goes mental.

  if(now.hour() >= 10 && now.minute() >= 0 && now.second() >= 0) {
      tft.setCursor(4,1);
      tft.print("on");
      digitalWrite(Pin1, LOW);
   }
  if(now.hour() == 12 && now.minute() == 36 && now.second() == 0) {
      tft.setCursor(4,1);
      tft.print("off");
      digitalWrite(Pin1, HIGH);

Thank you :slight_smile:
Macca

macca05:
Can someone please help me with the next if statement.

You have not explained what you want to happen

…R

Thanks. I didnt realise that. I just edited the first post :slight_smile:

Hi,

Post all your code from the start in order to be able to help you.

Ok cool will have to post it tmrw as im at work tonight :slight_smile:

When you get to 10 oclock, it will turn on. and keep turning on, every time Loop( ) runs. Once you turn it on, set a boolean flag that it is on, and then you don't need to keep turning it on again.

When you get to 12:36, then every time loop( ) runs, you will turn it on, and then off again. Because, at 12:36, the if conditions for the first and second if statements are both true. It will turn on and off as fast as loop runs.

When you get to 12:37, the first condition is STILL TRUE. So it turns on. The second condition is not true, so it won't turn off. It will stay on.

Thats the problem that i am having but i dont understand how to fix it. I have only been playing with arduino for about 2 weeks now.
Let me see if i can explain exactly what i want.
I basically want to create an if statement that would be something like this.

If ((now.hour >=10 && now.minute >= 30) (now.hour <=12 && now.minute <=30)) {
tft.setCursor(4,1);
tft.print(“on”);
digitalWrite(Pin1, LOW);

Now i just made that up and dont really think it is code language, but what im trying to get it to do is allow the relay to be on during those times. So incase of loss of power during 1130 to 1150 when it turns back on it will turn the relay back on again as it should be. Hope that makes sense

I'm thinking maybe the now minute is conflicting sense it's both now minute but two opposite ifs it gets confused???

If ((now.hour >=10 && now.minute >= 30) (now.hour <=12 && now.minute <=30)) {It is not clear to me what you want to do. Can you please explain in English rather than code ?

How about swapping the order of the tests. Test for ==12 before testing for >= 10 because >= 10 also includes 12. Maybe you should just have ==10 ?

…R

I think you should try something more like this :

 if((now.hour() >= 10 && now.hour()<12) && now.minute() >= 0 && now.second() >= 0) {
      tft.setCursor(4,1);
      tft.print("on");
      digitalWrite(Pin1, LOW);
   }
  if(now.hour() == 12 && now.minute() == 36 && now.second() == 0) {
      tft.setCursor(4,1);
      tft.print("off");
      digitalWrite(Pin1, HIGH);

I don’t think you need to test for now.minute() >=0 or now.second >=0.
I’m pretty sure they’re both always going to be greater or equal to 0.

if( (now.hour() >= 10) && (now.hour() < 12 || (now.hour() == 12 && now.minute() < 36) ) )
{
    digitalWrite(Pin1, LOW);
} 

if( (now.hour() < 10) || (now.hour() == 12 && now.minute() >= 36) )
{
    digitalWrite(Pin1, HIGH);
}

Casou:
I think you should try something more like this :

 if((now.hour() >= 10 && now.hour()<12) && now.minute() >= 0 && now.second() >= 0) {

tft.setCursor(4,1);
     tft.print(“on”);
     digitalWrite(Pin1, LOW);
  }
 if(now.hour() == 12 && now.minute() == 36 && now.second() == 0) {
     tft.setCursor(4,1);
     tft.print(“off”);
     digitalWrite(Pin1, HIGH);

Are you really expecting now.minute() or now.second to return a value below 0?
By pure logic the two functions should always return 0, 59 or somewhere therein.

evanmars:
I don’t think you need to test for now.minute() >=0 or now.second >=0.
I’m pretty sure they’re both always going to be greater or equal to 0.

if( (now.hour() >= 10) && (now.hour() < 12 || (now.hour() == 12 && now.minute() < 36) ) )

{
   digitalWrite(Pin1, LOW);
}

if( (now.hour() < 10) || (now.hour() == 12 && now.minute() >= 36) )
{
   digitalWrite(Pin1, HIGH);
}

First of all it is guaranteed that only one of them should ever be true at the same time, by making one of them into a else if() most of the time only one of the checks needs to be done.
Besides why are you first testing if it’s lower than 12 then test if it’s same as 12.

macca05:
o what I want to happen is Pin1 to be on between a time. So the code here says from 1030 to 1236.

The way I understand it is that the could would be like this:

if (( now.hour() >=10) && (now.hour() <=12) && (now.minute() >=30) && (now.minute() <=36))
{
    digitalWrite(Pin1, HIGH);
}
else // Unless the time is between 10:30 and 12:36 then it should be turned off
{
    digitalWrite(Pin2, LOW);
}

MisDev:
Besides why are you first testing if it’s lower than 12 then test if it’s same as 12.
The way I understand it is that the could would be like this:

if (( now.hour() >=10) && (now.hour() <=12) && (now.minute() >=30) && (now.minute() <=36))

{
    digitalWrite(Pin1, HIGH);
}
else // Unless the time is between 10:30 and 12:36 then it should be turned off
{
    digitalWrite(Pin2, LOW);
}

That code would only have it turn on if it was between 10:30 and 10:36, or 11:30 and 11:36 or 12:30 and 12:36, then off any other time. Or actually, it looks like your code would turn it off when it was supposed to be on and on when it is supposed to be off. He takes the pin LOW to turn on the LED

It needs to be on any time after 10:00 and before 12:00, or any time from 12:00 to 12:36

EDIT:
Now I see an error in my code, there also needs to be a test if now.hour() == 10 and now.minute >= 30.

Hey everyone,
sorry for not replying last night. Had night shift and it got busy (Ambulance officer). Thanks for all the input so far. Seems like a good discussion :slight_smile:

Im sorry if I may be confusing some of you with my non code language speak :slight_smile:
This is my original code which works fine.

// RELAY 1 = LIGHT 1
    if(now.hour() == 10 && now.minute() == 0 && now.second() == 0) {
    tft.setCursor(4,1);
      tft.print("on");
      digitalWrite(Pin1, LOW);
   }
  if(now.hour() == 12 && now.minute() == 36 && now.second() == 0) {
      tft.setCursor(4,1);
      tft.print("off");
      digitalWrite(Pin1, HIGH);
  }

However, if I plug the arduino in at 1050 and the arduino loads this relay gets skipped because the code did not run before 10am to turn the relay on. So this means that it will not turn RELAY 1 on until a further 24hrs have passed by.

So, is there a simple way that when the arduino turns on, it will run through the coding and check to see if any relays should already be on and turn them on and continue as it should.

This is all my code that I have been working on and so far it works great, but wish I had the added feature Im trying to explain above.

I can post all my code if needed if someone has the necessary parts to test it :slight_smile:
20x4 lcd screen
arduino uno (could be any i think)
RTC1307
8 channel relay

Thanks again for all the help :slight_smile: I love working with arduino. Its so addictive :slight_smile:

macca05:
However, if I plug the arduino in at 1050 and the arduino loads this relay gets skipped because the code did not run before 10am to turn the relay on. So this means that it will not turn RELAY 1 on until a further 24hrs have passed by.

That is very much clearer.

If you want the relay to be on between 10:00 and 12:00 you could test with

if (now.hour() >= 10 && now.hour() < 12 && relayON == false) {
// turn on
relayON = true;
}

…R

Robin2:
That is very much clearer.

If you want the relay to be on between 10:00 and 12:00 you could test with

if (now.hour() >= 10 && now.hour() < 12 && relayON == false) {
// turn on
relayON = true;
}

…R

Thank you for this. However i am unsure as to where this coding should go. Do i replace what i have already or add this in. Is relayON meant to be pin1 so pin1 = LOW
Sorry for the silly question. So much to get my head around