Go Down

Topic: how to store RTC time in an arduino uno board, which library should i use...? (Read 4474 times) previous topic - next topic

pranav2016

 :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

PaulS

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.
The art of getting good answers lies in asking good questions.

pranav2016

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

PaulS

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...
The art of getting good answers lies in asking good questions.

pranav2016

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....

PaulS

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();
   }
}
The art of getting good answers lies in asking good questions.

pranav2016

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...?

pranav2016

#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

UKHeliBob

The cunningly named <Time.h> may be of interest, but I am still not clear what you are doing or why.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

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?
The art of getting good answers lies in asking good questions.

pranav2016

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....

pranav2016


bperrybap

<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

ChrisTenone

#13
Sep 14, 2016, 08:39 am Last Edit: Sep 14, 2016, 09:04 am by ChrisTenone Reason: byte not int for time variables
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);
    }
  }
}
What, I need to say something else too?

ChrisTenone

#14
Sep 14, 2016, 08:58 am Last Edit: Sep 14, 2016, 09:07 am by ChrisTenone Reason: ... dont be so snarky!
@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!
What, I need to say something else too?

Go Up