Arduino Forum

Using Arduino => Programming Questions => Topic started by: pranav2016 on Sep 12, 2016, 02:53 pm

Title: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 12, 2016, 02:53 pm
 :smiley-confuse:



 i have a code as follows;;;
Code: [Select]




        #include <Arduino.h>
#include <Wire.h>
#include <RTClib.h>

const int light = 13;
int y1;
int y2;
int y3;
int x1;
int x2;
int x3;
int hour1 =5 ;
int min1 = 19;
int sec1 = 0;
int a = 6;
int b = 7;
int c = 8;
int d = 9;
int e = 10;
int f = 11;
int g = 12;
int h = 13;
int i = 14;
int j = 15;
int k = 16;
int l = 17;
int m = 18;
int n = 19;
int o = 20;
int p = 21;
int q = 22;
int r = 23;
int s =  0;
int t = 1;
int u = 2;
int v = 3;
int w = 4;
int hour2 = 21 ;
int min2 = 32;
int hour3 = 17 ;
int min3 = 19;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
RTC_DS1307 rtc;


void setup()
{

  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  pinMode(light, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);

}



void loop()
{
  DateTime now = rtc.now();
  buttonState1 = digitalRead(2);
  if ( buttonState1 ==  LOW)
  { if (now.hour() == hour1 && now.minute() == min1 && now.second() == sec1)
    {
      digitalWrite(light, HIGH);
    }
      if (now.hour() == a)
    {
      digitalWrite(light, HIGH);

     
    }
     if (now.hour() == b)
    {
      digitalWrite(light, HIGH);
    }
     if (now.hour() == c)
    {
      digitalWrite(light, HIGH);
    }
     if (now.hour() == d)
    {
      digitalWrite(light, HIGH);
    }
    if (now.hour() == e)
    {
      digitalWrite(light, HIGH);
    }
    if (now.hour() == f)
    {
      digitalWrite(light, HIGH);
    }
     if (now.hour() == g)
    {
      digitalWrite(light, HIGH);
    }if (now.hour() == h)
    {
      digitalWrite(light, HIGH);
    }
     if (now.hour() == i)
    {
      digitalWrite(light, HIGH);
    }
     if (now.hour() == j)
    {
      digitalWrite(light, HIGH);
    }
    if (now.hour() == k)
    {
      digitalWrite(light, HIGH);
    }
   
    if (now.hour() == hour3 && now.minute() == min3)
    {
      digitalWrite(light, LOW);
    }
  }
 
   buttonState2 = digitalRead(3);
  if(buttonState2 ==  LOW)
  {int x1 = now.hour();
  int x2 = now.minute();
  int x3 = now.second();
  if(now.hour() == x1 && now.minute()== x2 && now.second() == x3)
        {
          digitalWrite(light, HIGH);
        }
        if (now.hour() == c)
    {
      digitalWrite(light, HIGH);
    }
    if (now.hour() == d)
    {
      digitalWrite(light, HIGH);
    }
    if (now.hour() == e)
    {
      digitalWrite(light, HIGH);
    }
   
   }
  else(buttonState2 ==  HIGH);
   
    if(now.hour() == hour3 && now.minute()== min2)
        {
          digitalWrite(light, LOW);
        }
  buttonState3 = digitalRead(4);
  if (buttonState3 ==  LOW)
  { int y1 = now.hour();
    int y2 = now.minute();
    int y3 = now.second();
    if (now.hour() == y1 && now.minute() == y2)
    {
      digitalWrite(light, LOW);
    }
if  (now.hour() == hour1)
    {
      digitalWrite(light, HIGH);
    }
   
if  (now.hour() == a)
    {
      digitalWrite(light, HIGH);
    }
    if  (now.hour() == b)
    {
      digitalWrite(light, HIGH);
    }
    if  (now.hour() == c)
    {
      digitalWrite(light, HIGH);
    }
    if  (now.hour() == d)
    {
      digitalWrite(light, HIGH);
    }
   
  }

  }



[code]




i want to store the time when i made the pin 3 high;;;
and the loop should continue...
same function for pin 4
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: PaulS on Sep 12, 2016, 03:06 pm
Why do you need to store the time that you get from the RTC? The time changes at least every second, so you need to read it at least that often.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 13, 2016, 05:51 am
i am pushing the button.. and making led on,,,,,i want to store  buttn press time so that next time i won't   have to press the button
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: PaulS on Sep 13, 2016, 11:35 am
Quote
i want to store  buttn press time so that next time i won't   have to press the button
I don't understand that. If you press the switch on September 13th, 2016 at 2:30AM, it will never be September 13th, 2016 at 2:30AM again, so knowing that you pressed the switch at September 13th, 2016 at 2:30AM is rather useless information.

However, there are functions (hour(), minute(), etc. that return VALUES that you can store in variables...
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 13, 2016, 12:27 pm
exactly... i dont want to store "September 13th 2016"    i just want to store hour i. e.  "2:30 AM''    for that  i am using hour(), minute(), and second(),...  controller reads the value and perform according to time but i want  controller to store hour(), minute(),  in some variable when the button is pressed. i can use them afterwards....
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: PaulS on Sep 13, 2016, 12:42 pm
Quote
but i want  controller to store hour(), minute(),  in some variable when the button is pressed.
So, what is the problem?

Code: [Select]
#include <WhateverIsNeededToMakeHourAndMinuteFunctionsAvailable.h>

int switchPressedHour = 0;
int switchPressedMinute = 0;

int switchPin = 42;

void setup()
{
   pinMode(switchPin, INPUT_PULLUP);
}

void loop()
{
   if(digitalRead(switchPin) == LOW)
   {
      switchPressedHour = hour();
      switchPressedMinute = minute();
   }
}
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 13, 2016, 12:54 pm
i am unable to store time button press time in x and y variables,,, i want to know should i use eeprom library to store or is it possible with this code only...and how...?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 13, 2016, 01:00 pm
#include <WhateverIsNeededToMakeHourAndMinuteFunctionsAvailable.h>

int switchPressedHour = 0;
int switchPressedMinute = 0;

int switchPin = 42;

void setup()
{
   pinMode(switchPin, INPUT_PULLUP);
}

void loop()
{
   if(digitalRead(switchPin) == LOW)
   {
      switchPressedHour = hour();
      switchPressedMinute = minute();
   }
}





in this one... if i am using hour(); and minute();
   its telling me that hour and minute are not initialised....
   but now.hour();   is also not working.....what to do
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: UKHeliBob on Sep 13, 2016, 01:35 pm
The cunningly named <Time.h> may be of interest, but I am still not clear what you are doing or why.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: PaulS on Sep 13, 2016, 02:09 pm
Quote
i am unable to store time button press time in x and y variables
Using what code? Why do the names x and y mean anything to you? To me, they look like variables to hold coordinates, not times.

Quote
i want to know should i use eeprom library to store
Do you need to save the times in the event that the Arduino looses power/is reset? If not, no.

Quote
is it possible with this code only...and how...?
With what code? For how long?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 14, 2016, 06:27 am
i have read button press time of pin 3 in x variable (x1 == now.hour(),,x2 == now.minute()..)and exactly at that instant i am making led on....and after  some time at hour3, i am making it low.....
same function for pin 3..i am reading button press time (y1 == now.hour(),,y2 == now.minute()..) and making led off at that instant and after some time at hour 1 led will be on automatically...
       plz let me know if u still dont understand the working of thee two pins....
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 14, 2016, 07:32 am
<time.h>  library is helpless. i tried that one earlier
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 14, 2016, 08:36 am
<time.h>  library is helpless. i tried that one earlier
I have no idea what library that is.
The "Time" library is very useful i.e. <Time.h>
The key is to track time using epoch based time values not "human" or "wall clock" type of time that you are using.


If using epoch based time values you can get something like the time in seconds (which is what Time uses as well as pretty much any mainstream OS to track time internall)
Then to tell how much time has elapsed you do simple subtraction.

The key is stop thinking of human/"wall clock"  type time and use epoch based time values.
And then convert from an epoch value to a human time only when necessary.

BTW, the Time library also has a companion TimeAlarms library that can help doing the type of interval or repeated alarms like you have mentioned.

--- bill
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: ChrisTenone on Sep 14, 2016, 08:39 am
Actually the library, Time.h, can be quite helpful. But you don't need it if all you want is to get hours and minutes (and seconds, etc.) from an RTC that has been set already. Setting the RTC is another topic.

Last summer, I wrote a demonstration sketch that reads the hours, minutes and seconds from an RTC using I2C (and the Wire.h library) and sends it to the serial monitor. The demo assumes that you are using a UNO and an RTC breakout that has 4 connectors, SDA, SCL, GND and VIN. Perhaps you could modify this code to do what you want (which I am not quite clear on.) Just upload this sketch and open the serial monitor window:

Code: [Select]

/*
   Simplest clock with RTC, output to serial
   061316 Chris101 4106 bytes flash and 412 bytes RAM on a UNO

   Connect A4 on the UNO to SDA on the RTC
       "   A5 "   "   "  "  SCL "   "   "
       "  GND "   "   "  "  GND "   "   "
       "   5V "   "   "  "  VIN "   "   "

   Upload and open the serial monitor window.
   Assuming the time has been set on the RTC,
   the hours:minutes:seconds will scroll down
   the serial monitor window, one line a second.

 */

#include "Wire.h"
#define rtcAddress 0x68

unsigned long previousSecondMillis;
const long oneSecond = 1000UL;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  previousSecondMillis = millis();

}

void loop() {

  if (millis() - previousSecondMillis >= oneSecond) {
    previousSecondMillis += oneSecond; // do this once a second

    // get bcd data from the rtc
    Wire.beginTransmission(rtcAddress); // 0x68 is DS3231 device address
    Wire.write((byte)0); // start at register 0
    Wire.endTransmission();
    Wire.requestFrom(rtcAddress, 3); // request three bytes (seconds, minutes, hours)

    while (Wire.available())
    {
      byte seconds = Wire.read(); // get seconds
      byte minutes = Wire.read(); // get minutes
      byte hours = Wire.read(); // get hours

      // convert BCD to decimal: (number & 0b11110000) is the 'tens place' digit
      //                    and (number & 0b00001111) is the 'ones place' digit
      // of a two digit, decimal number (as are the hours, minutes and seconds)
      seconds = ((seconds & 0b11110000) >> 4) * 10 + (seconds & 0b00001111);
      minutes = ((minutes & 0b11110000) >> 4) * 10 + (minutes & 0b00001111);
      hours =   ((hours   & 0b00110000) >> 4) * 10 + (hours   & 0b00001111);

      // serial.print the bcd values
      Serial.print(hours);
      Serial.print(':');
      Serial.print(minutes);
      Serial.print(':');
      Serial.println(seconds);
    }
  }
}
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: ChrisTenone on Sep 14, 2016, 08:58 am
@pranav2016, can you tell us, without reference to the code, what your project is supposed to do? Turn on a mechanical system, wake the baby, or check a life support system to see if it's still working?  I suspect that you do not really need all those variables and comparisons. What's happening is known as an XY problem. Literally in this case!
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 14, 2016, 09:07 am
What's happening is known as an XY problem. Literally in this case!
:)

But he did give a hint in post #10 that this involved elapsed time, hence my comments about favoring epoch based time values as using those make it trivial to calculate time durations.
Wheras using hours/mins for that can get messy depending on the duration and the time when the events occur.

Heck you could even use millis() if all you need is a semi accurate time duration.
The Time and TimeLibraries can work without an RTC as well....  ;)

-- bill
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 16, 2016, 08:29 am
my project is having three functions
1st function.... turn on the light automatically at 6 pm and turn off at 6 am daily.

2nd function.... turn on the the light when button is pressed and off automatically at 12 pm. and once button is pressed we should not have to press it again....light should be on at the same time when button is pressed earlier and off at 12 automatically.

3rd function...same function as 2nd but it is for making the light off;;;;
   
   i am using pushbutton to switch between these functions
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: ChrisTenone on Sep 16, 2016, 09:53 am
Hi Bill,

If our OP were making a general purpose timer, I would agree that using 'epoch time' would be simpler due to having all the same units, say seconds or milliseconds, and thus all calculations would be continuous. From the last post however, it looks like he's building a programmable on/off switch. If this was my project, I would probably use clock time though. When you push the button, set a variable with the hms. I'd compare that variable to the clock time once a second and set the light switch accordingly.

----

Hi Pranav,

Now it makes sense to me. I outlined my idea above. The example I posted earlier is how I interact with a modern RTC chip, such as the DS3231, or the older DS1307 using I2C, without a Time library. You can also count millis (or seconds) without a library. The function millis() is made for this - it's the milliseconds since you last started your Arduino. Theoretically* there are 1000 milliseconds in a second, 3,600,000 in an hour, and 86,400,000 per day.

I said theoretical, because the milliseconds that are counted by an Arduino are not exactly a millisecond long. They use a resonator that is within 5% of the real time, but it changes with temperature, and age.  This is the big advantage of an RTC chip. It is accurate to much less that a percent. And it provides both a precise one second pulse (useful for accurate epoch timing), as well as stream the clock time over I2C.

In addition to your Arduino circuit (with or without an RTC), with interface components like the button, a mode switch, and perhaps a time and status display, you will also need the power switching circuitry. Probably using relays.

Start by drawing your circuit in block-schematic form. This is so you (and others) can tell exactly what is hooked to what. And write the first iteration of your sketch. Come back, post your schematic and code (use code tags!!), and folks will jump right into it.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 16, 2016, 09:04 pm
This project sounds like it could be even solved with some of the X10 modules and its companion s/w.

For any time based s/w I still prefer to use unix epoch based time. With something like the Time library, you get the flexibility of using either form of time and can convert back and forth if/when necessary.
This allow tracking time efficiently as an epoch which also allows easy delta calculations and then convert to and/or use human time only when necessary - which might be in the user interface used to display and configure things.

The Time and TimeAlarms library seem like a good fit for this project based on the descriptions i've seen so far.
TimeAlarms can be used to create the daily periodic events/alarms and you can create/destroy timers as necessary to handle the time based events needed after button presses.

Seems pretty simple if Time & TimeAlarms is used.
Just set up a sync provider to allow the Time library to  periodically read the time from the RTC to keep the time accurate.
The main code doesn't have to do any polling or comparison of the time as
the TimeAlarms library will call the designated functions for any/all configured alarms or timers at the appropriate time.



If it were me, I'd use a DS3231 and combine these libraries:
- Time
- TimeAlarms
-  DS3232RTC https://github.com/JChristensen/DS3232RTC (https://github.com/JChristensen/DS3232RTC)
- Button https://github.com/JChristensen/Button (https://github.com/JChristensen/Button)

This should provide all the needed time and button reading capabilities.
I've used these libraries on several clock projects and it allows for a very quick development cycle as all the "hard" stuff is already done and debugged.

Why create all this (or some subset of it) from scratch when you could use existing libraries and get on with the project?

If existing libraries are not used, code to do all the time comparisons and track the back ground events for the button pushes has to be written.
While it isn't that difficult, if things change or more functionality is added, it can start to get messy depending on the initial architecture used.
i.e. you can get away with lots of stuff when there is only a single event, single time, single button etc, but if things change or expand it can get messy to update the s/w, depending on how it was written.



I recommend using a DS3231 over the older DS1307 for several reasons
- they are now the same price (less than $1USD shipped to your door with a battery)
- DS32321 is more accurate
- DS1307 only runs a 100kHz so you can't speed the bus up to 400Khz
(pretty much all current I2C devices support 400Khz - faster can be important in some environments as it frees up the processor to do other things)


Be prepared to do some slight h/w mods to most of the RTC boards you buy on Ebay and/or directly from other suppliers as most of these modules were designed for a re-chargable battery and the charge circuit is total CRAP. It should be removed. It is typically one trace cut to to it. Also, many of the modules come with a non chargeable battery, which means that after some period of time, the  battery will leak and fail unless the charge circuit is disabled.
I'd recommend buying a DS3231 that comes with a non chargeable battery, then modding the module to disable the charge circuit. (I've done this on all the RTC modules I've used)



