Arduino Time to Julian Day Calculator Free Code

Posted on

Julian Day (JD)

Arduino Time to Julian Day – The Julian day (JD) is a continuous count of days that began at noon on January 1, 4713 BCE (Julian calendar) which is equivalent to November 24, 4714 BCE in the Gregorian calendar. The JD count is widely used in astronomical and historical research as it provides a single, unified count of days that is independent of the calendar system.

In astronomy, Julian day is often used to express the date and time of an event, such as the observation of a planet or a star. The JD includes both the date (year, month, and day) and the time (hours, minutes, and seconds) as a single number. By convention, the JD is expressed as a fraction of a day, with a fractional part that represents the time of day.

For example, a JD of 2,450,000.5 represents noon (12:00:00) on the date 2,450,000. The “0.5” at the end of this number represents 12:00:00. In contrast, a JD of 2,450,000.0 represents the beginning of the day at midnight (00:00:00) on the date 2,450,000.

The JD is widely used by astronomers and researchers in other fields like planetary science, archaeology, history, Earth Science, etc. Because it is a continuous count of days, it makes it easy to compare dates across different calendar systems and convert dates to other calendar systems.


Program Code

void setup() {
  // set baudrate speed
  Serial.begin(9600);
}

void loop() {
  // request input of date, month, and year from the user
  Serial.println("Enter the date (dd):");
  int tanggal = readInt();
  Serial.println("Enter the month (mm):");
  int bulan = readInt();
  Serial.println("Enter the year (yyyy):");
  int tahun = readInt();

  double julianDay = getJulianDay(tahun, bulan, tanggal);

  // displays the conversion results to the serial
  Serial.println("Julian Day Number: " + String(julianDay));
  Serial.println();
}

// Function to calculate the Julian Day from a given date
double getJulianDay(int year, int month, int day) {
  if (month < 3) {
    year--;
    month += 12;
  }
  int a = floor(year / 100.0);
  int b = 2 - a + floor(a / 4.0);
  return floor(365.25 * (year + 4716)) + floor(30.6001 * (month + 1)) + day + b - 1524.5;
}

// function to read integer input from serial screen
int readInt() {
  String input = "";
  while (true) {
    if (Serial.available() > 0) {
      char c = Serial.read();
      if (c >= '0' && c <= '9') {
        input += c;
      } else if (c == '\r' || c == '\n') {
        break;
      }
    }
  }
  return input.toInt();
}

How the program works

The getJulianDay is a function that calculates Julian Day from a given date, where the date is provided as the year, month, and day. The Julian Day is a continuous count of days since the beginning of the Julian Period (4713 BC) and is widely used in astronomy.

Here’s a breakdown of how the function works:

  1. The function first checks if the month is less than 3, if so then the year is decremented by 1 and the month value is incremented by 12. This is to account for the fact that the Julian calendar starts on March 1, so months January and February of a given year are considered to be in the preceding year, in the Julian calendar.
  2. The variable a is then calculated as the integer division of the year by 100. This variable is used in the calculation of the variable b that follows next.
  3. Next, variable b is calculated using the formula: 2-a + floor(a/4.0), where floor function is used to get the largest integer that is less than or equal to the given value. This variable also helps in adjusting the Julian day calculations for leap years.
  4. Finally, the Julian Day is calculated using the formula: floor(365.25 * (year + 4716)) + floor(30.6001 * (month + 1)) + day + b - 1524.5 , which is commonly used to calculate the Julian Day from the given date. The floor function is used here to round down to the nearest integer value.

This function will return a double value of the Julian Day which is calculated using the provided date. The returned value can be used to further calculations like adding the time component as done in your previous code snippet.


Result

The Julian day (JD) is an important concept in astronomy and other related fields. It provides a single, unified count of days that is independent of the calendar system, which makes it easy to compare dates across different calendar systems and to convert dates to other calendar systems.

The Julian day is often used to express the date and time of an event, such as the observation of a planet or a star. It is expressed as a single number with a fractional part that represents the time of day.

Finally, the current time is added to the Julian Day to give the final Julian Day value which is the combination of both the date and time. I hope this Arduino Time to Julian Day Calculator Free Code is useful.


Read more:

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *