jg53fn55
Published © GPL3+

Astronomical Clock with RTC ds3231

An astronomical clock with 16 functions, with only three shift registers

IntermediateShowcase (no instructions)Over 1 day1,013
Astronomical Clock with RTC ds3231

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
or an ATMEL328 processor
×1
Grove - Barometer Sensor (BMP280)
Seeed Studio Grove - Barometer Sensor (BMP280)
×1
DS3231MPMB1 Peripheral Module
Maxim Integrated DS3231MPMB1 Peripheral Module
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1
Resistor 220 ohm
Resistor 220 ohm
×8
Toggle Switch, Toggle
Toggle Switch, Toggle
×4
Shift Register- Parallel to Serial
Texas Instruments Shift Register- Parallel to Serial
or shiftregister SN74595
×3
Transistor driver UDN2981
or ULN2803 or eqv.
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

astronomical clock

It is a clock with RTC to show time, date sun set, rise and much more

Code

astronomical clock

Arduino
// made by Jan G from The Netherlands
// Astronomical clock using only three shift registers SN74595
// Date 2022/JUN/01
// Also using multiplexing
#include <Wire.h>
#include <BMx280I2C.h>
#define I2C_ADDRESS 0x76
BMx280I2C bmx280(I2C_ADDRESS);
#define PI 3.141592653
#define DEG_TO_RAD 0.01745329251
#define RAD_TO_DEG 57.2957795130

#include "Button2.h"; //  https://github.com/LennartHennigs/Button2
#include "Rotary.h";
#define ROTARY_PIN1  A1
#define ROTARY_PIN2 A2
#define BUTTON_PIN  A0
#define CLICKS_PER_STEP 4   // this number depends on your rotary encoder
#define MIN_POS         -500
#define MAX_POS         500
#define START_POS       0
#define INCREMENT       1   // this number is the counter increment on each step
Button2 bt;
Rotary rt;
const byte interruptPin = 2;

byte dataPin1 = 5;
byte latchPin1 = 6;
byte clockPin1 = 7;
byte SW1 = 9;
byte SW2 = 10;
byte SW3 = 11;
byte SW4 = 12;


/*
  Showing number 0-9 on a Common Anode 7-segment LED display
  Displays the numbers 0-9 on the display,
      A
     ---
  F |   | B
    | G |
     ---
  E |   | C
    |   |
     ---
      D
  display segment     :  G F E D C B A dp
  output shiftregister:  7 6 5 4 3 2 1 15 dec   inv 
  display number     0   0 1 1 1 1 1 1 0   126  129
                     1   0 0 0 0 1 1 0 0    12  243
                     2   1 0 1 1 0 1 1 0   182   73
                     3   1 0 0 1 1 1 1 0   158   97
                     4   1 1 0 0 1 1 0 0   204   51
                     5   1 1 0 1 1 0 1 0   218   37
                     6   1 1 1 1 1 0 0 0   250    5
                     7   0 0 0 0 1 1 1 0    14  241
                     8   1 1 1 1 1 1 1 0   254    1
                     9   1 1 0 1 1 1 1 0   222   33
                     
  in my project the dot =1, the dot is output 15 of the shiftregister
  A = 2  , A is output 1 of the shiftregister
  B = 4  , B is output 2 of the shiftregister
  C = 8  , C is output 3 of the shiftregister
  D = 16  , D is output 4 of the shiftregister
  E = 32  , E is output 5 of the shiftregister
  F = 64  , F is output 6 of the shiftregister
  G = 128  , G is output 7 of the shiftregister


  number zero = A+B+C+D+E+F = 126 >> invert >> 255-126 = 129
  number one = B+C = 12 >> invert >> 255-12 = 243
  number two = A+B+D+E+G = 182 >> invert >> 255-182 = 73
  number three = A+B+C+D+G = 158 >> invert >> 255-158 = 97
  etc... for the numbers 4 - 9
  see Data1[10]{129,243,........
  when you have a Common Kathode display, then change the next array to:
  byte Data1[10] = { 126, 12, 182, 158, 204, 218, 250, 14, 254, 222 };
  byte Digit1[10] = { 0, 16, 128, 144, 2, 18, 130, 146, 68, 84 };
  byte DisplaySelect1[12] = { 253, 251, 247, 239, 223, 191, 255, 255, 255, 255, 255, 255};
  byte DisplaySelect2[12] = { 255, 255, 255, 255, 255, 255, 253, 251, 247, 239, 223, 191};
*/
byte Data1[10] = { 129, 243, 73, 97, 51, 37, 5, 241, 1, 33 };
//----
byte Digit1[10] = { 255, 239, 127, 111, 253, 237, 125, 109, 187, 171 };
//------
byte DisplaySelect1[13] = { 2, 4, 8, 16, 32, 64, 0, 0, 0, 0, 0, 0, 0};

byte DisplaySelect2[13] = { 0, 0, 0, 0, 0, 0, 2, 4, 8, 16, 32, 64, 0};
//-----

byte Schrikkel = 0;
int Dagtotaal[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
int DagtotaalS[12] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
//------------ maan  moon -----------------------------------------------------------------
byte Maan[20] = { 2, 13, 24, 5, 16, 27, 8, 19, 30, 11, 22, 3, 14, 25, 6, 17, 29, 10, 21 };
//---------------- nederlands--------------------------------------------------------
// epacta getallen
//eerste getal van Maan[20] <2> is het jaar 2017, de maan is op 1 januari 2 dagen oud,
//getal 13 is het jaar 2018, de maan is dan op 1 januari 13 dagen oud, enz..
//elk jaar verschuift 11, behalve eens in de 19 jaar, dan 12.
// laatste was in 2014 dan in 2033, en in 2052
//----------------------english-------------------------------------------
//epacta numbers
// first number of array Maan[20] is 2. This number is the year 2017, the moon age in days is 2 on januari first
//The next number is 2018, the moon age in days is 13 also on januari first, etc..
// every year ads 11, except once in 19 years, then it wil be 12
// latest was in 2014, then in 2033 etc...
// moonage = 0 >> new moon, moonage = 14.75 >> full moon
// moonage = 7.375 >> first quarter,   moonage = 22.125 >> last quarter
// from new moon to the next new moon = 29.5 days
byte AR;
int second;
int minute;
int hour;
int weekday;
int monthDay;
int month;
int year;
int Sjaar;
byte Test;
byte test1;
byte test2;
byte test3;
byte test4;
byte a = 0;
byte b = 0;
byte c = 0;
byte d = 0;
byte e = 0;
byte f = 0;
byte g = 0;
byte h = 0;
byte i = 0;
byte j = 0;
byte k = 0;
byte m = 0;
byte WT = 1;
double aa = millis();
// The variables HDT,TDT,DT,HT,TT and EH are used in the subroutine Factor();
long HDT;// hundred thousand ( when number is 123456, HDT = 1)
long TDT;// ten thousand ( when number is 123456, TDT = 2)
long DT;//thousand ( when number is 123456, DT = 3)
long HT;// hundred ( when number is 123456, HT = 4)
long TT;// tens ( when the number is 123456, TT = 5)
long EH;// unity (when the number is 123456, EH = 6)
float temperatuur, pressure, LV;
int Lentepunt;
int Dagen;
float ZZO;
float ZZN;
float Stijd1;
float Maanq;
float ZonOp;
float ZonOnder;
float DagLengte;
float BG = 52.9;// latitude
float LG = 4.8;// longitude
unsigned long currentMillis;
long interval = 3000;
long previousMillis = 0;
volatile int encoderPos = 0;
volatile byte secondsInterrupt = 0;
int buttonPressed = 0;
int counter = 0;
//----------------------------------------------------------------------
void setup() {
  pinMode(interruptPin, INPUT_PULLUP);
  pinMode(latchPin1, OUTPUT);
  pinMode(clockPin1, OUTPUT);
  pinMode(dataPin1, OUTPUT);
  pinMode(SW1, INPUT_PULLUP);
  pinMode(SW2, INPUT_PULLUP);
  pinMode(SW3, INPUT_PULLUP);
  pinMode(SW4, INPUT_PULLUP);

  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(interruptPin), secondsRTC, FALLING);
  Wire.begin();  // begin I2C routine

  bt.begin(BUTTON_PIN);
  bt.setPressedHandler(pressed);
  bt.setReleasedHandler(released);

  rt.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP, MIN_POS, MAX_POS, START_POS, INCREMENT);
  rt.setLeftRotationHandler(showDirection);
  rt.setRightRotationHandler(showDirection);
  zet_24uurmode();

  SQW1hzOn();
  // DS3231 seconds, minutes, hours, day of week, date, month, year
  // first time set of the RTC. remove the '//' in next line to set the time, reinstall it afterwards
  //setDS3231time(00,58,10,1,29,07,22);
  bmx280.begin();
  bmx280.resetToDefaults();
  bmx280.writeOversamplingPressure(BMx280MI::OSRS_P_x16);
  bmx280.writeOversamplingTemperature(BMx280MI::OSRS_T_x16);
  delay(2000);
  TijdUitlezen();// read the time and date variables

  Displaytest1();// display test
  Displaytest2();// display test
  Displaytest3();// display test
  displayuit();// display off
  delay(2000);
  // 7 segment display is LTS3401


}
//------------------------------------------------------------------
void secondsRTC(void) {  // *** ISR iedere seconde ***
  secondsInterrupt = 1;  // ISR vlag = 1 : er was een 'seconds' interrupt.
}

//------------------------------------------------------------------------
byte AgingRead() {
  return leesroutine(16);
}

//------------------------------------------------------------------------
void zet_24uurmode() {// set 24 hour mode
  byte uurreg;
  uurreg = leesroutine(2);
  //Serial.println(uurreg);
  if (uurreg > 64) { // bit 6 = 64, set to zero
    uurreg = uurreg - 64;
    instelroutine(2, uurreg);
  }
}

//------------------------------------------------------------------------
byte lees_seconde() { // read seconds
  return  BcdToDec(leesroutine(0));
}
//------------------------------------------------------------------------
byte lees_minuut() { //read minute
  return  BcdToDec(leesroutine(1));
}
//------------------------------------------------------------------------
byte lees_uur() {  // read hour
  return  BcdToDec(leesroutine(2));
}
//------------------------------------------------------------------------
byte lees_dagweek() { // read DOW
  return  BcdToDec(leesroutine(3));
}
//------------------------------------------------------------------------
byte lees_datum() { // read day of month
  return  BcdToDec(leesroutine(4));
}
//------------------------------------------------------------------------
byte lees_maand() { // read month
  return  BcdToDec(leesroutine(5));
}

//------------------------------------------------------------------------
byte lees_jaar() {  // read year
  return  BcdToDec(leesroutine(6));
}
//------------------------------------------------------------------------
byte BcdToDec(byte val) {
  return  (val / 16 * 10) + (val % 16);// bcd to decimal
}
//------------------------------------------------------------------------
byte leesroutine(byte adres) {// read register of RTC

  Wire.beginTransmission(0x68); // adress RTC DS1307 or DS3231
  Wire.write(adres);       // address  register DS3231
  Wire.endTransmission();       // einde I2C routine
  Wire.requestFrom(0x68, 1);
  return  Wire.read();
}
//------------------------------------------------------------------------
void instelroutine(byte adres, byte data1) {// set register of RTC
  Wire.beginTransmission(0x68); // adress RTC DS3231
  Wire.write(adres);       // address register DS3231
  Wire.write(data1);
  Wire.endTransmission();       // einde I2C routine
}
//------------------------------------------------------------------------
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth,
                   byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(0x68);
  Wire.write(0);// set next input to start at the seconds register
  Wire.write(decToBcd(second));// set seconds
  Wire.write(decToBcd(minute));// set minutes
  Wire.write(decToBcd(hour));// set hours
  Wire.write(decToBcd(dayOfWeek));// set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth));// set date (1 to 31)
  Wire.write(decToBcd(month));// set month
  Wire.write(decToBcd(year));// set year (0 to 99)
  Wire.endTransmission();
}
//------------------------------------------------------------------------
byte decToBcd(byte val) {
  return ((val / 10 * 16) + (val % 10));
}
//------------------------------------------------------------------------
void Agingadjust(byte data1) {// fine adjust the frequency of the RTC

  instelroutine(16, data1);
  /*
     Positive aging values add capacitance to the array, slowing  the
     oscillator  frequency.  Negative  values  remove  capacitance from the array,
     increasing the oscillator frequency. The change in ppm per LSB is different
     at different temperatures. The frequency vs. temperature curve is shifted by
     the  values  used  in  this  registert.  At  +25C,  one  LSB  typically
     provides about 0.1ppm change in frequency.
  */
}
//------------------------------------------------------------------------
void SQW1hzOn() {// switch the 1 Hz SQW on
  instelroutine(14, 0);
}
//------------------------------------------------------------------------
void zet_jaar(byte data1) { // set year
  instelroutine(6, decToBcd(data1));
}
//------------------------------------------------------------------------
void zet_maand(byte data1) { // set month
  instelroutine(5, decToBcd(data1));
}
//------------------------------------------------------------------------
void zet_datum(byte data1) { // set day of month
  instelroutine(4, decToBcd(data1));
}
//------------------------------------------------------------------------
void zet_dagweek(byte data1) { // set DOW
  instelroutine(3, decToBcd(data1));
}
//------------------------------------------------------------------------
void zet_uur(byte data1) { // set hour
  instelroutine(2, decToBcd(data1));
}
//------------------------------------------------------------------------
void zet_minuut(byte data1) { // set minute
  instelroutine(1, decToBcd(data1));
}
//------------------------------------------------------------------------
void zet_seconde(byte data1) { // set second
  instelroutine(0, decToBcd(data1));
}
//------------------------------------------------------------------
void TijdUitlezen() {
  second = lees_seconde();
  minute = lees_minuut();
  hour = lees_uur();
  weekday = lees_dagweek();  //0-6 -> sunday - Saturday
  monthDay = lees_datum();
  month = lees_maand();
  year = lees_jaar();
  //Serial.println(month);
  Sjaar = year;
  Factor(minute, false);
  c = EH;
  d = TT;
  // uur uitlezen///
  Factor(hour, false);
  e = EH;
  f = TT;
  // dag uitlezen///
  Factor(monthDay, false);
  g = EH;
  h = TT;
  // maand uitlezen///
  //Serial.println(month);
  Factor(month, false);
  i = EH;
  j = TT;
  // jaar uitlezen///
  Factor(year, false);
  k = EH;
  m = TT;
  //Serial.println(second);
}

//---------------------------------------------------------------

void Schrikkeljaar() {// leapyear

  Sjaar %= 4;  // %= is remainder --------

  if (Sjaar == 0) {
    Schrikkel = 1;
    Lentepunt = 81;
    Dagen = 366;
  } else {
    Schrikkel = 0;
    Lentepunt = 80;
    Dagen = 365;
  }
}
// --------------------------------------------------------------------


void showDirection(Rotary& rt) {
  //Serial.println(rt.getDirection());

  counter = rt.getDirection();
  if (counter == 255)
  {
    counter = -1;
  }
  else
  {
    counter = 1;
  }
  encoderPos += counter;
  //Serial.println(encoderPos);
}


//--------------------------------------------------------
void pressed(Button2& btn) {
  //Serial.print("pressed ");
  buttonPressed = 1;
  //Serial.println(buttonPressed);
}
void released(Button2& btn) {
  // Serial.print("released: ");
  buttonPressed = 0;
  //Serial.println(buttonPressed);
}

//---------------------------------------------
void readtime() {
  second = lees_seconde();// read seconds
  // seconde uitlezen///
  Factor(second, false);
  a = EH;// seconds unity ( when seconds is 25, a = 5)
  b = TT;// seconds tens ( when seconds is 25, b = 2)

  if (second == 0) {
    minute = lees_minuut();// read minutes
    // minuut uitlezen///
    Factor(minute, false);
    c = EH;// minutes unity ( when minutes is 25, c = 5)
    d = TT;// minutes tens ( when minutes is 25, d = 2)
  }

  if (minute == 0 & second == 0) {
    hour = lees_uur();// read hour
    // uur uitlezen///
    Factor(hour, false);
    e = EH;// hour unity ( when hour is 12, e = 2)
    f = TT;// hour tens ( when hour is 12, f = 1)
  }

  if (hour == 0 & second == 0) {
    weekday = lees_dagweek();  //0-6 -> sunday - Saturday
    monthDay = lees_datum();// read day of month
    month = lees_maand();// read month
    year = lees_jaar();// read year

    // dag uitlezen///
    Factor(monthDay, false);
    g = EH;// day unity ( when day is 15, g = 5)
    h = TT;// day tens ( when day is 15, h = 1)
    // maand uitlezen///
    Factor(month, false);
    i = EH;// month unity ( when month is 07, i = 7)
    j = TT;// month tens ( when month is 07, j = 0)
    Factor(year, false);
    k = EH;// year unity ( when year is 17, k = 7)
    m = TT;// year tens ( when year is 17, m = 1)
  }
}


//---------------------------------------------------------
void loop() {

  if (secondsInterrupt == 1) {  // **** when ISR vlag = 1  ****
    readtime();                 //  subroutine reads every second the time
    secondsInterrupt = 0;       // put ISR flag back to '0'
  }

  test1 = digitalRead(SW1);// read the switches
  test2 = digitalRead(SW2);
  test3 = digitalRead(SW3);
  test4 = digitalRead(SW4);
  Test = test4 * 8;
  Test += test3 * 4;
  Test += test2 * 2;
  Test += test1;
  //Serial.println(Test);

  switch (Test) {
    case 0:  // 0000
      Tijd();// time and date
      break;
    case 1:// 0001
      Dagnr_LEDMaan();// amount of days since januari first , displa2: the age of the moon in days
      break;  // moonage = 0 >> new moon, moonage = 15 >> full moon
    case 2:// 0010
      DagSEC_MinutenJaar();// amount of seconds since 0 hour (max = 86400), displa2: amount of minutes since januari first (max = 527040)
      break;
    case 3:// 0011
      Zon_op_onder();// sun rise and displa2: sun set
      break;// value depends on latitude and longitude.
    case 4:// 0100
      HzonNU();// on display1 the zenith or elevation of the sun at noon, on display2 the actual elevation, it will be zero at sun set or sunrise
      break; // value depends on latitude
    case 5:// 0101
      SchStand(); // display1 tilt of the earth, display2 the time (in hours) between sun rise and sun set
      break;  // tilt = 23.5 start of summer and -23.5 start of winter, and zero start of spring and autumn
    case 6:// 0110
      Luchtdruk();// air pressure in mBar, and display2: temperature in celsius
      break;
    case 7:// 0111
      Adjust();// adjust the time and date
      break;
    case 8:// 1000
      Digitaal();
      break;
    case 9:// 1001        Days Of Week, displa2 : weeknumber
      Dow();
      break;
    case 10:// 1010
      AzimuthEarth_Moon();
      break;
    case 11:// 1011
      Equation_MoonAngle();
      break;
    case 12:// 1100
      SunSetRiseHorizon();
      break;
    case 13:// 1101
      Lat_Longitude();
      break;
    case 14:// 1110
      Zuid_MaanPercentage();
      break;
    case 15:// 1111
      Nachtmode();// night mode: between 0 and 8 the display is blank
      break;
    default:
      Tijd();
      break;
  }
  //============ every 3 seconds read air pressure sensor ===================
  currentMillis = millis();
  if (currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    SensorLDuitlezen();// read sensor
    //Serial.println(pressure);
  }
}
//---------------------------------------------------------
void AzimuthEarth_Moon() { // display1 is azimuth earth, display2 is azimuth moon
  float MaanUur;// position on the horizon
  float MaanOud;// 0 = north   90 = east   180 = south  270 = west
  float Stijd0;
  float LGcorr;
  float AZ;
  float TVE;

  LGcorr = LengteGraad(LG);
  AZ = TotaalDagen(month);
  AZ = AZ + monthDay;
  TVE = Tijdvereffening(AZ);

  Stijd0 = timevalue(0);
  Stijd1 = (Stijd0 * 24) - WT;
  Stijd1 = Stijd1 - LGcorr - TVE;
  Stijd1 = Stijd1 * 15;
  if (Stijd1 < 0) {
    Stijd1 = 360 + Stijd1;
  }
   //Serial.println(Stijd1);


  MaanUur = MoonOld();
  MaanUur = (MaanUur / 29.5305885) * 360;
  MaanUur = 360 - MaanUur;
  Maanq = MaanUur + Stijd1;
  if (Maanq > 360) {
    Maanq = Maanq - 360;
  }
  if (Maanq < 0) {
    Maanq = 360 + Maanq;
  }
  Factor(Stijd1 * 100, true);
  display1(199, Data1[TDT], Data1[DT],  Data1[HT] - 1,  Data1[TT],  Data1[EH]);

  Factor(Maanq * 100, true);

  display2(87, Data1[TDT], Data1[DT],  Data1[HT] - 1,  Data1[TT],  Data1[EH]);

  // when you put - 1 after "]", the display dot will be lit
}
//---------------------------------------------------------
void  Equation_MoonAngle() { // display1: equation of time
  float AZ;//                   display2 Angle between earth and moon
  float TVE;// if angle = 0 it is new moon, if angle = 90, first quarter
  byte teken;// if angle = 180, it is full moon, if angle = 270, it is last quarter
  float Hoek;
  AZ = TotaalDagen(month);
  AZ = AZ + monthDay;
  TVE = Tijdvereffening(AZ);

  if (TVE < 0) {
    TVE = abs(TVE);
    teken = 127;
  }
  else
  {
    teken = 255;
  }
  TVE = TVE * 60;
  // Serial.println(TVE,7);

  Factor(TVE * 100, true);
  display1(15, teken, Data1[DT],  Data1[HT] - 1,  Data1[TT],  Data1[EH]);


  Hoek = Stijd1 - Maanq;
  if (Hoek < 0) {
    Hoek = Hoek + 360;
  }
  Factor(Hoek * 100, false);
  display2(19, Data1[TDT], Data1[DT],  Data1[HT] - 1,  Data1[TT],  Data1[EH]);

  // when you put - 1 after "]", the display dot will be lit
}
//---------------------------------------------------------
void SunSetRiseHorizon() { //upper display is sun rise at the moment the sun is 1 degree below the horizon
        // it will be 90 degrees on start of spring and autumn
  float AZ;//lower display is sun set at the moment the sun is 1 degree below the horizon
        // it will be 270 degrees on start of spring and autumn
  float Decc;
  float ZZ;
  float ZZO;
  float ZZN;
  float TVE;
  float Lgt;
  Schrikkeljaar();
  AZ = TotaalDagen(month);
  AZ = AZ - Lentepunt + monthDay;
  Decc = Declinatie(AZ, Dagen);
  ZonOpOnder(Decc, BG);  // BG is latitude (51.9)
  ZZO = ZonOp * 15;
  ZZN =  ZonOnder * 15;

  Factor(ZZO * 100, false);

  display1(199, Data1[TDT], Data1[DT],  Data1[HT] - 1,  Data1[TT],  Data1[EH]);

  Factor(ZZN * 100, false);
  display2(87, Data1[TDT], Data1[DT],  Data1[HT] - 1,  Data1[TT],  Data1[EH]);

  // when you put - 1 after "]", the display dot will be lit
}
//---------------------------------------------------------
void Lat_Longitude() { // upper display is latitude, lower display is longitude

  Factor(BG * 10, false);

  display1(7, 255, Data1[DT],  Data1[HT] ,  Data1[TT] - 1,  Data1[EH]);

  Factor(LG * 10, false);
  display2(143, 255, Data1[DT],  Data1[HT] ,  Data1[TT] - 1,  Data1[EH]);

  // when you put - 1 after "]", the display dot will be lit
}
//---------------------------------------------------------
void Zuid_MaanPercentage() { // upper display is the time when the sun is at south position (and its max elevation)
  float LGcorr; // lower display is the moon percentage of visible light (0% = new moon, 100% is full moon, 50% is first or last quarter
  float AZ;
  float TVE;
  float Zuiden;
  float MaanUur;
  float Mtest;
  LGcorr = LengteGraad(LG);
  AZ = TotaalDagen(month);
  AZ = AZ + monthDay;
  TVE = Tijdvereffening(AZ);
  Zuiden = 12 + WT + LGcorr + TVE;

  Factor(Zuiden * 100, true);

  display1(131, 255, Data1[DT],  Data1[HT] - 1 ,  Data1[TT] ,  Data1[EH]);

  MaanUur = MoonOld();
  Mtest = MaanUur;
  //Serial.println(MaanUur);
  if (MaanUur > 14.765) {
    Mtest = (14.765 - (MaanUur - 14.765));

  }

  MaanUur = (Mtest / 14.765) * 100;
  Factor(MaanUur * 10, false);
  display2(91, 255, Data1[DT],  Data1[HT] ,  Data1[TT] - 1 ,  Data1[EH]);

  // when you put - 1 after "]", the display dot will be lit
}
//---------------------------------------------------------
void Dow() {// Day of Week
  int DR;
  DR = TotaalDagen(month) + monthDay;
  DR = (DR / 7) + 1;

  Factor(DR, false);
  display1(67, 71, 131, 255, 255, Data1[weekday]);
  display2(131, 87, 95, 255, Data1[TT], Data1[EH]);
}
//---------------------------------------------------------
void Nachtmode() { // night mode: between 0 and 8 the displays are blank
  if (hour >= 0 & hour < 8) {
    displayuit();// display off
  } else {
    Tijd();// time and date
  }
}
//---------------------------------------------------------
void Digitaal() {// semi digital
  int test;
  test = a % 2;
  if (test == 0) {
    display1(Data1[f], Data1[e] - 1, Data1[d], Data1[c], Data1[b], Data1[a]);
    display2(Digit1[f], Digit1[e] - 1, Digit1[d], Digit1[c], Digit1[b], Digit1[a]);
  }
  else
  {
    display1(Data1[f], Data1[e], Data1[d], Data1[c] - 1, Data1[b], Data1[a]);
    display2(Digit1[f], Digit1[e], Digit1[d], Digit1[c] - 1, Digit1[b], Digit1[a]);
  }
}
// when you put - 1 after "]", the display dot will be lit
//-----------------------------------------------------------
void SensorLDuitlezen() {// read sensor
  bmx280.measure();
  while (bmx280.hasValue())
  {
    temperatuur = bmx280.getTemperature();
    pressure = bmx280.getPressure();
  }
}
//-----------------------------------------------------------
void Luchtdruk() {// air pressure

  //Serial.println(pres);
  Factor(pressure / 10, false);// air pressure in mBar
  display1(143, Data1[TDT], Data1[DT], Data1[HT], Data1[TT] - 1, Data1[EH]);

  Factor(temperatuur * 100, false);// temperature in celsius
  display2(15, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
}
// when you put - 1 after "]", the display dot will be lit
//-----------------------------------------------------------
void Adjust() {
  rt.loop();
  bt.loop();
  if (encoderPos < 0) {
    encoderPos = 0;

  }
  //Serial.println(encoderPos);
  if (encoderPos > 10) {
    encoderPos = 10;
  }
  switch (encoderPos) {

    case 0:
      DisplayAdjust();
      break;
    case 1:
      DisplayAdjust();
      Displaykeuze1(127);

      if (encoderPos == 1 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustUUR();
      }
      break;
    case 2:
      DisplayAdjust();
      DisplaykeuzeWT(127);
      if (encoderPos == 2 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustWT();
      }
      break;
    case 3:
      DisplayAdjust();
      Displaykeuze2(127);
      if (encoderPos == 3 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustMIN();
      }
      break;
    case 4:
      DisplayAdjust();
      Displaykeuze3(127);
      if (encoderPos == 4 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustDAG();
      }
      break;
    case 5:
      DisplayAdjust();
      Displaykeuze5(127);
      if (encoderPos == 5 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustMND();
      }
      break;
    case 6:
      DisplayAdjust();
      Displaykeuze6(127);
      if (encoderPos == 6 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustYR();
      }
      break;
    case 7:
      DisplayAdjust();
      Displaykeuze7(127);
      if (encoderPos == 7 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustDOW();
      }
      break;
    case 8:
      DisplayAdjust();
      AR = AgingRead();
      Displaykeuze8(AR);
      if (encoderPos == 8 && buttonPressed == 1) {
        encoderPos = 0;

        buttonPressed = 0;
        AdjustAging();
      }
      break;
    case 9:
      DisplayAdjust();
      Displaykeuze9(BG, 7);
      if (encoderPos == 9 && buttonPressed == 1) {
        encoderPos = 0;
        buttonPressed = 0;
        Adjustlatitude();
      }
      break;
    case 10:
      DisplayAdjust();
      Displaykeuze9(LG, 143);
      if (encoderPos == 10 && buttonPressed == 1) {
        encoderPos = 0;
        buttonPressed = 0;
        Adjustlongitude();
      }
      break;
  }
}
//-------------------------------------------------------------
void DisplayAdjust() {
  display1(17, 67, 195, 131, 37, 15);
}

//-------------------------------------------------------------
void AdjustUUR() {// adjust hour
  int exx = 0;
  int cc;
  delay(100);
  cc = 109;
  Displaykeuze1(cc);
  encoderPos = 0;
  buttonPressed = 0;

  while (exx == 0) {
    rt.loop();
    bt.loop();
    //Serial.println(cc);

    Displaykeuze1(cc);
    if (encoderPos == 0) {
      Factor(hour, false);
      e = EH;
      f = TT;
      cc = 127;
      Displaykeuze1(cc);

    }
    //-----------------

    if (encoderPos > 1) {
      hour = hour + 1;
      if (hour == 24) {
        hour = 0;
      }
      Factor(hour, false);
      e = EH;
      f = TT;
      cc = 253;


      //Serial.println(cc);


      encoderPos = 1;
    }
    // --------------------------
    if (encoderPos < -1) {
      hour = hour - 1;
      if (hour < 0) {
        hour = 23;
      }
      Factor(hour, false);
      e = EH;
      f = TT;
      cc = 239;


      encoderPos = -1;
    }

    if (buttonPressed == 1 & encoderPos != 0) {
      zet_uur(hour);
      cc = 237;
      Displaykeuze1(cc);
      buttonPressed = 0;
      encoderPos = 0;
      exx = 1;
    }

  }

}

// ------------------------------------------------------
void Displaykeuze1(int x) {
  display2(199, 199, 95, x, Data1[f], Data1[e]);
}
//-------------------------------------------------------------
void AdjustWT() {// timesaving >> 0 = wintertime, 1 = summertime
  int cc;
  int exx = 0;
  DisplaykeuzeWT(109);
  encoderPos = 0;
  buttonPressed = 0;
  while (exx == 0) {
    rt.loop();
    bt.loop();

    DisplaykeuzeWT(cc);
    //Serial.println (memC);
    if (encoderPos == 0) {
      DisplaykeuzeWT(127);
      cc = 127;
    }
    //-----------------


    if (encoderPos > 1) {
      WT = 1;
      DisplaykeuzeWT(253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      WT = 0;
      DisplaykeuzeWT(239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      DisplaykeuzeWT(109);
      cc = 109;
      encoderPos = 0;
      buttonPressed = 0;
      exx = 1;
    }

  }
}


// ------------------------------------------------------
void DisplaykeuzeWT(int x) {
  display2(15, 15, 255, x, 255, Data1[WT]);
}
//-----------------------------------------------------------
void AdjustMIN() {// adjust minute
  int exx = 0;
  int cc;
  encoderPos = 0;

  Displaykeuze2(109);


  while (exx == 0) {
    Displaykeuze2(cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze2(127);
      cc = 127;
    }
    //-----------------



    if (encoderPos > 1) {
      minute = minute + 1;
      if (minute == 60) {
        minute = 0;
      }
      Factor(minute, false);
      c = EH;
      d = TT;
      Displaykeuze2(253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      minute = minute - 1;
      if (minute < 0) {
        minute = 59;
      }
      Factor(minute, false);
      c = EH;
      d = TT;
      Displaykeuze2(239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      zet_seconde(0);
      zet_minuut(minute);

      Displaykeuze2(109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }

  }
}

// ------------------------------------------------------
void Displaykeuze2(int x) {
  display2(145, 243, 87, x, Data1[d], Data1[c]);
}

//----------------------------------------------------------
void AdjustDAG() {// adjust day
  int exx = 0;
  int cc;
  encoderPos = 0;

  Displaykeuze3(109);


  while (exx == 0) {
    Displaykeuze3(cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze3(127);
      cc = 127;
    }
    //-----------------



    if (encoderPos > 1) {
      monthDay = monthDay + 1;
      if (monthDay == 32) {
        monthDay = 1;
      }
      Factor(monthDay, false);
      g = EH;
      h = TT;
      Displaykeuze3(253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      monthDay = monthDay - 1;
      if (monthDay == 0) {
        monthDay = 31;
      }
      Factor(monthDay, false);
      g = EH;
      h = TT;
      Displaykeuze3(239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      zet_datum(monthDay);
      Displaykeuze3(109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }

  }
}
// ------------------------------------------------------
void Displaykeuze3(int x) {
  display2(67, 17, 35, x, Data1[h], Data1[g]);
}
//-------------------------------------------------------
void AdjustMND() {// Adjust month
  int exx = 0;
  int cc;

  encoderPos = 0;

  Displaykeuze5(109);


  while (exx == 0) {
    Displaykeuze5(cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze5(127);
      cc = 127;
    }
    //-----------------



    if (encoderPos > 1) {
      month = month + 1;
      if (month == 13) {
        month = 1;
      }
      Factor(month, false);
      i = EH;
      j = TT;
      Displaykeuze5(253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      month = month - 1;
      if (month == 0) {
        month = 12;
      }

      Factor(month, false);
      i = EH;
      j = TT;
      Displaykeuze5(239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      zet_maand(month);
      Displaykeuze5(109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }

  }
}
// ------------------------------------------------------
void Displaykeuze5(int x) {
  display2(145, 87, 67, x, Data1[j], Data1[i]);
}
//------------------------------------------------------
void AdjustYR() {// Adjust year
  int exx = 0;
  int cc;
  encoderPos = 0;


  Displaykeuze6(109);

  if (year > 2000) {
    year = year - 2000;
  }

  while (exx == 0) {
    Displaykeuze6(cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze6(127);
      cc = 127;
    }
    //-----------------


    if (encoderPos > 1) {
      year = year + 1;
      if (year > 99) {
        year = 0;
      }
      Factor(year, false);
      k = EH;
      m = TT;
      Displaykeuze6(253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      year = year - 1;
      if (year < 0) {
        year = 99;
      }
      Factor(year, false);
      k = EH;
      m = TT;
      Displaykeuze6(239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      zet_jaar(year);
      Displaykeuze6(109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }
  }
}
// ------------------------------------------------------
void Displaykeuze6(int x) {
  display2(35, 95, 255, x, Data1[m], Data1[k]);
}

//------------------------------------------------------
void AdjustDOW() {// Day Of Week
  int exx = 0;
  int cc;
  encoderPos = 0;


  Displaykeuze7(109);



  while (exx == 0) {
    Displaykeuze7(cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze7(127);
      cc = 127;
    }
    //-----------------


    if (encoderPos > 1) {
      weekday = weekday + 1;
      if (weekday > 7) {
        weekday = 1;
      }

      Displaykeuze7(253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      weekday = weekday - 1;
      if (weekday < 1) {
        weekday = 7;
      }

      Displaykeuze7(239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      zet_dagweek(weekday);
      Displaykeuze7(109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }
  }
}
// ------------------------------------------------------
void Displaykeuze7(int x) {
  display2(67, 71, 131, 255, x, Data1[weekday]);
}
//------------------------------------------------------
void AdjustAging() {// routine to fine adjust the RTC
  int exx = 0;
  encoderPos = 0;


  Displaykeuze8(AR);

  AR = AgingRead();

  while (exx == 0) {
    Displaykeuze8(AR);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze8(AR);

    }
    //-----------------


    if (encoderPos > 1) {
      AR = AR + 1;
      if (AR > 127) {
        AR = 0;
      }

      Displaykeuze8(AR);
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      AR = AR - 1;
      if (AR < -128) {
        AR = 0;
      }

      Displaykeuze8(AR);
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {
      Agingadjust(AR);
      Displaykeuze8(AR);
      encoderPos = 0;

      exx = 1;
    }

  }
}
// ------------------------------------------------------
void Displaykeuze8(byte ARR) {
  int RR;
  // aging register of RTC uses two's complement, a negative number is from 128 to 255
  RR = ARR;

  if (RR >= 0 & RR < 128) {
    Factor(RR, false);
    display2(17, 255, 255, Data1[HT], Data1[TT], Data1[EH]);
  }
  else
  {
    RR = RR - 256;
    RR = abs(RR);
    Factor(RR, false);
    display2(17, 255, 127, Data1[HT], Data1[TT], Data1[EH]);
  }

}
//------------------------------------------------------
void Adjustlatitude() {
  int exx = 0;
  int cc;
  encoderPos = 0;

  Displaykeuze9(BG, 109);



  while (exx == 0) {
    Displaykeuze9(BG, cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze9(BG, 127);
      cc = 127;
    }
    //-----------------


    if (encoderPos > 1) {
      BG = BG + 0.1;
      if (BG > 90) {
        BG = 0;

      }

      Displaykeuze9(BG, 253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      BG = BG - 0.1;
      if (BG < -90) {
        BG = 0;
      }

      Displaykeuze9(BG, 239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {

      Displaykeuze9(BG, 109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }
  }
}
// ------------------------------------------------------
void Displaykeuze9(float x, byte teken) {
  if (x < 0) {
    x = abs(x);
    Factor(x * 10, false);
    display2(teken, 255, 127, Data1[HT], Data1[TT] - 1, Data1[EH]);
  }
  else
  {
    Factor(x * 10, false);
    display2(teken, 255, 255, Data1[HT], Data1[TT] - 1, Data1[EH]);
  }
}

//------------------------------------------------------
void Adjustlongitude() {
  int exx = 0;
  int cc;
  encoderPos = 0;

  Displaykeuze9(LG, 109);



  while (exx == 0) {
    Displaykeuze9(LG, cc);
    rt.loop();
    bt.loop();


    if (encoderPos == 0) {
      Displaykeuze9(LG, 127);
      cc = 127;
    }
    //-----------------


    if (encoderPos > 1) {
      LG = LG + 0.1;
      if (LG > 180) {
        LG = 0;

      }

      Displaykeuze9(LG, 253);
      cc = 253;
      encoderPos = 1;
    }

    // --------------------------
    if (encoderPos < -1) {
      LG = LG - 0.1;
      if (LG < -180) {
        LG = 0;
      }

      Displaykeuze9(LG, 239);
      cc = 239;
      encoderPos = -1;
    }
    if (buttonPressed == 1 & encoderPos != 0) {

      Displaykeuze9(LG, 109);
      cc = 109;
      encoderPos = 0;

      exx = 1;
    }
  }
}

//-------------------------------------------------------------
void Tijd() {// time on display1  and date on display2
  int test;
  test = a % 2;
  if (test == 0) {
    display1( Data1[f],  Data1[e] - 1,  Data1[d],  Data1[c], Data1[b],  Data1[a]);
  } else {
    display1( Data1[f],  Data1[e],  Data1[d],  Data1[c] - 1,  Data1[b],  Data1[a]);
  }
  display2( Data1[h],  Data1[g] - 1,  Data1[j],  Data1[i] - 1,  Data1[m],  Data1[k]);

  // when you put - 1 after "]", the display dot will be lit
}
// ------------------------------------------------------------ -
void display1(byte D1, byte D2,  byte D3,  byte D4,  byte D5, byte D6) {
  DDP(0, D1);
  DDP(1, D2);
  DDP(2, D3);
  DDP(3, D4);
  DDP(4, D5);
  DDP(5, D6);
  DDP(12, 0);// extra line to prevent that the last digit 6 is more bright
  DDP(12, 0);// extra line to prevent that the last digit 6 is more bright
}
// ------------------------------------------------------------ -
void display2( byte D1,  byte D2,  byte D3,  byte D4,  byte D5,  byte D6) {
  DDP(6, D1);
  DDP(7, D2);
  DDP(8, D3);
  DDP(9, D4);
  DDP(10, D5);
  DDP(11, D6);
  DDP(12, 0);// extra line to prevent that the last digit 11 is more bright
  DDP(12, 0);// extra line to prevent that the last digit 11 is more bright

}
//-------------------------------------------------------------
void DDP(byte DispKeuze, byte DispData) {// the multiplexing routine

  digitalWrite(latchPin1, HIGH);
  shiftOut(dataPin1, clockPin1, MSBFIRST, DispData);
  shiftOut(dataPin1, clockPin1, MSBFIRST, DisplaySelect2[DispKeuze]);
  shiftOut(dataPin1, clockPin1, MSBFIRST, DisplaySelect1[DispKeuze]);
  digitalWrite(latchPin1, LOW);
  //delay(1);
}

//------------------------------------------------------------------
void displayuit() {// display off
  display1(255, 255, 255, 255, 255, 255);
  display2(255, 255, 255, 255, 255, 255);
}
//------------------------------------------------------------------
void Displaytest1() {
  for (int x = 0; x < 13; x++) {
    Lus(x);
  }
}
//------------------------------------------------------------------
void Displaytest2() {
  for (int x = 0; x < 13; x++) {
    Lus1(x);
  }
}
//------------------------------------------------------------------
void Displaytest3() {
  for (int x = 0; x < 10; x++) {
    Lus2(x);
  }
}
//---------------------------------------------------------
void Lus(byte tt) {
  int test[13] = { 253, 127, 239, 251, 247, 223, 191, 127, 237, 127, 237, 127, 237 };

  for (int x = 0; x < 40; x++) {
    display1(test[tt], test[tt], test[tt], test[tt], test[tt], test[tt]);
    display2(test[tt], test[tt], test[tt], test[tt], test[tt], test[tt]);
  }
}
//---------------------------------------------------------
void Lus1(byte tt) {
  int test[13] = { 71, 57, 71, 57, 71, 57, 71, 57, 71, 57, 71, 57, 71 };

  for (int x = 0; x < 40; x++) {
    display1(test[tt], test[tt], test[tt], test[tt], test[tt], test[tt]);
    display2(test[tt], test[tt], test[tt], test[tt], test[tt], test[tt]);
  }
}
//---------------------------------------------------------
void Lus2(byte tt) {
  for (int x = 0; x < 40; x++) {
    display1(Data1[tt], Data1[tt], Data1[tt], Data1[tt], Data1[tt], Data1[tt]);
    display2(Data1[tt], Data1[tt], Data1[tt], Data1[tt], Data1[tt], Data1[tt]);
  }
}

// --------------------------------------------------------------
void Factor(long Getal, bool graden) {
  long Test;
  //Serial.println(Getal);
  Test = Getal;
  HDT = Test / 100000;
  Test = Test - (HDT * 100000);

  TDT = Test / 10000;
  Test = Test - (TDT * 10000);

  DT = Test / 1000;
  Test = Test - (DT * 1000);


  HT = Test / 100;
  Test = Test - (HT * 100);

  if (graden == true) {// when true, the number will be converted into minutes (e.g. 0.9 degrees >> 0 degrees and 54 minutes)
    Test = Test * 0.6;
  }
  TT = Test / 10;
  Test = Test - (TT * 10);

  EH = Test;
}

// -------------------------------------------------------------------
int TotaalDagen(int maand) {  // dit is een functie
  int Dagen;
  int dag1 = maand - 1;
  if (Schrikkel == 1) {
    Dagen = DagtotaalS[dag1];
  } else {
    Dagen = Dagtotaal[dag1];
  }
  return Dagen;
}
//------------------------------------------------------
float timevalue(byte TVorSEC) {
  float DS;
  DS = hour;
  switch (TVorSEC) {
    case 0://TimeValue in 24 hour format(24 hour = 1)
      DS = DS + (minute / 60.0);
      DS = DS + (second / 3600.0);
      return DS = DS / 24;
      break;
    case 1://Time in Seconds
      DS = DS * 3600;
      DS = DS + (minute * 60);
      DS = DS + second;
      return DS;
      break;
  }

}
//------------------------------------------------------
void DagSEC_MinutenJaar() {
  float DS;

  DS = timevalue(1);
  Factor(DS, false);// DS is amount of seconds since midnight
  display1(109, Data1[TDT], Data1[DT], Data1[HT], Data1[TT], Data1[EH]);

  Schrikkeljaar();
  DS = TotaalDagen(month);
  DS += monthDay - 1;
  //Serial.println(DS);
  DS *= (24 * 60);
  DS += (hour * 60);
  DS += minute;

  Factor(DS, false);// DS is amount of minutes since januari first
  display2(Data1[HDT], Data1[TDT], Data1[DT], Data1[HT], Data1[TT], Data1[EH]);
}
//-----------------------------------------------------------------
float MoonOld() {
  float Uur1;
  float MOud;
  int Index;
  float AZ;
  Schrikkeljaar();
  Uur1 = timevalue(0);
  AZ = TotaalDagen(month);
  AZ = AZ + monthDay;
  AZ = AZ + Uur1;
  if (year > 2000) {
    year = year - 2000;
  }
  Index = year - 17;  // epactagetal ophalen
  while (Index > 18) {
    Index = Index - 19;
  }

  MOud = Maan[Index];

  MOud = MOud + AZ;

  while (MOud >= 29.5305885) {
    MOud -= 29.5305885;
  }
  //Serial.println(MOud);
  return MOud;
}
//-----------------------------------------------------------------
void Dagnr_LEDMaan() {// moon age in days
  float MOud;
  int DR;


  DR = TotaalDagen(month) + monthDay;
  Factor(DR, false);// DR is the amount of days since januari first
  display1(67, 87, 255, Data1[HT], Data1[TT], Data1[EH]);

  MOud = MoonOld();
  Factor(MOud * 100, false);// MOud is the age of the moon in days.
  display2(145, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
  // when you put - 1 after "]", the display dot will be lit
}
//---------------------------------------------------------
void Zon_op_onder() {// sun rise, sun set
  float AZ;
  float Decc;
  float ZZ;
  float ZZO;
  float ZZN;
  float TVE;
  float Lgt;
  Schrikkeljaar();
  AZ = TotaalDagen(month);
  AZ = AZ - Lentepunt + monthDay;
  Decc = Declinatie(AZ, Dagen);
  ZonOpOnder(Decc, BG);  // BG is latitude (51.9)
  Lgt = LengteGraad(LG); // LG is longitude (4.75)

  AZ += Lentepunt;
  TVE = Tijdvereffening(AZ);
  ZZ = WT;
  ZZ += TVE;
  ZZO = ZZ + ZonOp + Lgt;
  ZZN = ZZ + ZonOnder + Lgt;
  Factor(ZZO * 100, true); //ZZO is sunrise

  display1(25, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);

  Factor(ZZN * 100, true);//ZZN is sunset
  display2(87, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
}
// when you put - 1 after "]", the display dot will be lit
// -----------------------------------------------

double Declinatie(float dag, float Jaardag) {// tilt of the earth
  double DECL;
  DECL = (dag * 360.0 / Jaardag);
  DECL = SINUS(DECL);
  return DECL = DECL * 23.5;
}
//-------------------------------------------------
double ARCTANGENS(double x) {
  double XX;
  XX = x;
  return atan(XX) * RAD_TO_DEG;
}
//-------------------------------------------------
double ARCCOSINUS(double x) {
  double XX;
  XX = x;
  return acos(XX) * RAD_TO_DEG;
}
//-------------------------------------------------
double ARCSINUS(double x) {
  double XX;
  XX = x;
  return asin(XX) * RAD_TO_DEG;
}
//-------------------------------------------------
double TANGENS(double x) {
  double XX;
  XX = x * DEG_TO_RAD;
  return tan(XX);
}
//-------------------------------------------------
double COSINUS(double x) {
  double XX;
  XX = x * DEG_TO_RAD;
  return cos(XX);
}
//-------------------------------------------------
double SINUS(double x) {
  double XX;
  XX = x * DEG_TO_RAD;
  return sin(XX);
}

//--------------------------------------------------
void ZonOpOnder(float decl, float brg) {// sun rise, sun set
  float ZON;
  float ZonOpOn;
  float AA2 = 180.0;
  float BB2 = 15.0;
  float gr = -1.25;// when the sun is -1.25 degree below horizon, it is the time of sun rise/sun set
  ZON = (SINUS(gr) - (SINUS(brg) * (SINUS(decl)))) / (COSINUS(brg) * COSINUS(decl));
  ZonOpOn = ARCCOSINUS(ZON);
  ZonOp = (AA2 - ZonOpOn) / BB2;
  ZonOnder = (AA2 + ZonOpOn) / BB2;
  DagLengte = ZonOnder - ZonOp;
}
//-------------------------------------------------
float Tijdvereffening(float dd) {// Equation of time
  float Tijd;// dd = dagen sinds 1 januari
  float TVV;
  Schrikkeljaar();// Dagen = 365 of 366
  Tijd = (7.56 * SINUS((2 * 180 * (dd - 2.7)) / Dagen));
  Tijd = Tijd + (9.85 * (SINUS((4 * 180 * (10.5 + dd)) / Dagen)));
  TVV = (Tijd / 60);// tijd in uren
  return TVV;
}
// ------------------------------------------------
void HzonNU() {// present elevation of the sun
  float Tijd1;
  float AZ;
  float Decc;
  float TVE;
  float Tijd2;
  float HGTE;
  float Hoogte;
  float ZZ;
  float Lgt;
  float BB;
  float HGT;
  Tijd1 = timevalue(0) * 24;
  //Serial.println(Tijd1,6);
  Lgt = LengteGraad(LG);
  Tijd1 = Tijd1 - WT - Lgt;
  Schrikkeljaar();
  AZ = TotaalDagen(month);
  AZ = AZ - Lentepunt + monthDay;
  Decc = Declinatie(AZ, Dagen);
  AZ = AZ + Lentepunt;
  TVE = Tijdvereffening(AZ);
  Tijd1 = Tijd1 - TVE;
  Tijd2 = (Tijd1 * 15.0) - 180.0;
  HGTE = (SINUS(BG) * SINUS(Decc)) + (COSINUS(BG) * COSINUS(Decc) * COSINUS(Tijd2));
  Hoogte = ARCSINUS(HGTE);
  // Serial.print("hoogte: ");
  //Serial.println(Hoogte);
  ZZ = Hoogte;
  ZZ = abs(ZZ);  // abs = absoluut
  HGT = (SINUS(BG) * SINUS(Decc)) + (COSINUS(BG) * COSINUS(Decc));
  BB = ARCSINUS(HGT);

  Factor(BB * 100, true);
  display1(19, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);

  Factor(ZZ * 100, true);
  if (Hoogte >= 0) {
    display2(145, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
  } else {
    display2(145, 127, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
  }
}
// when you put - 1 after "]", the display dot will be lit
//----------------------------------------------------------------
void SchStand() {// tilt of the earth
  float BB;
  float BB2;
  float AZ;

  Schrikkeljaar();
  AZ = TotaalDagen(month);
  //AZ = 345;
  AZ = (AZ - Lentepunt) + monthDay;
  BB = Declinatie(AZ, Dagen);
  BB2 = BB;

  BB2 = abs(BB2);  // abs = absoluut


  Factor(BB2 * 100, true);
  if (BB >= 0) {
    display1(91, 255, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
  } else {
    display1(91, 127, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
  }

  Factor(DagLengte * 100, true);
  display2(67, 143, Data1[DT], Data1[HT] - 1, Data1[TT], Data1[EH]);
}
// when you put - 1 after "]", the display dot will be lit
//-----------------------------------------------------
float LengteGraad(float Lg1) {// longitude
  float Lgt;

  while (Lg1 >= 15) {
    Lg1 -= 15;
  }
  Lgt = (15 - Lg1);
  Lgt = Lgt / 15;
  return Lgt;
}

Credits

jg53fn55

jg53fn55

0 projects • 4 followers
I am retired.

Comments