Pages: [1]   Go Down
Author Topic: physical button time change on RTC  (Read 1069 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,
first time using an RTC attachment, im using the coding from: http://www.ladyada.net/learn/breakoutplus/ds1307rtc.html

Code:
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
 
#include <Wire.h>
#include "RTClib.h"
 
RTC_DS1307 RTC;
 
void setup () {
    Serial.begin(57600);
    Wire.begin();
    RTC.begin();
 
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
 
}
 
void loop () {
    DateTime now = RTC.now();
 
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
 
    Serial.print(" since 1970 = ");
    Serial.print(now.unixtime());
    Serial.print("s = ");
    Serial.print(now.unixtime() / 86400L);
    Serial.println("d");
 
    // calculate a date which is 7 days and 30 seconds into the future
    DateTime future (now.unixtime() + 7 * 86400L + 30);
 
    Serial.print(" now + 7d + 30s: ");
    Serial.print(future.year(), DEC);
    Serial.print('/');
    Serial.print(future.month(), DEC);
    Serial.print('/');
    Serial.print(future.day(), DEC);
    Serial.print(' ');
    Serial.print(future.hour(), DEC);
    Serial.print(':');
    Serial.print(future.minute(), DEC);
    Serial.print(':');
    Serial.print(future.second(), DEC);
    Serial.println();
 
    Serial.println();
    delay(3000);



I am yet to migrate it so that it also prints to an LCD 20x4 using custom numbering that i have already coded, however, that should be relatively easy to do: lcd.print($value$);    correct?.

and what i was curious about was how would you change the coding so that it can use physical buttons to change the time, using just H+, M+, H-, M-, neglecting seconds and the date (for DST mainly)

would it simply be a matter of grounding a switch with resistor to I/O pin (x4) and linking the pin to the corrosponding action? if so, would anyone be able to help me out with how exactly to go about it please? if not what would the the right method?

thanks, Jess
Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you don't have it, download:
http://playground.arduino.cc/Code/time
Unzip the file, add to your documents Arduino library folder
Find:
Time\Time\Examples\TimeRTCSet\TimeRTCSet.pde
This sketch exposes different things you can do.
Use    http://www.epochconverter.com/    to calculate the current time to set the RTC
in the Serial monitor, enter    T1358581537      for Human time (GMT): Sat, 19 Jan 2013 07:45:37 UTC
Examples:
In your sketches you can  setTime(10,59,55,27,12,12);     //Hour,Minute,Second,Day,Month,Year
                                   OR  setTime(1354532395);               //Use EPOCH Time to set the clock
Switches can be tied to the H, M fields when setting the RTC  
Yes you can         lcd.print(hourFormat12());

In your sketch include:
#include <Time.h>               //
#include <DS1307RTC.h>     //RTC DS1307 library that returns time as a time_t
#include <Wire.h>               //Needed for IIC (I2C) comunications
#include <LiquidCrystal.h> //LCD library
  
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PART 1/2
Thanks, that was what i was trying to find, but somehow couldn't....

now, i have NOT tested this code, as i am still waiting for my RTC to arrive in le post, but this is my compiled code:

Code:
#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // returns time as a time_t
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//manual time adjustment away from serial update
int modePin = 13;
int plusMin = 1;
int plusHr = 6;
int minusMin = 7;
int minusHr = 8;

//sets switches value to off as default
int mode = 0;
int minPlus = 0;
int minMinus = 0;
int hrPlus = 0;
int hrMinus = 0;

// connects to opto-isolator
int opticalONE = 9;
int opticalTWO = 10;

//temporary variables for time changing
int varMin = 0;
int varHr = 0;
int varSec = 0;
int vardd = 0;
int varmm = 0;
int varyy = 0;
int hours = 0;
int minutes = 0;

//Custom numbering
byte BLOCK[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};
byte HALFBLOCK[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
};
byte BLANK[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};
byte CT[8] = {
  B00000,
  B01110,
  B01110,
  B01110,
  B00000,
  B00000,
  B00000,
  B00000,
};
byte CB[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B01110,
  B01110,
  B01110,
  B00000,
};


//A4 connects to RTC: SCL
//A5 connects to RTC: SDA

void setup() {
  pinMode(modePin, INPUT);
  pinMode(plusMin, INPUT);
  pinMode(plusHr, INPUT);
  pinMode(minusMin, INPUT);
  pinMode(minusHr, INPUT);
  lcd.begin(20, 4);
 
  // assignes each segment a write number
  lcd.createChar(1,HALFBLOCK);
  lcd.createChar(2,BLANK);
  lcd.createChar(3,CT);
  lcd.createChar(4,CB);
  lcd.createChar(5,BLOCK);
 
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
}

void custom10(){   //Hr : min seperator
  lcd.setCursor(0,0);    //top row
  lcd.write(2);
  lcd.setCursor(0,1);
  lcd.write(3);
  lcd.setCursor(0,2);
  lcd.write(4);
  lcd.setCursor(0,3);
  lcd.write(2);
}
void custom0(){  // 0
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(2);
  lcd.write(5);
  lcd.setCursor(0,2);
  lcd.write(5);
  lcd.write(2);
  lcd.write(5);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom1(){  // 1
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,1);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,2);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom2(){  // 2
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(2);
  lcd.setCursor(0,1);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,2);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom3(){  // 3
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,2);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom4(){  // 4
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,2);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom5(){  // 5
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(2);
  lcd.setCursor(0,2);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom6(){  // 6
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,2);
  lcd.write(2);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom7(){  // 7
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,2);
  lcd.write(5);
  lcd.write(2);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom8(){  // 8
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,2);
  lcd.write(5);
  lcd.write(5);
  lcd.write(2);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}
void custom9(){  // 9
  lcd.setCursor(0,0);    //top row
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,1);
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,2);
  lcd.write(5);
  lcd.write(5);
  lcd.write(5);
  lcd.setCursor(0,3);
  lcd.write(1);
  lcd.write(5);
  lcd.write(1);
}

