#include <math.h>
#include <RTClib.h>

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {
  "Sunday",
  "Monday",
  "Tuesday",
  "Wednesday",
  "Thursday",
  "Friday",
  "Saturday"
};

int ClockHours;
int ClockMinutes;
int BCDConversion[4];
int BCDHoursTens[4];
int BCDhoursOnes[4];
int ClockHoursTens, ClockHoursOnes, ClockMinutesTens, ClockMinutesOnes, ClockParameter;
int x, y;
int pinStart;
int ValveArray[4] [4];
int pins[4] ={1, 5, 9, 13};
int Writeloop[4] ={2, 4, 4, 4};
//int DecimalInput;



/* This Function builds a 2D array. 
*  Each row is a representation of a 4 bit number
*  Column 0 is LSB and Column 3 is MSB
*  Rows 0 to 3 are HoursTens, HoursOnes, MinutesTens,  MinutesOnes respectively
*1000
*1110
*0010
*1010
* represents 17:45
*/
void BCDConvert(int DecimalInput, int index){
int QR, MR;
// Serial.print("Decimal, row  ");
// Serial.print(DecimalInput);
// Serial.print(",");
// Serial.print(index);
//Serial.print();
//Serial.print();

for (int i = 0; i < 4; i++){
  QR = round(DecimalInput/2);
  MR = DecimalInput % 2;
  if (MR >=1){
    ValveArray[index][i] = 1;
  }
  else{
    ValveArray[index][i] = 0;
  }
  DecimalInput = QR;
  //Serial.print(ValveArray[index][i]);
 }
return;
}

void setup (){
  Serial.begin(9600);

  // SETUP RTC MODULE
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1);
  }
//Pins 1 and 2 to be used for HoursTens 0, 1 or 2
  for (int i = 1; i < 3; i++){
    
     pinMode(i, OUTPUT);
}
//Pins 5 to 16 used four HoursOnes, MinutesTens and MinutesOnes 
  for (int i = 5; i < 17; i++){
    pinMode(i, OUTPUT);
  }
}

void loop (){
  DateTime now = rtc.now();
  Serial.print("Date & Time: ");
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.day(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);

// Time is read from the RTC, Ultimately these values will be modified through sych
// with Atomic Clock Signal, plus timezone adjustment
// ClockMinutesTens

ClockHours = now.hour();
  
if (ClockHours >= 10) {
      ClockHoursTens = ClockHours/10;
      ClockHoursOnes = ClockHours - (ClockHoursTens * 10);
}
else if (ClockHours < 10) {
      ClockHoursTens = 0;
      ClockHoursOnes = ClockHours;
}
else {
  // do Thing C
}
  
  
ClockMinutes = now.minute();
  if (ClockMinutes >= 10){
      ClockMinutesTens = ClockMinutes/10;
      ClockMinutesOnes = ClockMinutes - (ClockMinutesTens * 10);
  }
  else{
      ClockMinutesTens = 0;
      ClockMinutesOnes = ClockMinutes;
      }
 
// This loop builds the Valve Array which will be mapped to the decoder


 for (int i = 0; i < 4; i++){

    switch (i){
      case 0:
        ClockParameter = ClockHoursTens;
      break;

      case 1:
        ClockParameter = ClockHoursOnes;
      break;

      case 2:
        ClockParameter = ClockMinutesTens;
      break;

      case 3:
        ClockParameter = ClockMinutesOnes;
      break;
  }
  
  // Serial.print("Send to BCD Converter decimal Value");
  // Serial.print(" ");
  // Serial.print(ClockParameter);
  // Serial.print(" ");
  // Serial.print("Row Number");
  // Serial.print(" ");
  // Serial.print(i);
  // Serial.print("");
  // Serial.println();


  BCDConvert(ClockParameter,i);

  //Serial.println();
  }
  

// Serial.print("writing outputs"); 
// Serial.println();

//Writing Time
for (int block = 0; block < 4; block++){
  pinStart = pins[block];
    for (int cols = 0  ; cols < Writeloop[block]; cols++){
            
      if (ValveArray[block] [cols] == 1){
        digitalWrite((pinStart + cols), HIGH);
             
      }
      else{
        digitalWrite((pinStart + cols), LOW);
      }
          Serial.print((pinStart + cols));
          Serial.print("  ");
          Serial.print (ValveArray[block] [cols]);
          Serial.println();
    }
    Serial.println();
}

Serial.print("ValveArray");
Serial.println();
for (int d=0; d < 4; d++){
   Serial.print(ValveArray[d][0]);
   Serial.print(ValveArray[d][1]);
   Serial.print(ValveArray[d][2]);
   Serial.print(ValveArray[d][3]);
Serial.println();
}



}
//Serial.print();






 

nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
GND5VSDASCLSQWRTCDS1307+
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW