Pages: [1]   Go Down
Author Topic: Maths expert required  (Read 641 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 154
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
I am trying to do an event countdown with the arduino. I am very new to the programming language and am trying to feel my way through.
I am using a ds1307 and I made some code that is 'supposed' to perform a calculation but it is returning the wrong values.

I used the code from here https://sites.google.com/site/tronixstuff/home/arduino-tutorial-series-files/example7p3.pde?attredirects=0
as the base for my code which sets up the ds1307 and reads the time from it and you can manually set the time. The reason I used this code is because the ladyada tutorial the rtc1307 is set automatically.

I then wrote some code that tries to take the date and perform a calculation on it.
Because the number it returns is higher than 65,535 I am not sure what to use. Do I use int or long in my code instead of byte and word?
My second issue stems from the fact that the data extracted and printed to the serial monitor for the year is only 2 digits because it is a byte. I need to return the entire year for my calculation to work. It uses the whole 2011 as part of the equation. Which is why I have added +2000 to my equations.
I have set the ds1307 to the 2/9/2011 and the number it should be returning is 114 days. But the days and daysf values which are printing to the serial monitor are incorrect. The results should be days=734781 and daysf=734895. It currently returns days=13889 and daysf=13999 and returns 110.

The code for my calculation is below.
I created 2 global bytes called yearA and monthA which stands for year adjusted and month adjusted and 2 bytes called days and daysf which stands for days future.
The problem probably lies in my use of calculation functions or the way I have set up the storage for the integers. Could someone please tell me how to debug it easier? I really need to see what value is actually being used in the calculation for year and yearA and month and monthA.
Code:
if (month <3) {//if month is January or February subtract 1 from year and use in yearA
      yearA=year-1;
    }
    else {
      yearA=year;
    }
    if (month <3) {//if month is January or February add 12 to the month and use in monthA
    monthA=month +12;
    }
    else {
    monthA=month;
    }
  //  Serial.println(dayOfWeek, DEC);
  delay(1000);
  /*The following calculates days to set countdown with the following formula
    If month is 1 or 2 add 12 to the month and subtract 1 from the year
    then apply the following formula
    365*year + year/4 - year/100 + year/400 + date + (153*month+8)/5 all integers rounded down
    Do this for the current date and the future date and subtract one from the other then
    display the days remaining on the 7 segment display*/
    days=365*(yearA+2000) + (2000+yearA)/4 - (2000+yearA)/100 + (2000+yearA)/400 + dayOfWeek + (153*monthA+8)/5;
    /* Below is the function to perform the calculation on the date till the event.
    I have set the now.year because I will always be counting down till the
    25th December in the current year. Just alter the year and month and day values
    for other dates*/
    daysf=365*(year+2000) + (2000+year)/4 - (2000+year)/100 + (2000+year)/400 + 25 + (153*12+8)/5;
    Serial.println (days, DEC);//for debugging
    Serial.println(daysf, DEC);//for debugging
    Serial.println(daysf-days);//This is the number I will shift out to the7 segment leds.
Sorry to ask for help on something that takes so long to explain! I am pulling my hair out. Manually the calculations works perfectly.
Thanks
Steve
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26313
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Because the number it returns is higher than 65,535 I am not sure what to use.
"long" or "unsigned long".
Watch out for simple constants which will be taken as "int"s unless you tag an "L" on the end.
Code:
days=365L*(yearA+2000L) + (2000L+yearA)/4L - (2000L+yearA)/100L + (2000L+yearA)/400L + dayOfWeek + (153L*monthA+8L)/5L;
just to be sure.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Full Member
***
Karma: 0
Posts: 154
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for that. I have added the L to the constants which has bought it closer to the correct figure. It is now in the 700,000's but still 4 days off.

I added Serial.println (yearA, DEC) and Serial.println (year, DEC) too to debug that variable and it is returning 11 which is correct in this case so the formula should be working.
I will have to go through it slowly again.

Thanks for the tip.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 154
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

All sorted now. It was a "duh" moment. I was using dayofWeek instead of dayofMonth in my code.

Thanks for your tip, without it I wouldn't have got the correct answer out.

Appreciate your help.
Logged

Pages: [1]   Go Up
Jump to: