Issue with long and double


hi,

i have problem using long , double values on arduino uno.

my code:


code: [select]
    #include <ds3231.h>

    ds3231  rtc(sda, scl);
 
    void setup() {
      serial.begin(9600);
     rtc.begin(); // initialize rtc object
  //      rtc.setdow(friday);     // set day-of-week sunday
//  rtc.settime(19, 35, 0);     // set time 12:00:00 (24hr format)
 // rtc.setdate(30, 12, 2016);   // set date january 1st, 2014
 
    }
void loop()
{
 
  double year;
  double yearfloor;
  double yearrest;
 
  double month;
  double monthfloor;
  double monthrest;

  double week;
  double weekfloor;
  double weekrest;
 
  double day;
  double dayfloor;
  double dayrest;
 
  double hour;
  double hourfloor;
  double hourrest;
 
  double minute;
  double minutefloor;
  double minuterest;
 
  double second;
  double secondfloor;
  double secondrest;
 
  long startdate = 1217980800;
  double currenttimestring;
  long unixtimestamp;
 
  unixtimestamp = rtc.getunixtime(rtc.gettime());
 
  serial.println(unixtimestamp);
  serial.println(rtc.getunixtime(rtc.gettime()));
 
  currenttimestring = (double)unixtimestamp - startdate;
 
  serial.println(currenttimestring);
  serial.println(unixtimestamp);



  year = double(currenttimestring)/31536000.00;
  yearfloor = floor(year); 
  yearrest = year - yearfloor;
 
  month = yearrest * 12;
  monthfloor = floor(month);
  monthrest = month - monthfloor;

  week = monthrest * 4;
  weekfloor = floor(week);
  weekrest = week - weekfloor;
 
  day = weekrest * 7;
  dayfloor = floor(day);
  dayrest = day - dayfloor;
 
  hour = dayrest * 24;
  hourfloor = floor(hour);
  hourrest = hour - hourfloor;
 
  minute = hourrest * 60;
  minutefloor = floor(minute);
  minuterest = minute - minutefloor;
 
  second = minuterest * 60;
  secondfloor = floor(second);
  secondrest = second - secondfloor;
 
  serial.print("jahre: ");
  serial.println(yearrest);
  serial.println(yearfloor);
  serial.print("monate: ");
  serial.println(monthrest);
  serial.println(monthfloor);
  serial.print("wochen: ");
  serial.println(weekrest);
  serial.println(weekfloor);
  serial.print("tage: ");
  serial.println(dayrest);
  serial.println(dayfloor);
  serial.print("minuten: ");
  serial.println(minuterest);
  serial.println(minutefloor);
  serial.print("sekunden: ");
  serial.println(secondrest);
  serial.println(secondfloor);
 
  serial.println(" -- ");
  serial.println(" -- ");
 
  // wait 1 second before repeating
  delay (2000);
}




output:
code: [select]
0.41
8.00
monate: 0.90
4.00
wochen: 0.60
3.00
tage: 0.17
4.00
minuten: 0.84
58.00
sekunden: 0.57
50.00
 --
 --
1483142804
1483142804
265161984.00
1483142804
jahre: 0.41
8.00
monate: 0.90
4.00
wochen: 0.60
3.00
tage: 0.17
4.00
minuten: 0.84
58.00
sekunden: 0.57
50.00
 --
 --
1483142807
1483142807
265161984.00
1483142807
jahre: 0.41
8.00
monate: 0.90
4.00
wochen: 0.60
3.00
tage: 0.17
4.00
minuten: 0.84
58.00
sekunden: 0.57
50.00
 --
 --



currenttimestring = (double)unixtimestamp - startdate;
does not work think, because
serial.println(currenttimestring);
does not updated after each loop iteration.
serial.println(unixtimestamp);
works fine..

can me solve problem, please?

thanks

short summary issue:
code:
serial.println(rtc.getunixtime(rtc.gettime()));
serial.println(float (rtc.getunixtime(rtc.gettime())));


output:
1483146725
1483146752.00


Arduino Forum > Using Arduino > Programming Questions > Issue with long and double


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor