#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 ClockArray[60];
int ZeroesTable[11]= {4, 10, 11, 14, 20, 21, 24, 34, 35, 44, 54};
int StartTime, DelayTime, EndTime;
//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 (){
int ZeroLocation;
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);
}
ClockArray[0]= 2;
for( int i = 0; i < 12; i++){
ClockArray[(i+10)] = 2;
}
for( int j = 0; j < 12; j++){
ZeroLocation = ZeroesTable[j];
ClockArray[ZeroLocation] ;
}
ClockArray[22] = 0;
ClockArray[23] = 0;
ClockArray[25] = 0;
ClockArray[26] = 1;
ClockArray[27] = 1;
ClockArray[28] = 0;
ClockArray[30] = 0;
ClockArray[31] = 1;
ClockArray[32] = 1;
ClockArray[33] = 0;
ClockArray[36] = 0;
ClockArray[37] = 1;
ClockArray[38] = 0;
ClockArray[40] = 0;
ClockArray[41] = 0;
ClockArray[42] = 1;
ClockArray[43] = 1;
ClockArray[45] = 0;
ClockArray[46] = 0;
ClockArray[47] = 0;
ClockArray[48] = 0;
ClockArray[50] = 1;
ClockArray[51] = 0;
ClockArray[52] = 0;
ClockArray[53] = 0;
ClockArray[55] = 1;
ClockArray[56] = 0;
ClockArray[57] = 0;
ClockArray[57] = 0;
pinMode(A0, 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();
}
ClockArray[1] = ValveArray[2][2];
ClockArray[2] = ValveArray[2][1];
ClockArray[3] = ValveArray[2][0];
ClockArray[5] = ValveArray[3][3];
ClockArray[6] = ValveArray[3][2];
ClockArray[7] = ValveArray[3][1];
ClockArray[8] = ValveArray[3][0];
ClockArray[12] = ValveArray[0][1];
ClockArray[13] = ValveArray[0][0];
ClockArray[15] = ValveArray[1][3];
ClockArray[16] = ValveArray[1][2];
ClockArray[17] = ValveArray[1][1];
ClockArray[18] = ValveArray[1][0];
analogWrite(19, 255);
for (int messageloop = 0; messageloop < 61; messageloop++){
Serial.print("Loop:");
Serial.println(messageloop);
Serial.print("Message bit");
Serial.println(ClockArray[messageloop]);
StartTime = millis();
Serial.print("Start Time ");
Serial.println(StartTime);
analogWrite(A0, 130);
if (ClockArray[messageloop] ==2){
delay(800);
}
else if (ClockArray[messageloop] ==1){
delay(500);
}
else{
delay(200);
}
analogWrite(A0, 255);
EndTime = millis();
Serial.print("End time ");
Serial.println(EndTime);
DelayTime = 1000 - (EndTime - StartTime);
Serial.print("Delay Time ");
Serial.println(DelayTime);
if (DelayTime < 1000){
Serial.println(DelayTime);
delay(DelayTime);
}
else {
Serial.println("Timer Value Expired");
}
}
}
//Serial.print();