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
}
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
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?
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);
}
}