Plz help with math

Hello All

Its my first time to get in forum: I am stuck in a simple math, (i am beginner)

I managed to make a ds1307 clock with predefined on and off time for water pump. Problem is that when it checks with predfined time, motor does on with minutes and hour but if hour is more than or less than current time, it does not on motor.

eg; current time: 3:15

on time: 3:10; off time 3:54

MOTOR ON (stays one between on and off time when it reaches)

if it is : current time: 3:15
on time 3:10; off time 4:05

MOTOR OFF(does not ON between on and off time when it reaches)

problem is here

//-----------relay------
void relay(){
DateTime now = rtc.now();
** if (((now.minute()>=mmm)&&(now.minute()<=(offmmm-1)))&&((now.hour()>=hhh)&&(now.hour()<=(offhhh))))**
{Serial.print("\t MOTOR ON "); digitalWrite(13,HIGH);lcd.setCursor(0,1);motor=1;}
else {digitalWrite(13,LOW); motor=0;
}

complete_donewithLCD.ino (9.5 KB)

RTClib.cpp (9.17 KB)

RTClib.h (2.93 KB)

pitches.h (1.96 KB)

The way I’d be inclined to tackle this is to first create a function that combines Hours and minutes into a single value like so

int totalMins(int Hours,int Mins)
{
return Hours*60 + Mins;
}

Then within my loop function I’d convert the dueOn, dueOff, and current times into integers, using this method.

Then it’s a simple matter to determine that the current time is within the specified time.
ie

void loop()
{
int dueOn=totalMins(3,10);
int dueOff=totalMins(3,15);
int current=totalMins(now.hour,now.minute);

if((current>=dueOn)&&(current<=dueOff))
 {Serial.print("\t MOTOR ON  "); 
  digitalWrite(13,HIGH);
  lcd.setCursor(0,1);motor=1;
  }
  else {digitalWrite(13,LOW); motor=0;
 } 
}

+1 for KenF method

one small remark: the off time should not be included

if ( (current >= dueOn ) && (current < dueOff ) ) // CHANGED <= TO <

robtillaart:
+1 for KenF method

one small remark: the off time should not be included

if ( (current >= dueOn ) && (current < dueOff ) ) // CHANGED <= TO <

Yep, that would be slightly more accurate. :blush:

if (((now.minute()>=mmm)&&(now.minute()<=(offmmm-1)))&&((now.hour()>=hhh)&&(now.hour()<=(offhhh))))

The number of parentheses in that statement is ridiculous. This should be broken into a minimum of two if statements, so you can debug it.

First, test that the hour is correct. If it isn’t, then the minute value does not matter.

In the end, though, you are going to go with KenF’s approach, or you are going to go crazy.

Thanks to All especially KENF:

should i change
if ( (current >= dueOn ) && (current < dueOff ) ) // CHANGED <= TO <

to

if(current>=dueOn)
{Serial.print("\t MOTOR ON ");
digitalWrite(13,HIGH);

I will try it today and inform.

Thanks

Sir one query more:

Plz have a look at code: I am also facing buttons problems like menu style, anyhow it is crude and just working but not as it should be. can you give a simple program by which as a guide i make menu, get in it and set variable and exit etc. I found libraries, but for me it was very complicated. In my code it works, but in crude way.

Only one site came, which is in russian who made such a program, which hold data for days, week in programming and uses just one IO for multiple buttons, but he just gave hex file not program. if you have and referral site, plz. Ready made devices are available online, i ordered one from china, but to make myself will boost me in programming and is really interesting though this prototype cost me about $30 where chinese are selling it $8.

Your all input will be very much appreciated and light for beginner like us.

thanks

Looking through your code I find it difficult to understand where your code for reading buttons is located. Furthermore I find it difficult to understand what options you want to give to your user.

If you could give these details I'm sure we could work something out.

Sir, i know my code is messy. It is like this

int sethour = digitalRead(2);// is menu and also increment hours int setminutes = digitalRead(3);// increment minutes int exitset = digitalRead(4); // exit

when we press menu button, it goes in loop to set motor ON TIME where pressing menu again will increment hour and minute button will increase minute and exit will exit loop. Then it goes for OFF TIME where same happen and after exiting it will exit to real time clock. I want to simplify it and make button nice handling.

sir can u give me a sample code for menu (simplified) style with debounce button:

menu > set ON TIME > set minutes, hour > save or not save > set OFF TIME > set minutes, hour > save or not save >exit sample code will be easy to digest and i will be able to implement it on my program and increase my knowledge.

As someone who's been programming for many years, I can tell you that overuse of magic variables (e.g. i, x, mmm) will do more harm than good, especially when asking others for help with your code. Try and reformat your code and edit your post.