void loop()
{
 
  SetTimeManual();
  serialClockDisplay();
  PhysicalDisplay();
  delay(200);
}

void SetTimeManual() {
  mode = digitalRead(modePin); //gets state of mode pin that locks to clock or set time
  minPlus = digitalRead(plusMin);
  minMinus = digitalRead(minusMin);
  hrPlus = digitalRead(plusHr);
  hrMinus = digitalRead(minusHr);
 
  if (mode == HIGH)
  {
   
    if(Serial.available())
    {
      setSyncProvider(RTC.get);
      time_t t = processSyncMessage();
      if(t >0)
      {
        RTC.set(t);   // set the RTC and the system time to the received value
        setTime(t);         
      }
    }
   
    if(minPlus == HIGH) {
      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varSec = second(t);
      vardd = day(t);
      varmm = month(t);
      varyy = year(t);
      varMin++;   //add 1 minute from the current tine
      setTime(varHr, varMin, varSec, vardd, varmm, varyy);
    }

    if(minMinus == HIGH) {
      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varSec = second(t);
      vardd = day(t);
      varmm = month(t);
      varyy = year(t);
      varMin--;   //subtract 1 minute from the current tine
      setTime(varHr, varMin, varSec, vardd, varmm, varyy);
    }

    if(hrPlus == HIGH) {
      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varSec = second(t);
      vardd = day(t);
      varmm = month(t);
      varyy = year(t);
      varHr++;   //add 1 hour from the current tine
      setTime(varHr, varMin, varSec, vardd, varmm, varyy);
    }

    if(hrMinus == HIGH) {
      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varSec = second(t);
      vardd = day(t);
      varmm = month(t);
      varyy = year(t);
      hrMinus--;    //subtract 1 hour from the current tine
      setTime(varHr, varMin, varSec, vardd, varmm, varyy);
    }
   
    delay(200); // delay to allow time to take finger off setting buttons
   
  }
}

void serialClockDisplay() {
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

PART 2/2

Code:
void PhysicalDisplay() {
 
  time_t t = now();
  hours = hour(t);
  minutes = minute(t);
 
  lcd.setCursor(9,0);
  custom10();
 
  if(hours < 10)  {
    lcd.setCursor(1,0);
    custom0();
  }
  if(20 > hours > 10)  {
    lcd.setCursor(1,0);
    custom1();
  }
  if(hours >= 20)  {
    lcd.setCursor(1,0);
    custom2();
  }   
//----------------------------//   
    if(hours%10 == 1)  {
    lcd.setCursor(5,0);
    custom1();
  }
  if(hours%10 == 2)  {
    lcd.setCursor(5,0);
    custom2();
  }   
  if(hours%10 == 3)  {
    lcd.setCursor(5,0);
    custom3();
  }   
  if(hours%10 == 4)  {
    lcd.setCursor(5,0);
    custom4();
  }   
  if(hours%10 == 5)  {
    lcd.setCursor(5,0);
    custom5();
  }   
  if(hours%10 == 6)  {
    lcd.setCursor(5,0);
    custom6();
  }   
  if(hours%10 == 7)  {
    lcd.setCursor(5,0);
    custom7();
  }   
  if(hours%10 == 8)  {
    lcd.setCursor(5,0);
    custom8();
  }
  if(hours%10 == 9)  {
    lcd.setCursor(5,0);
    custom9();
  }
    if(hours%10 == 0)  {
    lcd.setCursor(5,0);
    custom0();
  }
//=============================//
  if(10 > minutes)  {
    lcd.setCursor(11,0);
    custom0();
  } 
  if(20 > minutes > 10)  {
    lcd.setCursor(11,0);
    custom1();
  } 
  if(30 > minutes > 20)  {
    lcd.setCursor(11,0);
    custom1();
  }     
  if(40 > minutes > 30)  {
    lcd.setCursor(11,0);
    custom1();
  }     
  if(50 > minutes > 40)  {
    lcd.setCursor(11,0);
    custom1();
  }   
//-------------------------------//   
    if(minutes%10 == 1)  {
    lcd.setCursor(15,0);
    custom1();
  }
  if(minutes%10 == 2)  {
    lcd.setCursor(15,0);
    custom2();
  }   
  if(minutes%10 == 3)  {
    lcd.setCursor(15,0);
    custom3();
  }   
  if(minutes%10 == 4)  {
    lcd.setCursor(15,0);
    custom4();
  }   
  if(minutes%10 == 5)  {
    lcd.setCursor(15,0);
    custom5();
  }   
  if(minutes%10 == 6)  {
    lcd.setCursor(15,0);
    custom6();
  }   
  if(minutes%10 == 7)  {
    lcd.setCursor(15,0);
    custom7();
  }   
  if(minutes%10 == 8)  {
    lcd.setCursor(15,0);
    custom8();
  }
  if(minutes%10 == 9)  {
    lcd.setCursor(15,0);
    custom9();
  }
    if(minutes%10 == 0)  {
    lcd.setCursor(15,0);
    custom0();
  }
 
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ;
    Serial.print(c); 
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();         
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number   
        }
      }   
      return pctime;
    } 
  }
  return 0;
}

