Decimal number to HH:MM:SS --Solved--

Hi

I have this code were i need to print the local sidereal time. in hours, minutes and seconds, just like a normal clock.
So far the code has only reduced it to show the hours correctly. Minutes are still in base10.
How do i continue to get the minuts and seconds right?

Here is the code section:
double LST = GMST + 0.00000000000000; // longitude as hour angle.

** //reduce it to 24 format**

** int LSTint; **
** LSTint = (int)LST; **
** LSTint /= 24; **
__ LST = LST - (double)LSTint * 24; // How do i continue to get minutes and seconds??__

Initier code here

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



RTC_DS1307 RTC;

void setup()
{
Serial.begin(9600);
pinMode(13, HIGH); // Turns off the LED on pin 13, nothing else.
Wire.begin();
RTC.begin();

//RTC.adjust(DateTime(__DATE__, __TIME__));   //<< uncomment to set time
if (!RTC.isrunning()) {
	Serial.println("RTC is NOT running!");
		
}

}


void loop() {

	
	CalculateLST();
	GetRealTime();
	delay(2000);
}


void GetRealTime() {            	//Get Present time from RTC DS1307
	DateTime now = RTC.now();
	   
	   Serial.print(now.day(), DEC);
	   Serial.print('/');
	   Serial.print(now.month(), DEC);
	   Serial.print('/');
	   Serial.print(now.year(), DEC);                         
	   Serial.print(' ');
	   Serial.print(now.hour(), DEC);
	   Serial.print(':');
	   Serial.print(now.minute(), DEC);
	   Serial.print(':');
	   Serial.print(now.second(), DEC);
	   Serial.println();
	   
}


void CalculateLST() {   	// Calculate Local Sidereal Time
	DateTime now = RTC.now();
	
	 double M, Y, D, MN, H, S;
	 double A, B, C, E, F;

	 M = (int)now.month();
	 Y = (int)now.year();
	 D = (int)now.day();
	 MN = (int)now.minute();
	 H = (int)now.hour()-1;  // Hours must be GMT/UT.
     S = (int)now.second();

	 if (M < 3) { M += 12; Y -= 1; }

	 A = (long)Y / 100;
	 B = (long)A / 4;
	 C = (long)2 - A + B;
	 E = (long)(365.25*(Y + 4716));
	 F = (long)(30.6001*(M + 1));

	double LastJDN = C + D + E + F - 1524.5; // Julian day last midnight UT 
	double Current_d = LastJDN - 2451545.0;   //Days since Year 2000
	long Current_T = Current_d / 36525;  // Julian centuries since Year 2000
	long NewJDN = LastJDN + H/24;  // Julian day today
		
	Serial.println();
	Serial.print("Julian Day Number..: ");
	Serial.print(NewJDN);  // Julian day today 
	
	double Term1 = 6.697374558;   // this line must be a double!
	double Term2 = 0.06570982441908*Current_d; 
	 

    
	H = H + ((double)MN / 60) + ((double)S / 3600);
	
	float GMST;
	float Term3;

	Term3 = 0.00273790935*H; 
	Term3 += H;
	GMST = Term1 + Term2 + Term3;  // Terms are found at httP://aa.usno.navy.mil/faq/docs/GAST.php  
	
	//add on longitude divided by 15 to get LST
	
	double LST = GMST + 0.00000000000000;  // longitude as hour angle.
	
	//reduce it to 24 format
	
	int LSTint;												
	LSTint = (int)LST;                  
	LSTint /= 24;                       
	LST = LST - (double)LSTint * 24;    // How do i continue to get minutes and seconds??
	

	Serial.println();
	Serial.print("Local Sidereal time: ");  
	Serial.print(LST);
	Serial.println();
}


void MotorSpeedx1() {
	// Sets motorSpeed to track the stars, future project.
}
    LSTint = (int)LST;            
    LSTint /= 24;                      
    LST = LST - (double)LSTint * 24;    // How do i continue to get minutes and seconds??

This way:

LSTint = (int) LST/60;
minutesTens =  (int) LSTint /10;
minutesUnits =  LSTint % 10;
LST = LST - LSTint * 60;

LSTint = (int) LST/60;
int secondsTens =  LSTint /10;
int secondsUnits =  LSTint % 10;

// longitude as hour angle.

What the heck does "hour angle" mean?

Why are you casting ints to ints to store in doubles?

double Term1 = 6.697374558; // this line must be a double! double Term2 = 0.06570982441908*Current_d;

Unless you have a Due, doubles and floats are the same type, and do not have anywhere that much precision.

int LSTint; LSTint = (int)LST; LSTint /= 24; LST = LST - (double)LSTint * 24; // How do i continue to get minutes and seconds??

Perhaps if you used meaningful names, the answer to the question would become obvious. LSTint doesn't mean a thing. Print LST before and after this code. I think it will become quite obvious that lSTint really should be called.

Sorry Poul i did not explain that. 360 degrees equals 24 hours. (One revolution of planet earth is 24 hours) 1 hour equals 15 degree.

So any longitude in degrees must be divided by 15 to get the HA.

LST means local sidereal time, or star time if you wan't. There are really no meaningfull names for it.

LST works by the hour and minutes but minutes goes to 100 not 60.

I must admit that i snatched the code from someone else, and many of the variables were not defined. But it works with doubles nothing else.

About LSTint, i had to define it myself, it my be a wrong approach but it works, what else should i call it ?

If i print LSTint i get something completely different.

I tried to make Term1 as a long but i lost 3 minuts of accuracy i don't know why

The code really works :confused:

LST works by the hour and minutes but minutes goes to 100 not 60.

That is not quite correct. Sidereal time works just like a regular clock, based on the solar day, except that in 24 sidereal hours, there are 23h 56m 04s of civil time. The Earth moves in its orbit, so that the stars do not appear to be in the same place at the same time each night.

For one brief discussion see Local Sidereal Time

If you look around, you can find large collections of C code that do all these conversions for you. Otherwise, it would be good to brush up on the difference between integer and float arithmetic.

I think you misunderstand.

Right now my sidereal clock shows 4.74.00 and that's the problem it should show 4.44.00

It's quite simple 74*60/100 = 44 But how do i do that in the sketch

Since the hour angle is set to 0 in my sketch Greenwich mean Sidereal time equals LST. But lets forget all that. All i need to is to convert a decimal clock to a real clock time, nothing else.

As I said, you [u]desperately need[/u] to review floating point versus integer arithmetic.

But here is how to convert decimal hours into integer h, m, s.

void setup() 
{
float hours = 4.77; //for example
int h = hours;
float ms = (hours - h)*60.;
int m = ms;
int s = (ms - m)*60.;

Serial.begin(9600);

Serial.print("decimal hours ");
Serial.println(hours);
Serial.print("clock time ");
Serial.print(h);
Serial.print(":");
Serial.print(m);
Serial.print(":");
Serial.println (s);
}
void loop() {}

Here is the source

http://astro.neutral.org/arduino/local-sidereal-time-arduino-microcontroller.shtml

Perhaps i did something wrong, but it works. Except for minutes counting to 100.

Problem solved. And just as simple as i hoped for.
Thank you Henry_Best for leading me in the right direction :slight_smile:

//reduce it to 24 format
	
	int LSTint;												
	LSTint = (int)LST;                  
	LSTint /= 24;                       
	LST = LST - (double)LSTint * 24;    
	
	int LST_H = (int)LST;
	int LST_M = ((LST - LST_H) * 60);
	int LST_S = (((LST - LST_H) * 60) - LST_M) * 60;
	

	


	Serial.println();
	Serial.print("Local Sidereal time: ");  
	Serial.print(LST_H);
	Serial.print(':');
	Serial.print(LST_M);
	Serial.print(':');
	Serial.println(LST_S);