// ################################################################################## // ######## Program to run the LEDs of the old Clock (Basketball field) ############# // ######## Written by Dr. Mohammad Saadeh on 2/11/2021 ############# // ################################################################################## // Relays are active LOW #include #include DS3231 clock; RTCDateTime dt; unsigned long previous_time; unsigned long current_time; unsigned long multiplier = 1000;// for the micro seconds unsigned long MINUTE_DELAY = 60000; // 60K ms which is equivalent to 60 sec(1 min) delay //int minute = Clock_Time % 10; // e.g. 9 //int minute_ten = ((Clock_Time - minute) / 10) % 10; // e.g. 4 //int hour = ((Clock_Time - 10 * minute_ten - minute) / 100) % 10; // e.g. 2 //int hour_ten = (Clock_Time - 100 * hour - 10 * minute_ten - minute) / 1000; // e.g. 1 int hr; // integer for hours int hr_one; int hr_ten; int mint; // integer for minutes int mint_one; int mint_ten; // {a,b,c,d,e,f,g,af,fg} //int S0[] = {1, 1, 1, 1, 1, 1, 0, 1, 1}; // array to change light sequence to 0 //int S1[] = {0, 1, 1, 0, 0, 0, 0, 0, 0}; // array to change light sequence to 1 //int S2[] = {1, 1, 0, 1, 1, 0, 1, 1, 1}; // array to change light sequence to 2 //int S3[] = {1, 1, 1, 1, 0, 0, 1, 1, 1}; // array to change light sequence to 3 //int S4[] = {0, 1, 1, 0, 0, 1, 1, 1, 1}; // array to change light sequence to 4 //int S5[] = {1, 0, 1, 1, 0, 1, 1, 1, 1}; // array to change light sequence to 5 //int S6[] = {1, 0, 1, 1, 1, 1, 1, 1, 1}; // array to change light sequence to 6 //int S7[] = {1, 1, 1, 0, 0, 0, 0, 1, 0}; // array to change light sequence to 7 //int S8[] = {1, 1, 1, 1, 1, 1, 1, 1, 1}; // array to change light sequence to 8 //int S9[] = {1, 1, 1, 0, 0, 1, 1, 1, 1}; // array to change light sequence to 9 int S0[] = {0, 0, 0, 0, 0, 0, 1, 0, 0}; // array to change light sequence to 0 int S1[] = {1, 0, 0, 1, 1, 1, 1, 1, 1}; // array to change light sequence to 1 int S2[] = {0, 0, 1, 0, 0, 1, 0, 0, 0}; // array to change light sequence to 2 int S3[] = {0, 0, 0, 0, 1, 1, 0, 0, 0}; // array to change light sequence to 3 int S4[] = {1, 0, 0, 1, 1, 0, 0, 0, 0}; // array to change light sequence to 4 int S5[] = {0, 1, 0, 0, 1, 0, 0, 0, 0}; // array to change light sequence to 5 int S6[] = {0, 1, 0, 0, 0, 0, 0, 0, 0}; // array to change light sequence to 6 int S7[] = {0, 0, 0, 1, 1, 1, 1, 0, 1}; // array to change light sequence to 7 int S8[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // array to change light sequence to 8 int S9[] = {0, 0, 0, 1, 1, 0, 0, 0, 0}; // array to change light sequence to 9 int pin [] = {2, 3, 4, 5, 6, 7, 8, 9, 44}; // pins connected to each digit (use offset of 10 or 20 to move between digits) int bin [] = {0, 0, 0, 0}; // binary hour (4 digits to represent the hours only 1-12 (i.e. 0001 to 1010) int check_hr; int bin_hr; int digit; int digit_2 = 99; // SOME RANDOM NUMBER int digit_3 = 99; // SOME RANDOM NUMBER int digit_4 = 99; // SOME RANDOM NUMBER int start = 1; // pins 1 - 9 are dedicated to digit 1 // pins 11-19 are dedicated to digit 2 // pins 21-29 are dedicated to digit 3 // pins 31-32 are dedicated to digit 4 // pins 41-44 are dedicated to binary clock (4 bottom lights) //############################ STEPPER MOTOR PARAMETERS ############################# const int stepPin_min = 19; //PUL -Pulse const int dirPin_min = 18; //DIR -Direction const int enPin_min = 17; //ENA -Enable const int stepPin_hr = 16; //PUL -Pulse const int dirPin_hr = 15; //DIR -Direction const int enPin_hr = 14; //ENA -Enable //long gear_hr = 8;// gear ratio for the hour handle //long rotation = 6400;// number of steps for a complete rotation without the gear long steps_min = 106 ; long steps_hr = 71 ; int td = 8000;//3 msec delay between successive steps // ################################################################################ void setup() { pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(22, OUTPUT); pinMode(23, OUTPUT); pinMode(24, OUTPUT); pinMode(25, OUTPUT); pinMode(26, OUTPUT); pinMode(27, OUTPUT); pinMode(28, OUTPUT); pinMode(29, OUTPUT); pinMode(32, OUTPUT); pinMode(33, OUTPUT); pinMode(34, OUTPUT); pinMode(35, OUTPUT); pinMode(36, OUTPUT); pinMode(37, OUTPUT); pinMode(38, OUTPUT); pinMode(39, OUTPUT); pinMode(44, OUTPUT); pinMode(45, OUTPUT); pinMode(46, OUTPUT); pinMode(47, OUTPUT); pinMode(48, OUTPUT); pinMode(49, OUTPUT); pinMode(50, OUTPUT); pinMode(51, OUTPUT); pinMode(stepPin_hr, OUTPUT); pinMode(dirPin_hr, OUTPUT); pinMode(enPin_hr, OUTPUT); pinMode(stepPin_min, OUTPUT); pinMode(dirPin_min, OUTPUT); pinMode(enPin_min, OUTPUT); digitalWrite(enPin_hr, LOW); digitalWrite(dirPin_hr, LOW); digitalWrite(enPin_min, LOW); digitalWrite(dirPin_min, LOW); Serial.begin(9600); //previous_time = millis(); Serial.println("Initialize DS3231");; clock.begin(); // Set sketch compiling time clock.setDateTime(__DATE__, __TIME__); dt = clock.getDateTime(); int scnd = dt.second; int mint = dt.minute; int mint_one = mint % 10; int mint_ten = (mint - mint_one) / 10; int hr = dt.hour; if (hr > 12) { hr = hr - 12; } int hr_one = hr % 10; int hr_ten = (hr - hr_one) / 10; } // ###################################################################### // ########################## LOOP STARTS HERE ########################## // ###################################################################### void loop() { dt = clock.getDateTime();// read real time if (dt.minute != mint | start == 1) { mint = dt.minute; // update time to reset timer mint_one = mint % 10; mint_ten = (mint - mint_one) / 10; hr = dt.hour; if (hr > 12) { hr = hr - 12; } hr_one = hr % 10; hr_ten = (hr - hr_one) / 10; ////// UPDATE THE FIRST DIGIT (minutes)////// digit = mint_one; pin[0] = 2; pin[1] = 3; pin[2] = 4; pin[3] = 5; pin[4] = 6; pin[5] = 7; pin[6] = 8; pin[7] = 9; pin[8] = 44; // update pin numbers for digit 1 LIGHTS(); // call the subfunction LIGHTS which changes lights for the first three digits (minutes, minutes_ten, and hours) ////// UPDATE THE SECOND DIGIT (minutes tens)////// if (mint_ten != digit_2) // check if different and update only if there is a change { digit_2 = mint_ten;// update current minute_ten digit = mint_ten; pin[0] = 22; pin[1] = 23; pin[2] = 24; pin[3] = 25; pin[4] = 26; pin[5] = 27; pin[6] = 28; pin[7] = 29; pin[8] = 45; // update pin numbers for digit 2 LIGHTS(); // call the subfunction LIGHTS which changes lights for the first three digits (minutes, minutes_ten, and hours) } ////// UPDATE THE THIRD DIGIT (hours)////// if (hr_one != digit_3) // check if different and update only if there is a change { digit_3 = hr_one;// update current hour digit = hr_one; pin[0] = 32; pin[1] = 33; pin[2] = 34; pin[3] = 35; pin[4] = 36; pin[5] = 37; pin[6] = 38; pin[7] = 39; pin[8] = 46; // update pin numbers for digit 3 LIGHTS(); // call the subfunction LIGHTS which changes lights for the first three digits (minutes, minutes_ten, and hours) BINARY(); // call the binary clock subroutine if either hour or hour_ten changes } ////// UPDATE THE FOURTH DIGIT (hours tens)////// if (hr_ten != digit_4) // check if different and update only if there is a change { digit_4 = hr_ten;// update current hour if (hr_ten == 0) { pinMode(47, 0); // set the "a" block to LOW (turn them ON) } else if (hr_ten == 1) { pinMode(47, 1); // set the "a" block to HIGH (turn them OFF) } BINARY(); // call the binary clock subroutine if either hour or hour_ten changes } if (!start) // skip when code runs for the first time {STEPPERS();} // call the stepper subroutine to update the analog handles start = 0; // change it so it won't excute after the initial run } } // ####################################################################################### // ############# Call Subroutine LIGHTS to send 0 or 1 to designated LEDs ################ // ####################################################################################### void LIGHTS() { if (digit == 0) { digitalWrite(pin[0], S0[0]); digitalWrite(pin[1], S0[1]); digitalWrite(pin[2], S0[2]); digitalWrite(pin[3], S0[3]); digitalWrite(pin[4], S0[4]); digitalWrite(pin[5], S0[5]); digitalWrite(pin[6], S0[6]); digitalWrite(pin[7], S0[7]); digitalWrite(pin[8], S0[8]); } else if (digit == 1) { digitalWrite(pin[0], S1[0]); digitalWrite(pin[1], S1[1]); digitalWrite(pin[2], S1[2]); digitalWrite(pin[3], S1[3]); digitalWrite(pin[4], S1[4]); digitalWrite(pin[5], S1[5]); digitalWrite(pin[6], S1[6]); digitalWrite(pin[7], S1[7]); digitalWrite(pin[8], S1[8]); } else if (digit == 2) { digitalWrite(pin[0], S2[0]); digitalWrite(pin[1], S2[1]); digitalWrite(pin[2], S2[2]); digitalWrite(pin[3], S2[3]); digitalWrite(pin[4], S2[4]); digitalWrite(pin[5], S2[5]); digitalWrite(pin[6], S2[6]); digitalWrite(pin[7], S2[7]); digitalWrite(pin[8], S2[8]); } else if (digit == 3) { digitalWrite(pin[0], S3[0]); digitalWrite(pin[1], S3[1]); digitalWrite(pin[2], S3[2]); digitalWrite(pin[3], S3[3]); digitalWrite(pin[4], S3[4]); digitalWrite(pin[5], S3[5]); digitalWrite(pin[6], S3[6]); digitalWrite(pin[7], S3[7]); digitalWrite(pin[8], S3[8]); } else if (digit == 4) { digitalWrite(pin[0], S4[0]); digitalWrite(pin[1], S4[1]); digitalWrite(pin[2], S4[2]); digitalWrite(pin[3], S4[3]); digitalWrite(pin[4], S4[4]); digitalWrite(pin[5], S4[5]); digitalWrite(pin[6], S4[6]); digitalWrite(pin[7], S4[7]); digitalWrite(pin[8], S4[8]); } else if (digit == 5) { digitalWrite(pin[0], S5[0]); digitalWrite(pin[1], S5[1]); digitalWrite(pin[2], S5[2]); digitalWrite(pin[3], S5[3]); digitalWrite(pin[4], S5[4]); digitalWrite(pin[5], S5[5]); digitalWrite(pin[6], S5[6]); digitalWrite(pin[7], S5[7]); digitalWrite(pin[8], S5[8]); } else if (digit == 6) { digitalWrite(pin[0], S6[0]); digitalWrite(pin[1], S6[1]); digitalWrite(pin[2], S6[2]); digitalWrite(pin[3], S6[3]); digitalWrite(pin[4], S6[4]); digitalWrite(pin[5], S6[5]); digitalWrite(pin[6], S6[6]); digitalWrite(pin[7], S6[7]); digitalWrite(pin[8], S6[8]); } else if (digit == 7) { digitalWrite(pin[0], S7[0]); digitalWrite(pin[1], S7[1]); digitalWrite(pin[2], S7[2]); digitalWrite(pin[3], S7[3]); digitalWrite(pin[4], S7[4]); digitalWrite(pin[5], S7[5]); digitalWrite(pin[6], S7[6]); digitalWrite(pin[7], S7[7]); digitalWrite(pin[8], S7[8]); } else if (digit == 8) { digitalWrite(pin[0], S8[0]); digitalWrite(pin[1], S8[1]); digitalWrite(pin[2], S8[2]); digitalWrite(pin[3], S8[3]); digitalWrite(pin[4], S8[4]); digitalWrite(pin[5], S8[5]); digitalWrite(pin[6], S8[6]); digitalWrite(pin[7], S8[7]); digitalWrite(pin[8], S8[8]); } else if (digit == 9) { digitalWrite(pin[0], S9[0]); digitalWrite(pin[1], S9[1]); digitalWrite(pin[2], S9[2]); digitalWrite(pin[3], S9[3]); digitalWrite(pin[4], S9[4]); digitalWrite(pin[5], S9[5]); digitalWrite(pin[6], S9[6]); digitalWrite(pin[7], S9[7]); digitalWrite(pin[8], S9[8]); } } // ####################################################################################### // ############# Call Subroutine BINARY to send 0 or 1 to designated LEDs ################ // ####################################################################################### void BINARY() { bin_hr = hr_one + hr_ten * 10; bin[0] = 1; bin[1] = 1; bin[2] = 1; bin[3] = 1; // reset binary hour to 1111 while (bin_hr > 0) { if (bin_hr >= 8) { bin[0] = 0; bin_hr = bin_hr - 8; } if (bin_hr >= 4) { bin[1] = 0; bin_hr = bin_hr - 4; } if (bin_hr >= 2) { bin[2] = 0; bin_hr = bin_hr - 2; } if (bin_hr == 1) { bin[3] = 0; bin_hr = bin_hr - 1; } } digitalWrite(48, bin[0]); digitalWrite(49, bin[1]); digitalWrite(50, bin[2]); digitalWrite(51, bin[3]); } // ####################################################################################### // ############# Call Subroutine STEPPERS to send 0 or 1 to designated LEDs ################ // ####################################################################################### void STEPPERS() { if (mint % 30 == 0) // add the rounding to min and hr at the 30 min and the 60 min marks { steps_min = 106+20; } else { steps_min = 106; } if ((mint == 0) & (hr % 3 > 0))// add the rounding to hr at the beginning of the hr and skip every two hours { steps_hr = 71 + 10; } else { steps_hr = 71; } // Move the minutes handle digitalWrite(dirPin_min, LOW); digitalWrite(enPin_min, LOW); for (long x = 0; x < steps_min; x++) { digitalWrite(stepPin_min, HIGH); delayMicroseconds(td); digitalWrite(stepPin_min, LOW); delayMicroseconds(td); } digitalWrite(enPin_min, HIGH); // move the hour handle digitalWrite(dirPin_hr, HIGH); digitalWrite(enPin_hr, LOW); for (long y = 0; y < steps_hr; y++) { digitalWrite(stepPin_hr, HIGH); delayMicroseconds(td); digitalWrite(stepPin_hr, LOW); delayMicroseconds(td); } digitalWrite(enPin_hr, HIGH); }