Pages: [1]   Go Down
Author Topic: Unable to determine week number for a given date - pl help  (Read 228 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am new to Adruino and learning to use it for my project, part of which requires to get the week number from given date - with week starting from Sundays. As a pre-requisite to get week number, I need to know the day of week too.

I searched the internet and copy paste codes but it is not helping me.

Presently my program has following code ( copied from web site)  - which I understand fully, but unable to get the value of      "w"  ( which as per my understanding must be the Day of Week). The problem is  " DoW"  or " DayofWeek" or "Weekday"
 functions are bringing no result and causing error, as if they are not being recognized.


 int days[]={0,31,59,90,120,151,181,212,243,273,304,334};    // Number of days at the beginning of the month in a non leap year.
// calculating week number 
        if (mth==1 || mth==2){DN = days[(m-1)]+dy;}                      //(if m=1 OR m=2)calculate the number of days for January or february ( both leap & non-leap)
        else if ((yr % 4 == 0 && yr % 100 != 0) ||  yr % 400 == 0){    //those are the conditions to have a leap year
        DN = days[(mth-1)]+dy+1;}                                      // if leap year, calculate in the same way but increasing one day
        else {DN = days[(mth-1)]+dy;}                                  //if not a leap year, calculate in the normal way, such as January or February
        // Now start to calculate Week number
        if (w==0){WN = (DN-7+10)/7;}                                //if it is sunday (time library returns 0)
        else{
        WN = (DN-w+10)/7; }                                         // for the other days of week
 

Any help to bring me on track will be highly appreciated.

Thanks in advance.


* error message.JPG (69.85 KB, 1250x313 - viewed 16 times.)
Logged

Saskatchewan, Canada
Offline Offline
Edison Member
*
Karma: 49
Posts: 1385
Coding Geezer
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It would be best if you post your entire code (and put it within code tags please). If you are using the DateTime library available here on the libraries page, then the property for the day of the week is  DayofWeek.

byte DayofWeek;
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 522
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The specific error you are getting in your screen shot is because there is no method with the name weekday() in the class DateTime. This suggests that you are using the wrong method name. The definitive answer for this will be the DateTime.h file that came with the library, which contains the definitions for all methods in the class.
Logged

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

Thank you both for replies.

 Yes, I felt the same that I am missing some file. I am using Wire.h and RTClib.h in my code ... where will I get the DateTime.h library from ?

I know that following code does not give a pro look, please never mind it.

Code:
#include <Wire.h>
#include "RTClib.h"
 
  RTC_DS1307 RTC;
  int pwr = A3;          //  setting pwr         
  int grd = A2;          //  and grd for Real Time Clock (RTC)
        short DN;         // Returns the number of day in the year
        short WN;         // Returns the number of the week in the year
        unsigned int yr;
        unsigned int mth;
        unsigned int dy;
        unsigned int w;
        unsigned int yy;  // ten of year e.g 1 in 2013
        unsigned int y;   // unit of year e.g 3 in 2013
        unsigned int mm;  // ten of month e.g 1 in 12
        unsigned int m;   // unit of month e.g 2 in 12
        unsigned int dd;  // ten of day e.g 2 in 25
        unsigned int d;   // unit of day e.g 5 in 25
        int days[]={0,31,59,90,120,151,181,212,243,273,304,334};    // Number of days at the beginning of the month in a non leap year.
     
void setup ()
  {
   pinMode(pwr, OUTPUT);     
   pinMode(grd, OUTPUT);
   digitalWrite(pwr, HIGH);   
   digitalWrite(grd, LOW);
   Serial.begin(9600);
   Wire.begin();
   RTC.begin();
   // following line sets the RTC to the date & time this sketch was compiled
   //RTC.adjust(DateTime(__DATE__, __TIME__));
  }
   
void loop () {
 
 DateTime now = RTC.now();
 
 yr=(now.year());
 mth=(now.month());
 dy=(now.day());
 
        // calculating week number  refer http://en.wikipedia.org/wiki/ISO_week_date
        if (mth==1 || mth==2){DN = days[(m-1)]+dy;}                      //(if m=1 OR m=2)calculate the number of days for January or february ( both leap & non-leap)
        else if ((yr % 4 == 0 && yr % 100 != 0) ||  yr % 400 == 0){    //those are the conditions to have a leap year
        DN = days[(mth-1)]+dy+1;}                                      // if leap year, calculate in the same way but increasing one day
        else {DN = days[(mth-1)]+dy;}                                  //if not a leap year, calculate in the normal way, such as January or February
        // Now start to calculate Week number
        if (w==0){WN = (DN-7+10)/7;}                                //if it is sunday (time library returns 0)
        else{
        WN = (DN-w+10)/7; }                                         // for the other days of week
             
 {   
    yy = (yr % 100) / 10;  // getting tens of year
    y = (yr % 10);         // getting unit of year
    mm = (mth % 100) / 10;  // getting tens of month
    m = (mth % 10);        // getting unit of month
    dd = (dy % 100) / 10;  // getting tens of days
    d = (dy % 10);        // getting unit of day
  }
      Serial.print("weekno: ");
      Serial.print(WN);
      delay (1000);
}
Logged

London
Offline Offline
Edison Member
*
Karma: 46
Posts: 1368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        if (mth==1 || mth==2){DN = days[(m-1)]+dy;} 
  This will be true whether or not it's a leap year, so the following else statement will always be ignored.
Logged

Offline Offline
God Member
*****
Karma: 17
Posts: 522
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you both for replies. Yes, I felt the same that I am missing some file. I am using Wire.h and RTClib.h in my code ... where will I get the DateTime.h library from ?

If you aren't already using the DateTime library, then don't start. It has been superceded by this one:

http://playground.arduino.cc/Code/Time
Logged

Pages: [1]   Go Up
Jump to: