Go Down

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

bperrybap

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.


ChrisTenone

#31
Sep 20, 2016, 09:06 am Last Edit: Sep 20, 2016, 09:07 am by ChrisTenone Reason: be more snarky
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.
What, I need to say something else too?

bperrybap

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

pranav2016

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
  • , INPUT);

 
}

}

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

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


  { if (buttonState == 0 && buttonPin
  • == 2)

  { if (now.hour() == 4 && now.minute() == 22)
    {
      digitalWrite(light, HIGH);
    }
   
  }
  else (buttonState == 1 && buttonPin
  • == 2);

  { if (now.hour() == 10 && now.minute() == 22)
    {
      digitalWrite(light, LOW);
    }
    if (now.hour() == 4 && now.minute() == 22 )
    {
      digitalWrite(light, HIGH);
    }
     }}
{ if (buttonState == 0 && buttonPin
  • == 3)

  {int z1 = now.hour();
  int z2 = now.minute();
 
  if(now.hour() == z1 && now.minute()== z2)
        {
          digitalWrite(light, HIGH);
        }
     }
  else(buttonState == 1 && buttonPin
  • == 3);

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

  { int y1 = now.hour();
    int y2 = now.minute();
   
    if (now.hour() == y1 && now.minute() == y2)
    {
      digitalWrite(light, LOW);
    }}
else (buttonState == 1 && buttonPin
  • == 4);

  { if (now.hour() == 4 && now.minute()== 22)
    {
      digitalWrite(light, HIGH);
    }
   if (now.hour() == y1 && now.minute() == y2)
    {
      digitalWrite(light, LOW);
    }
  } 
}
}
}


code[]

PaulS

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

pranav2016


   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


       

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

bperrybap

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


Gabriel_swe

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.


Gabriel_swe

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

pranav2016

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

UKHeliBob

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 ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Gabriel_swe

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 tried to kill EEPROM, and it took more than 1.2 million writes before it failed for them.


Go Up