But this is just my views.....
Others will likely have alternative ways of handling this.

--- bill

Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: ChrisTenone on Sep 16, 2016, 11:48 pm
Another advantage the 3231` has over the 1307 is the crystal is inside the chip, so there is one less (delicate) component to worry about.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 18, 2016, 02:38 pm
THEN WHAT WILL BE THE FUNCTION TO STORE THE rtc TIME THEN......
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: ChrisTenone on Sep 18, 2016, 07:12 pm
Quit SHOUTING!
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 18, 2016, 07:17 pm
THEN WHAT WILL BE THE FUNCTION TO STORE THE rtc TIME THEN......
Huh? I can't make any sense of this.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: Gabriel_swe on Sep 18, 2016, 08:29 pm
THEN WHAT WILL BE THE FUNCTION TO STORE THE rtc TIME THEN......
A cheap RTC module keep track of time. Do you want to store on / off switches?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 19, 2016, 05:38 am
yes exactly...i want to store on off switches....
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: UKHeliBob on Sep 19, 2016, 08:00 am
Do you need the actual time or do you need to know how long (hours, minutes, seconds) a switch was on or off ?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 19, 2016, 08:18 am
Your description in post #16 is really vague.

It mentions 3 functions:

1) Turn on light at 6pm and off at 6am

2) turn on light when button 1 pressed, and turn light off at noon (12 pm)
Then this:
Quote
and once button is pressed we should not have to press it again....light should be on at the same time when button is pressed earlier and off at 12 automatically.
What does that mean?

3) No idea what this means as it doesn't really say:
Quote
3rd function...same function as 2nd but it is for making the light off;;;;
And this last bit:

Quote
i am using pushbutton to switch between these functions
What does that mean?

The description of what you said isn't clear enough for people to understand what you want.

--- bill
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: odometer on Sep 19, 2016, 02:23 pm
For any time based s/w I still prefer to use unix epoch based time. With something like the Time library, you get the flexibility of using either form of time and can convert back and forth if/when necessary.
You are trying to kill an ant with a howitzer.
And the TimeAlarms libraries are almost certainly more trouble than just writing the thing from scratch.

And I am sure that this is all Greek to the OP.


Why even use an Arduino? Why not just use one of these?
http://www.homedepot.com/p/Intermatic-15-Amp-Plug-In-Lamp-and-Appliance-Timer-White-TN111K/205478759 (http://www.homedepot.com/p/Intermatic-15-Amp-Plug-In-Lamp-and-Appliance-Timer-White-TN111K/205478759)
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 19, 2016, 06:03 pm
You are trying to kill an ant with a howitzer.
And the TimeAlarms libraries are almost certainly more trouble than just writing the thing from scratch.
I would disagree.
I like to get things done and actually finish a project to be able to put it to bed without having to monkey around with it. As soon as you do anything "real" that will involve daylight savings time or timezones things will start to get messy.
TimeAlarms is quick and easy to use and supports several different types of events/timers/alarms.
Yeah, I've written stuff like this from scratch before, but having done that, and having used these types of epoch based libraries MANY times over the decades and having the flash code space available in the AVR to be able to use more generalized libraries, I'd still recommend using Time & TimeAlarms libraries as it can provide a much quicker and easier path to project completion.
Using these types of libraries allows putting a clean abstraction layer between the main code and the h/w which keeps the main line code clean and allows it to focus on the overall task vs having to sprinkle h/w specific code all over the place which makes any potential maintenance more difficult.
I'm a big believer in layered s/w design.
I've used the libraries I've mentioned earlier on several personal clock projects.
But then I've been doing embedded s/w development and *nix development for 35+ years...

Quote
And I am sure that this is all Greek to the OP.
This I believe. And I'm guessing that this is the #1 reason that most people will shy away from using epoch based time tracking.
They are not grasping the concept and its usefulness.

Quote
Why even use an Arduino? Why not just use one of these?
http://www.homedepot.com/p/Intermatic-15-Amp-Plug-In-Lamp-and-Appliance-Timer-White-TN111K/205478759 (http://www.homedepot.com/p/Intermatic-15-Amp-Plug-In-Lamp-and-Appliance-Timer-White-TN111K/205478759)
I was thinking more along the lines of a X10 computer interface.
It is programmable and the built intelligence, can calculated sunrise/set for any georgraphic location, and supports macros for doing more than simple independent on/off things.

--- bill
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: odometer on Sep 20, 2016, 06:35 am
Quote from: odometer
And I am sure that this is all Greek to the OP.
This I believe. And I'm guessing that this is the #1 reason that most people will shy away from using epoch based time tracking.
They are not grasping the concept and its usefulness.
Chances are, the OP's project doesn't care what day it is, nor how many days have passed: it only cares about the time of day. And for that, anything epoch-based is overkill.

And as for "daylight savings time and time zones": does either the Time or the TimeAlarms library handle those? If so, that's news to me.

If nothing else, Daylight Saving Time could be implemented by a simple "spring forward, fall back" toggle switch. Otherwise, woe to the OP if his (her?) country changes the Daylight Saving rules.


Is the OP even aware that simple, easy-to-set electromechanical lighting timers exist? Electronic versions of the same also exist, but might be a bit more difficult to set.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 20, 2016, 07:32 am
Chances are, the OP's project doesn't care what day it is, nor how many days have passed: it only cares about the time of day. And for that, anything epoch-based is overkill.

I still haven't figured out what the project is trying to do.

Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: ChrisTenone on Sep 20, 2016, 09:06 am
I still haven't figured out what the project is trying to do.


The OP asked how to make a device that does two things:

1: turn a light on at 6pm and off at 6am.

2: turn a light on at a user selectable time (set by pressing a button at that time) and off at a pre-programmed time, 12pm (which is noon, but I suspect they meant 12am, midnight.)

Seems pretty straightforward to me. After explaining how I would do it, the OP YELLED at me when I suggested that he do some work on it, then re-asked the question in a less specific manner.

I now think the OP has asked on various fora, hoping someone will have done this specific project before, and can just provide working code.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 20, 2016, 04:52 pm
The OP asked how to make a device that does two things:
* * *
Seems pretty straightforward to me.
I don't think it is clear at all and I think he is wanting quite a bit more than you have just described.
In looking back at post #16 which where he provided some details,  he said:
Quote
my project is having three functions
1st function.... turn on the light automatically at 6 pm and turn off at 6 am daily.

2nd function.... turn on the the light when button is pressed and off automatically at 12 pm. and once button is pressed we should not have to press it again....light should be on at the same time when button is pressed earlier and off at 12 automatically.

3rd function...same function as 2nd but it is for making the light off;;;;
   
   i am using pushbutton to switch between these functions
But that description is much more than just 3 "functions".
AND that description does not match the code example provided in the first post and nor some of the other things he said in the thread.

And while the first requirement is obvious and matches your first,
the 2nd seems to be asking for more than just turning on the light when the button is pressed and off at noon (12pm).
Like he is wanting to alter the time at which something is happening, for all days moving forward,
particularly since he mentioned in other posts about storing the time when the button was pressed and possibly storing that in eeprom.

Then there was a 3rd - which was very vague with no explanation
And then even a 4th requirement which said:
Quote
i am using pushbutton to switch between these functions
What does that mean? Some master button that alters what the other buttons do?
Or is there just a single button? But then the code in the first post seems to be initializing and reading 3 buttons.

From the description in post #16 I can't tell what the requirements are or even how many buttons there are, and what each button is supposed to do.

And then could you really build a useful device that *only* does that?
i.e. how do you ever set the time?

So IMO there is simply not enough information to know what he is wanting to do.

I believe you summed it up best in post #14:
Quote
What's happening is known as an XY problem. Literally in this case!
As I think we are still in XY problem land.....


--- bill
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 21, 2016, 10:33 am
sorry for not explaining in detail.....
    here i again made some modifications to my code.....and its important to note that i am using three pushbuttons for seprate pin(function)... i also made some modifications to my function than i wanted earlier
     
reset button ----pin 2--- start the light on 6pm and off at 6am(this pin is working great)

on button ----pin 3---start the light when i preses the button and off at 12. on the next day. i should not have to press button again. it should be on automatically at  the time when i pressed the switch earlier and again should be off at 12 again...this cycle should go on each day automatically until i press the other switchand jump to another function

off button----pin 4---off the light when i press the button and should be on at the time when i pressed earlier pushbutton(pin 3 which is for on). again without pressing any button  this cycle should go on contineously every day until i press other one.....


    and yes exactly....
   i want to store that button press time in the eprom... but i dont know how.....?
   i am unable to add third function to my program thts why i switch on the light at 6pm again(u can see this in prgram)
how do i make it on at the button press time of pin 3




code[]



   #include <Arduino.h>
#include <Wire.h>
#include <RTClib.h>

const int light = 13;

int y1;
int y2;

int z1;
int z2;



const int buttonPin[] = {2,3,4};
int buttonState = 0;
RTC_DS1307 rtc;


void setup()
{

  Serial.begin(9600);
  Wire.begin();
  rtc.begin();

  pinMode(light, OUTPUT);
 for (int x=0; x<3; x++)
  {
    pinMode(buttonPin
 
}

}

void loop(){
DateTime now = rtc.now();

for(int x=0; x<3; x++)
{
  buttonState = digitalRead(buttonPin

  { if (buttonState == 0 && buttonPin
  { if (now.hour() == 4 && now.minute() == 22)
    {
      digitalWrite(light, HIGH);
    }
   
  }
  else (buttonState == 1 && buttonPin
  { if (now.hour() == 10 && now.minute() == 22)
    {
      digitalWrite(light, LOW);
    }
    if (now.hour() == 4 && now.minute() == 22 )
    {
      digitalWrite(light, HIGH);
    }
     }}
{ if (buttonState == 0 && buttonPin
  {int z1 = now.hour();
  int z2 = now.minute();
 
  if(now.hour() == z1 && now.minute()== z2)
        {
          digitalWrite(light, HIGH);
        }
     }
  else(buttonState == 1 && buttonPin
 { if (now.hour() == 10   && now.minute()== 22 )
        {
          digitalWrite(light, LOW);
        }
   if(now.hour() == z1 && now.minute()== z2)
        {
          digitalWrite(light, HIGH);
        }     
 } }     
{if (buttonState == 0 && buttonPin
  { int y1 = now.hour();
    int y2 = now.minute();
   
    if (now.hour() == y1 && now.minute() == y2)
    {
      digitalWrite(light, LOW);
    }}
else (buttonState == 1 && buttonPin
  { if (now.hour() == 4 && now.minute()== 22)
    {
      digitalWrite(light, HIGH);
    }
   if (now.hour() == y1 && now.minute() == y2)
    {
      digitalWrite(light, LOW);
    }
  } 
}
}
}


code[]
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: PaulS on Sep 21, 2016, 10:40 am
Is it really so difficult to select the </> icon BEFORE posting code, and then pasting the code between the tags that it generates?

I KNOW that your code does not look like the crap you posted!
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 21, 2016, 10:47 am

   pauls
  but pin 2 and 3 are working great

   ChrisTenone
   i cant find the time library, can u provide the link..
   i downloaded some but they are not working


       
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: UKHeliBob on Sep 21, 2016, 03:02 pm
Quote
but pin 2 and 3 are working great
Good, but does the code look the same in the IDE as it does in reply #33 ?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: bperrybap on Sep 21, 2016, 06:20 pm
The description is still too vague and incomplete.

For example:
Quote
on button ----pin 3---start the light when i preses the button and off at 12. on the next day. i should not have to press button again. it should be on automatically at  the time when i pressed the switch earlier and again should be off at 12 again...this cycle should go on each day automatically until i press the other switchand jump to another function
"off at 12"
Does that mean noon or midnight?

"on the next day"
What about right now? Does it do anything now or does it wait until the next day to do something?

"it should be on automatically at  the time when i pressed the switch earlier and again should be off at 12 again"
"pressed the switch earlier"
Earlier than what? The previous part of the sentence said you were just pressing the button.

"this cycle should go on each day automatically until i press the other switchand jump to another function"
"jump to another function"
What does that mean? It doesn't make sense to me.
What other switch?
Each button appears to control some part of the configuration so "jumping to another function" doesn't make sense to me.

Programming requires very close attention to details.
Even if actual programming is not understood that is ok; however, the functional description must be described in great detail in order to be able to write the code to implement the desired functionality.

So far after 9 days of posts and many questions, there is still no accurate and full details on the desired functionality.

At this point I'm going to back out and let others continue the conversation.

--- bill

Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: Gabriel_swe on Sep 22, 2016, 06:07 pm
I'll try to decode. 24h for easy time management.

I think this is an alternative for a simple timer you plug in the wall socket.

Default and button 2, lets call it mode 1. On at 18.00 and off at 06.00.

Button 3, manual selected on-time. Mode 2a. Save the time of button press and turn on light. Let it be on until 12.00 unless....

Button 4, manual selected off time. Mode 2b. Save the time of button press and turn off light


There is no need save time in eeprom unless arduino need to remember on/off after a power failure.


I don't even see the need for different modes. At start read eeprom. If it is valid time use it, else set onTime to 18.00 and offTime to 06:00.

If button 3 is pressed set onTime to rtc.now, store in eeprom. Turn on light. Change offTime to 12.00 (
Why does light need to be on until 12.00? )

Next morning light turn off at 12.00. (or 06.00). When it is a good time to turn off lamp press button 4. Set offTime to rtc.now and save in eeprom and turn off lamp.

Press button 2 an both on and offTimes are changed to 18:00 respectively 06:00 (and store in eeprom?)


When button 3 is pressed, I would turn lamp off for 1s before turning on so you know your press is detected. If it is off it will turn on with a little delay. If it is already on, it will blink.
The opposite with button 4. Turn it on for 1s before turning it off. If it is off it will blink to confirm your press.

My guess is op wanted to have it on until 12.00 with button 3 because he didn't thought button 4 could e used to turn off a lamp that was already off.

If you don't touch the offTime when onTime is set you can set whichever you want first, and can change any of them independently.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 22, 2016, 07:20 pm
but how can i store it
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: Gabriel_swe on Sep 22, 2016, 11:08 pm
Decide which four addresses to use for ontime hour, ontime minutes, offtime hour and offtime minutes. I assume seconds are not needed.

EEPROM.write(address, value)

Reference (https://www.arduino.cc/en/Reference/EEPROMWrite)
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 23, 2016, 06:22 am
it means eeprom library is required,, and what should be in the address ?, i am using atmega 328p for my project. Any examples for adresses to write in there.....?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: UKHeliBob on Sep 23, 2016, 08:10 am
it means eeprom library is required,, and what should be in the address ?, i am using atmega 328p for my project. Any examples for adresses to write in there.....?
The EEPROM library is installed as part of the IDE so you already have it.  You can store a byte value at each address in the EEPROM and read it back later, so choose an address (an EEPROM position) to store each piece of data in so you know where to save it and where to find it later then use them later.

Have you looked at the EEPROM examples in the IDE ?
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: Gabriel_swe on Sep 23, 2016, 09:58 am
it means eeprom library is required,, and what should be in the address ?, i am using atmega 328p for my project. Any examples for adresses to write in there.....?
328p has 1kB of EEPROM, and you freely chose any addresses you want from 0 to 1023.

After you started arduino IDE, click Sketch > Include Library > EEPROM.
It will add
Code: [Select]
#include <EEPROM.h> at top of your sketch and you are ready to go.


EEPROM has a limited number of writes, but don't worry. 100 000 writes is guaranteed. Tronixstuff (http://tronixstuff.com/2011/05/11/discovering-arduinos-internal-eeprom-lifespan/) tried to kill EEPROM, and it took more than 1.2 million writes before it failed for them.
Title: Re: how to store RTC time in an arduino uno board, which library should i use...?
Post by: pranav2016 on Sep 23, 2016, 10:18 am
thanks for the answer..its great...