Time betwen 2 condition

I want to make relay on and off when it's:
on 19:2:0 and off 19:3:30
but why this code make relay off in two times?
on at 19:2:0 then off at 19:2:30
on again at 19:3:0 then second off at 19:3:30
anyone can help me, please.

if
((waktu.Hour==19)&&(waktu.Minute>=2)&&(waktu.Minute<=3)&&(waktu.Second<=30)){digitalWrite(relay,LOW);}

Welcome to the forum

Let's start by formatting the code in a nicer way to make it easier to see


    if ((waktu.Hour == 19) && (waktu.Minute >= 2) && (waktu.Minute <= 3) && (waktu.Second <= 30))
    {
        digitalWrite(relay, LOW);
    }

The relay pin will go LOW any time that all of the conditions are true

The conditions are as follows :

  • hour has to equal 19
  • minute is greater than or equal to 2 so it will be true for values of 2, 3 and greater
  • minute is smaller than or equal to 3 so it will be true for 0, 1, 2 and 3
  • second has to be less than or equal to 30

Look at the 2 middle conditions. They will both be true if minute equals 2 or minute equals 3

How to make it start on at 19:2:0 and off at 19:3:30?
or something like
start at 19:2:30 and stop at 19:3:30

    if (waktu.Hour == 19 && waktu.Minute == 2 && waktu.Second == 30)
    {
        //turn on the relay
    }
    else if (waktu.Hour == 19 && waktu.Minute == 3 && waktu.Second == 30)
    {
        //turn off the relay
    }

All time comparisons using hours, minutes and seconds get complicated as you have seen.

A technique time-tested (see what I did there?) is to convert HH:MM:SS three variables into one number, seconds since midnight.

Then you can see if your time, expressed in seconds since midnight, is before, at or after the current time also expressed i seconds since midnight.

HTH

a7

simply calculate the second of the day

secondOfDay = hour * 3600 + minutes * 60 + seconds;
on the example-numbers that you have given

switchOnTime = 19 * 3600 + 2* 60 + 30;
switchOffTime = 19 * 3600 + 3* 60 + 30;

switchOnTime: 19 * 3600 + 2* 60 + 30 = 68550

switchOffTime: = 19 * 3600 + 3* 60 + 30 = 68610

If (actualSecondOfDay == switchOnTime) {
  switch on
}

If (actualSecondOfDay == switchOffTime) {
  switch off
}

best regards Stefan

Right. Another tip for time is to not use exact equality, as it means you have to be checking exactly within a one second window.

So do

  if (actualSecondOfDay >= switchOnTime) {
      //…  switch on
  }

instead. Then if you check any time after the switchOnTime, you still OK.

You'll need to work out some minor logical implications of using >= greater than or equals to.

Also, if you are using large integer numbers, make sure your variables are declared long, and when you use big long numbers, make sure you

     switchOffTime = 68610L;

Properly write the constant with an 'L' for long. Best to print any numbers like that and check so you don't get a surprise.

HTH

a7

If the time is after the switchOffTime, you will get a brief on/off cycle every time this check is made. Much better to test to see if you are within the on window or not

If (actualSecondOfDay >= switchOnTime && actualSecondOfDay < switchOffTime) {
  //switch on
}
else {
  // switch off
}

Yes, much better, THX. Imma say that's what I meant by implications. But I shoulda seen that.

It's the kind of mistake that would be hard to see, while one was wondering what was going wrong what might be caused by a "a brief on/off cycle", which might not matter to an LED but not everything switched on and off might be so forgiving.

a7

a7

thanks, it work.!
can we make transfer data from spreadsheet data into arduino?
i have time table like this

or i must do it manualy?

What do you want to do with the data ?

Save it from Excel as a .csv file and manipulate it with a text editor using copy/paste/replace etc

Has your Arduino got enough memory to hold this much data in say an array of structs ?

i want to run the motor as per this table.
I'm using Arduino Uno, is there enough memory?

How many rows of data are there ?

30 rows

I suggest that you start by using Excel to convert each cell containing a time into seconds since midnight, then export them as a .csv file

The data is then practically in the right format to put into a 2 dimensional array. You could put that in PROGMEM on the Uno to save memory if necessary

Once in the array have you worked out how you are going to use the data ? Perhaps start with a few rows and columns entered manually

You are asking for almost each and every detail of your project. How much are you gonna pay me if I realise your project?

Look up the specs of an arduino uno

  • how much flash rom?
  • how much RAM?

lookup how many bytes a single variable of
type unsigned long needs
then calculate
16 columms * 30 rows * bytes per unsigned long.
compare the number with the memory of the arduino uno

best regards Stefan

That table has a lot of repeating patterns.
I’m pretty sure you figure out the times with a couple of lines of code,

Yes, it mean can be more simple code?

If it's not a secret, why are the times so odd?

Also, minutes MM is always zero. All you need for a fake time like seconds since midnight is actually just a number that is guaranteed to get bigger.

So

  magicTime = hours * 100 + seconds;

would be integers between 0 and 2359. Just cut your data in half, that.

Now the on time is hours only, and only 8 different hours, so you can store that in three bits. The other time is the same hour, and 59 max, that's a byte or really only 6 bits.

Each event of on-ness for this odd table is 9 bits.

There are libraries that make using N bit numbers easy with minimum possible storage. I'd have to do more work I am too lazy to do to see the code / bits of storage tradeoff.

Games to play.

But srsly, what needs to run for 12 seconds one day and 13 the next?

a7