/*
### simplest ever Arduino UNO digital clock ###
Time settings
- Press on H increments the Hours
- Press on M increments the Minutes and resets the seconds
*/
// initial Time display is 12:59:45 PM
int h=12;
int m=59;
int s=45;
int flag=1; //PM
// Time Set Buttons
int button1;
int button2;
// Pins definition for Time Set Buttons
int hs=A4;// pin A4 for Hours Setting
int ms=A5;// pin A5 for Minutes Setting
// Backlight Time Out
const int Time_light=150;
int bl_TO=Time_light;// Backlight Time-Out
int bl=10; // Backlight pin
const int backlight=120; // no more then 7mA !!!
// For accurate Time reading, use Arduino Real Time Clock and not just delay()
static uint32_t last_time, now = 0; // RTC
#include <LiquidCrystal_I2C.h>
// #include <BigNumbers_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); // construct LCD object
// BigNumbers_I2C bigNum(&lcd); // construct BigNumbers_I2C object, passing to it the name of our LCD object
byte x = 0;//x & y determines position of character on screen
byte y = 0;
byte i=0;
int M[3][4] = {{1, 2, 3, 4}, {6, 7, 8, 9}, {10, 11, 12, 13}};
int mS[4][4] = {{1, 0, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 1}};
int STPn[] = {0, 0, 0, 0};
int mN = 0;
void setup() {
// Serial.begin(9600);
// initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
// bigNum.begin(); // set up BigNumbers
lcd.clear(); // clear display
pinMode(hs,INPUT_PULLUP);// avoid external Pullup resistors for Button 1
pinMode(ms,INPUT_PULLUP);// and Button 2
now=millis(); // read RTC initial value
for (int i=1; i<=13; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
for (int i=0; i<=4; i++) {
for (int j=0; j<=4; j++) {
pinMode(M[i][j], OUTPUT);
digitalWrite(M[i][j], LOW);
}
}
}
void loop() {
lcd.init(); // every second
// Update LCD Display - Print TIME in Hour, Min, Sec + AM/PM
lcd.setCursor(0,0);
lcd.print("Time ");
if(h<10)lcd.print("0");// always 2 digits
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
if(flag==0) lcd.print(" AM");
if(flag==1) lcd.print(" PM");
lcd.setCursor(0,1);// for Line 2
lcd.print("Precision clock");
// improved replacement of delay(1000)
// Much better accuracy, no more dependant of loop execution time
for ( int i=0 ;i<5 ;i++) { // make 5 time 200ms loop, for faster Button response
while ((now-last_time)<200) { //delay200ms
now=millis();
}
// inner 200ms loop
last_time=now; // prepare for next loop
// read Setting Buttons
button1=digitalRead(hs);// Read Buttons
button2=digitalRead(ms);
// Hit any to activate Backlight
if( ((button1==0)|(button2==0)) & (bl_TO==1)) {
bl_TO=Time_light;
analogWrite(bl,backlight);
// wait until Button released
while ((button1==0)|(button2==0)) {
button1=digitalRead(hs);// Read Buttons
button2=digitalRead(ms);
}
}
else {
// Process Button 1 or Button 2 when hit while Backlight on
if(button1==0){
h=h+1;
bl_TO=Time_light;
analogWrite(bl,backlight);
}
if(button2==0){
s=0;
m=m+1;
bl_TO=Time_light;
analogWrite(bl,backlight);
}
// Update display if time set button pressed
if((button1==0)|(button2==0)) {
// Update LCD Display
// Print TIME in Hour, Min, Sec + AM/PM
lcd.setCursor(0,0);
lcd.print("Time ");
if(h<10)lcd.print("0");// always 2 digits
lcd.print(h);
lcd.print(":");
if(m<10)lcd.print("0");
lcd.print(m);
lcd.print(":");
if(s<10)lcd.print("0");
lcd.print(s);
if(flag==0) lcd.print(" AM");
if(flag==1) lcd.print(" PM");
lcd.setCursor(0,1);// for Line 2
lcd.print("Precision clock");
}
} // end if else
}// end for
// outer 1000ms loop
s=s+1; //increment sec. counting
for (int y2y=0; y2y <=20; y2y++) {
Rotate(0, STPn[0]); // mN = Motor Number - STPn[mN] = The last Step for THIS Motor
}
// ---- manage seconds, minutes, hours am/pm overflow ----
if(s==60){
s=0;
m=m+1;
for (int y2y=0; y2y <=20; y2y++) {
Rotate(1, STPn[1]); // mN = Motor Number - STPn[mN] = The last Step for THIS Motor
}
}
if(m==60) {
m=0;
h=h+1;
for (int y2y=0; y2y <=20; y2y++) {
Rotate(2, STPn[2]); // mN = Motor Number - STPn[mN] = The last Step for THIS Motor
}
}
if(h==13) {
h=1;
flag=flag+1;
if(flag==2)flag=0;
}
}
void Rotate(int mN2, int STPn2) {
for (int j=0; j<=3; j++) {
digitalWrite(M[mN2][j], mS[STPn2][j]);
}
if (STPn[mN2] < 3) {
STPn[mN2]++;
} else {
STPn[mN2] = 0;
}
delay(3); // Stepper min Delay between Comands
}