DS1307

hi, need help with my arduino mega 2560 and RTC. how can i display the right weekday from this code? .now doesnt work.

#include <Wire.h>
#include "RTClib.h"
#include <Time.h>

RTC_DS1307 RTC;

char *monthName[13] = {
  "" ,"January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
};

void setup () {
    Serial.begin(9600);
    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(dayStr(weekday()));
    Serial.println(monthName[now.month()]);  
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print('/');
    Serial.print(now.year(), DEC);
    Serial.print(' ');
    Serial.print(now.hour());
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    
  
    delay(1000);
}

Thank you! :slight_smile:

.now doesnt work.

Please explain.

Does the code compile?

     Serial.print(dayStr(weekday()));

Shouldn't that be now.weekday()?

The DS1307 outputs a number for the DoW and you switch on case.

There is a system for it here http://bildr.org/2011/03/ds1307-arduino/

which I use as a standard code.

//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include <PCD8544.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68
static PCD8544 lcd;

void setup(){
  Wire.begin();
  Serial.begin(9600);
  lcd.begin(84, 48);
  
  // Print a message to the LCD.
    lcd.setCursor(0,0);
  lcd.print("Today it is");
}

void loop(){
  printDate();
  delay(1000);

}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());


  lcd.setCursor(21,1);
    switch (weekDay)               // Friendly printout the weekday
  {
    case 1:
      lcd.print("MONDAY  ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUESDAY  ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WEDNESDAY  ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THURSDAY  ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRIDAY  ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SATURDAY  ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUNDAY  ");
       Serial.print("SUN  ");
      break;
  }

  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
 
   lcd.setCursor(0,4); 
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
  
  lcd.setCursor(0,5);
  
   if( second==0)
  {
   lcd.print("         ");
   lcd.setCursor(0,5);
   } 
  
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second);  
}

PaulS, no sir, their is an error:

RTC.ino: In function 'void loop()':
RTC:28: error: 'class DateTime' has no member named 'weekday'

RTC:28: error: 'class DateTime' has no member named 'weekday'

My version of the Time library does, but it doesn't have a class called DateTime.

So, you need to post a link to the libraries you are using.

I got all the libraries from here sir, Arduino Playground - HomePage, do you have a better library?

I got mine from there, too. Did you follow the links in the Update: note, or the one in the next paragraph?

no sir, i didn't saw any update their i only downloaded the zip file. can you give me the link sir? thank you very much! :slight_smile:

Is this code right?

//Arduino 1.0+ Only
//Arduino 1.0+ Only

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

char *weekName[13] = {
  "Monday" ,"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
};

char *monthName[13] = {
  "" ,"January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
};

void setup(){
  Wire.begin();
  Serial.begin(9600);
}

void loop(){
  printDate();
  delay(1000);
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(weekName[weekDay]);
   Serial.print("/");
  Serial.print(monthName[month]);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);

}

and how can make my hour to only 12 and put some AM and PM? thank you to the one that can give the answer.. :slight_smile:

kengwapo:
and how can make my hour to only 12 and put some AM and PM? thank you to the one that can give the answer.. :slight_smile:

boolean PM = false;
if(hour > 12)
  {
  hour =- 12;
  PM = true;
  }
if(PM)
  { Serial.println(" PM"); }
else
  { Serial.println(" AM"); }