specifically, can i get opinions on optimisation, and if you think my method for manual adjustment should work (before i can actually test it)

ln 336-346:
Code:
    if(minPlus == HIGH) {
      time_t t = now();
      varHr = hour(t);
      varMin = minute(t);
      varSec = second(t);
      vardd = day(t);
      varmm = month(t);
      varyy = year(t);
      varMin++;   //add 1 minute from the current tine
      setTime(varHr, varMin, varSec, vardd, varmm, varyy);
}

my question itself should a resistor be connected in series with the switch to +5V to limit current into the I/O pin?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
specifically, can i get opinions on optimisation, and if you think my method for manual adjustment should work (before i can actually test it)
Why are all the variables used to set the time global? There is no reason for them to be.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There was no reason they were global. i simply put all of the introductory code at the beginning.

all the ordered parts have arrived for me and i have worked all but one bug out of the programming. when updating the time through the serial display, and even isolating the code so all that gets uploaded is the update command it uploads the time: 17:18:08 29/6/2037 each time i try compiling and uploading.

despite from my understanding that it uploads the system time which is correct (pctime) it does not. here is the update code.
unless i have some wiring set up incorrectly i must be missing something.

[wiring]:
A4 connects to   RTC: SCL
A5 connects to   RTC: SDA
GND to GND
5V to 5V
SQW does not connect to anything (yet?)

and the programming code for clarity
Code:
/*
 * TimeRTCSet.pde
 * example code illustrating Time library with Real Time Clock.
 *
 * RTC clock is set in response to serial port time message
 * A Processing example sketch to set the time is inclided in the download
 */

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t


void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
}

void loop()
{
  if(Serial.available())
  {
     time_t t = processSyncMessage();
     if(t >0)
     {
        RTC.set(t);   // set the RTC and the system time to the received value
        setTime(t);         
     }
  }
   digitalClockDisplay(); 
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

/*  code to process time sync messages from the serial port   */
#define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER  'T'   // Header tag for serial time sync message

time_t processSyncMessage() {
  // return the time if a valid sync message is received on the serial port.
  while(Serial.available() >=  TIME_MSG_LEN ){  // time message consists of a header and ten ascii digits
    char c = Serial.read() ;
    Serial.print(c); 
    if( c == TIME_HEADER ) {       
      time_t pctime = 0;
      for(int i=0; i < TIME_MSG_LEN -1; i++){   
        c = Serial.read();         
        if( c >= '0' && c <= '9'){   
          pctime = (10 * pctime) + (c - '0') ; // convert digits to a number   
        }
      }   
      return pctime;
    } 
  }
  return 0;
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
despite from my understanding that it uploads the system time which is correct (pctime)
What is running on the PC that the Arduino is listening to? Why is the Arduino sending that application time/date information?
Logged

Offline Offline
Edison Member
*
Karma: 64
Posts: 2488
The last thing you did is where you should start looking.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
[wiring]:
A4 connects to   RTC: SCL
A5 connects to   RTC: SDA
GND to GND
5V to 5V
SQW does not connect to anything (yet?)
Arduino Uno
A4 = SDA  ---->  RTC   SDA
A5 = SCL  ---->  RTC   SCL
GND ---------->  RTC   GND
5V    ---------->  RTC   5V

For example if you use        T1360280782
You will see                        23:46:23 7 2 2013      on your serial monitor.


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the only programs that i had running were internal programs only (chrome, eagle, media player etc)

also even when i plug it in to an external power supply and NOT connected to serial monitor the time resets to 17:18 aswell

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ignore that last post, the RTC module wasn't programming correctly. however i think i have it sorted now. just need to test it. i shall return with results.
Logged

Pages: [1]   Go Up
Jump to: