Go Down

Topic: Maths expert required (Read 1 time) previous topic - next topic


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: [Select]

if (month <3) {//if month is January or February subtract 1 from year and use in yearA
    else {
    if (month <3) {//if month is January or February add 12 to the month and use in monthA
    monthA=month +12;
    else {
  //  Serial.println(dayOfWeek, DEC);
  /*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.


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: [Select]
days=365L*(yearA+2000L) + (2000L+yearA)/4L - (2000L+yearA)/100L + (2000L+yearA)/400L + dayOfWeek + (153L*monthA+8L)/5L;
just to be sure.
"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.
I speak for myself, not Arduino.


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.


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.

Go Up