how to use button interface in clock

i am making a pump timer for my aquarium

and the code follows:

int relayPin=5;
#include <Wire.h>
int ontime=16;
int offtime=17;
#include “RTClib.h”
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
RTC_DS1307 RTC;

void setup () {

Wire.begin();
RTC.begin();
lcd.init(); // initialize the lcd
lcd.backlight();

pinMode(5, OUTPUT);//Any pin. I have used Pin 4
}

void loop () {
DateTime now = RTC.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print(’/’);
lcd.print(now.month(), DEC);
lcd.print(’/’);
lcd.print(now.day(), DEC);
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(’:’);
lcd.print(now.minute(), DEC);
lcd.print(’:’);
lcd.print(now.second(), DEC);
lcd.setCursor(0, 2);
lcd.print(“relay ontime:”);lcd.print((ontime));lcd.print(“hrs”);
lcd.setCursor(0,3 );
lcd.print(“relay offtime:”);lcd.print((offtime));lcd.print(“hrs”);
delay(1000);
if(now.hour() >= ontime && now.minute() >=37 && now.hour() < offtime)
{
digitalWrite(5, HIGH);
}
else
{
digitalWrite(5, LOW);
}
}
.

my question is how do i add button to adjust the on and off time which should not exceed 24hr

Solder?

Please remember to use code tags when posting code.

See "Useful links" at the top of the forum, and check out "How to do several things at a time"

:slight_smile:

If you want to set on and off time independently you would need at least two buttons - one for setting the time and the other for telling the program what you are setting (ON or OFF).

As the program is running it is in one of three modes: setting the ON time, setting the OFF time or running normally. Every time the mode button is pressed the program mode is changed. Press once to set ON time, again to set OFF time and again to resume normal operation.

If ON or OFF time is being set the button used to set time can be pressed or held down to advance the hour. I wrote some demo code that you can try out with buttons on a breadboard, it should not be difficult to merge with your current code. You need to decide what to do about error checking in case the OFF time is less than the ON time.

// arduino pins for buttons, HIGH => button pressed
const byte modePin = 2;
const byte setTimePin = 3;

byte lastModePinVal = LOW;

// mode the program is in - setting time or running
const byte runMode = 0;
const byte setOnMode = 1;
const byte setOffMode = 2;
byte currnetMode = runMode;

const int setDelay = 500;  // delay when holding down set button

int ontime=16;
int offtime=17;

void setup()
{
  Serial.begin(9600);

}

void loop()
{
  byte modePinVal = digitalRead(modePin);

  // if mode pin just went down
  if( (modePinVal == HIGH) && (lastModePinVal == LOW) )
  {

    nextMode();    // set current mode

      delay(100);  // debounce

  } // if

  lastModePinVal == modePinVal;    // remember mode pin state

  // if set button is down
  if(  digitalRead(setTimePin) == HIGH )
  {
    // if setting on time
    if( currnetMode == setOnMode )
    {

      // increment on time
      ontime++;

      // don't go past 24 hours
      if( ontime >= 24 ) ontime = 0;

      delay(setDelay);

    } 
    else if( currnetMode == setOffMode ) {

      // increment off time
      offtime++;

      // don't go past 24 hours
      if( offtime >= 24 ) offtime = 0;

      delay(setDelay);

    } // else



  } // if


  Serial.print("On  Time:  ");
  Serial.println(ontime);
  Serial.print("Off Time:  ");
  Serial.println(offtime);

}

// advnance program mode
void nextMode()
{

  // if we are in run mode
  if( currnetMode == runMode )
  {
    currnetMode = setOnMode;  // set on time

  } 
  else if( currnetMode == setOnMode ) {

    currnetMode = setOffMode;  // set off time

  } 
  else {

    currnetMode = runMode;    // back to run mode

  } // else

}
pinMode(5, OUTPUT);//Any pin. I have used Pin 4

You used pin 4 but set the mode of pin 5. Why? If you are going to have useless comments, you must make sure that they are accurate useless comments.

PaulS:

pinMode(5, OUTPUT);//Any pin. I have used Pin 4

You used pin 4 but set the mode of pin 5. Why? If you are going to have useless comments, you must make sure that they are accurate useless comments.

sorry bro recently i changed it ,u guys are like my collage professor finding mistake in every possible way lol,
thanks by the way i will change it at once

u guys are like my collage professor finding mistake in every possible way lol,

You have two choices. 1) Don't make stupid mistakes. 2) Make stupid mistakes harder to spot.

This text is green. Now, anyone can see that that is not true.

This text is colored. But, is it the right color? It's not so obvious that a (stupid) mistake has been made.

,u guys are like my collage professor

You need a professor to teach you how to stick stuff down?
(sorry, couldn’t resist :smiley: )

PaulS:
... 2) Make stupid mistakes harder to spot.

a.k.a. a career in middle management.

i have added button to change the off time but if i use same code to change the ontime its giving me erros

#include <TimeLib.h>
int ontime = 13;//relay on time in 24hrs
int offtime = 17;//relay off time in 24hrs
#include <Wire.h>
int relayPin = 5;//where relay is connected
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 RTC;
int ontimePin = 2;//push botton to change relay ontime
int offtimePin = 3;//push botton to change relay offtime
boolean lastButton = LOW;
boolean currentButton = LOW;


void setup () {
  pinMode(ontimePin, INPUT);
  pinMode(offtimePin, INPUT);

  Wire.begin();
  RTC.begin();
  lcd.init(); // initialize the lcd
  lcd.backlight();
  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  //Any pin. I have used Pin 4
  pinMode(relayPin, OUTPUT);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(offtimePin);
  if (last != current)
  {
    delay(10);
    current = digitalRead(offtimePin);
  }
  return current;


}

void loop () {

  DateTime now = RTC.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 2);
  lcd.print("relay ontime:"); lcd.print((ontime)); lcd.print("hrs");
  lcd.setCursor(0, 3 );
  lcd.print("relay offtime:"); lcd.print((offtime)); lcd.print("hrs");
  delay(1000);

  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    offtime = offtime + 1;
  }
  lastButton = currentButton;

  if (offtime > 24) offtime = 0;
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ontime = ontime + 1;
  }
  lastButton = currentButton;

  if (ontime > 24) ontime = 0;


  if (now.hour()  == 6 && now.minute() == 10 & now.second() == 0)
  {

    digitalWrite(4, 1);

  }



  if (now.hour() >= ontime && now.minute() >= 25 && now.hour() < offtime)
  {
    digitalWrite(relayPin, HIGH);
  }
  else
  {
    digitalWrite(relayPin, LOW);
  }
}

but if i use same code to change the ontime its giving me erros

Since you didn't share them, I'll assume you know how to fix them. So, what was the purpose of the last post?

this the vode which gives me erros

int relayPin = 5;
#include <Wire.h>
int ontime = 13;
int offtime = 17;
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 RTC;
int ontimePin = 2;
int offtimePin = 3;
boolean lastButton = LOW;
boolean currentButton = LOW;

void setup () {
  pinMode(ontimePin, INPUT);
  pinMode(offtimePin, INPUT);
 
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(offtimePin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(offtimePin);
  }
  return current;
boolean debounce(boolean last)
{
  boolean current = digitalRead(ontimePin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(ontimePin);
  }
  return current;



  Wire.begin();
  RTC.begin();
  lcd.init(); // initialize the lcd
  lcd.backlight();


  pinMode(relayPin, OUTPUT);//Any pin. I have used Pin 5
}

void loop () {
   currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    offtime = offtime + 1;
  }
  lastButton = currentButton;
  
  if (offtime > 24) offtime = 0;
 
 currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    onime = ontime + 1;
  }
  lastButton = currentButton;
  
  if (ontime > 24) ontime = 0;
  
  DateTime now = RTC.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 2);
  lcd.print("relay ontime:"); lcd.print((ontime)); lcd.print("hrs");
  lcd.setCursor(0, 3 );
  lcd.print("relay offtime:"); lcd.print((offtime)); lcd.print("hrs");
  delay(1000);
  

  if (now.hour() >= ontime && now.minute() >= 25 && now.hour() < offtime)
  {
    digitalWrite(5, HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }
}

this the vode which gives me erros

Which, again, you didn't post. So, why are you here? I have to assume that you don't need help fixing the errors, so what is the point of telling us that the code has errors?

But, just for giggles, perhaps you could explain why you have a debounce() function defined inside a debounce() function.

Perhaps you could explain why you have any code, in the second debounce() function, after the return statement.

Perhaps you could explain the random placement of curly braces. Sometimes they are on a new line, where IMHO they belong, and sometimes they are on the same line as the function/statement.

please sir look i am a noob in writing programs, i am doing this as an hobby and i dont have any basic in c or c++, iam writing all the code by what i understand or what i think i understand, see i am even noob in asking questions, if there is any problem in my please show me what i dint wrong and what is the right way to do it , and if u think me i am a dumb for even a basic arduino programming please tell that also i will stop project using arduino and buy a chines timer switch,

PaulS:
Which, again, you didn't post. So, why are you here? I have to assume that you don't need help fixing the errors, so what is the point of telling us that the code has errors?

But, just for giggles, perhaps you could explain why you have a debounce() function defined inside a debounce() function.

Perhaps you could explain why you have any code, in the second debounce() function, after the return statement.

Perhaps you could explain the random placement of curly braces. Sometimes they are on a new line, where IMHO they belong, and sometimes they are on the same line as the function/statement.

It's OK being a beginner, but before you wrote what you posted above, did you work through any of the examples built-in to the IDE?
Did they throw up any errors like the ones you're seeing, but are reluctant to post?

now i got no errors but ontime is not changing but offtime is changing
please tell me what i did wrong

#include <TimeLib.h>
int ontime = 13;//relay on time in 24hrs
int offtime = 17;//relay off time in 24hrs
#include <Wire.h>
int relayPin = 5;//where relay is connected
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 RTC;
int ontimePin = 2;//push botton to change relay ontime
int offtimePin = 3;//push botton to change relay offtime
boolean lastButton = LOW;
boolean currentButton = LOW;


void setup () {
  pinMode(ontimePin, INPUT);
  pinMode(offtimePin, INPUT);

  Wire.begin();
  RTC.begin();
  lcd.init(); // initialize the lcd
  lcd.backlight();
  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  
  pinMode(relayPin, OUTPUT);
}
boolean debounce(boolean last)
{
  boolean current = digitalRead(offtimePin);
  if (last != current)
  {
    delay(10);
    current = digitalRead(offtimePin);
  }
  return current;


}

void loop () {

  DateTime now = RTC.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 2);
  lcd.print("relay ontime:"); lcd.print((ontime)); lcd.print("hrs");
  lcd.setCursor(0, 3 );
  lcd.print("relay offtime:"); lcd.print((offtime)); lcd.print("hrs");
  delay(1000);

  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    offtime = offtime + 1;
  }
  lastButton = currentButton;

  if (offtime > 24) offtime = 0;
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
  {
    ontime = ontime + 1;
  }
  lastButton = currentButton;

  if (ontime > 24) ontime = 0;


  if (now.hour()  == 6 && now.minute() == 10 & now.second() == 0)
  {

    digitalWrite(4, 1);

  }



  if (now.hour() >= ontime && now.minute() >= 25 && now.hour() < offtime)
  {
    digitalWrite(relayPin, HIGH);
  }
  else
  {
    digitalWrite(relayPin, LOW);
  }
}

Well, for one thing you never actually read ontimePin.

Thank you all i have made both buttons to work and change the on time but if the arduino restarts after a power cut it going back to the default time which is mention in code, how do i change that

#include <TimeLib.h>
int ontime = 13;//relay on time in 24hrs
int offtime = 17;//relay off time in 24hrs
#include <Wire.h>
int relayPin = 5;//where relay is connected
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
RTC_DS1307 RTC;
int ontimePin = 2;//push botton to change relay ontime
int offtimePin = 3;//push botton to change relay offtime
boolean lastButton = LOW;
boolean currentButton = LOW;


void setup () {


  pinMode(ontimePin, INPUT);
  pinMode(offtimePin, INPUT);

  Wire.begin();
  RTC.begin();
  lcd.init(); // initialize the lcd
  lcd.backlight();

  pinMode(relayPin, OUTPUT);

}




void loop () {

  DateTime now = RTC.now();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print(now.year(), DEC);
  lcd.print('/');
  lcd.print(now.month(), DEC);
  lcd.print('/');
  lcd.print(now.day(), DEC);
  lcd.setCursor(0, 1);
  lcd.print(now.hour(), DEC);
  lcd.print(':');
  lcd.print(now.minute(), DEC);
  lcd.print(':');
  lcd.print(now.second(), DEC);
  lcd.setCursor(0, 2);
  lcd.print("relay ontime:"); lcd.print((ontime)); lcd.print("hrs");
  lcd.setCursor(0, 3 );
  lcd.print("relay offtime:"); lcd.print((offtime)); lcd.print("hrs");
  delay(1000);
  // read the state of the pushbutton value:
  lastButton = digitalRead(ontimePin);
  // read the state of the pushbutton value:
  currentButton = digitalRead(offtimePin);

  // check whether a  pushbutton is pressed.
  // if it is, the buttonState is HIGH:

  if (lastButton == HIGH) {
    offtime = offtime + 1;
  }
  lastButton = currentButton;
  // turn LED on:   // Tweet that the washer is done
  if (offtime > 24) offtime = 0;
  
  if (currentButton == HIGH) 
  {
    ontime = ontime + 1;
  }
  lastButton = currentButton;
  if (ontime > 24) ontime = 0;

  if (now.hour() >= ontime && now.minute() >= 00 && now.hour() < offtime)
  {
    digitalWrite(relayPin, HIGH);
  }
  else
  {
    digitalWrite(relayPin, LOW);
  }
}