Code

Arduino Digital Clock Version 1.1C/C++

#include<LiquidCrystal.h>#include"TimerOne.h"#define btnRIGHT 1#define btnUP 2#define btnDOWN 3#define btnLEFT 4#define btnSELECT 5#define btnNONE 0intbtnSelected=0;// select the pins used on the LCD panelLiquidCrystallcd(8,9,4,5,6,7);intlcd_key=0;intadc_key_in0=0;intadc_key_in1=0;intadc_key_in2=0;intadc_key_in3=0;intadc_key_in4=0;intadc_key_in5=0;intthesec=0;intthesec2=0;intthemin=0;intthemin2=0;intthehr=1;intthehr2=0;boolbacklightstatus=true;boolsemicol=true;boolampm=true;voidsetup(){//initialize serial portSerial.begin(9600);//initialize the custom characterslcdcharinitiate();//initiate timer1 with 1 sec delayTimer1.initialize(1000000);//add the function that will be run every 1 secTimer1.attachInterrupt(thedisplayfunction);lcd.clear();theclock();}//checking if buuton is pressed and do its functionvoidloop(){inttheinput=read_LCD_buttons();if(theinput==btnRIGHT&&btnSelected!=btnRIGHT){btnSelected=btnRIGHT;themin++;if(themin>9){themin=0;themin2++;if(themin2>5){themin2=0;}}}elseif(theinput==btnLEFT&&btnSelected!=btnLEFT){btnSelected=btnLEFT;thehr++;if(thehr>9&&thehr2==0){thehr=0;thehr2++;if(thehr2>1){thehr2=0;}}if(thehr>2&&thehr2==1){thehr=1;thehr2++;if(thehr2>1){thehr2=0;ampm=!ampm;}}}elseif(theinput==btnSELECT&&btnSelected!=btnSELECT){btnSelected=btnSELECT;backlightstatus=!backlightstatus;if(backlightstatus){digitalWrite(10,LOW);}else{digitalWrite(10,HIGH);}}elseif(theinput==btnNONE&&btnSelected!=btnNONE){btnSelected=btnNONE;}delay(200);}//get which button is pressedintread_LCD_buttons(){adc_key_in0=analogRead(0);// read the value from the sensor// my buttons when read are centered at these valies: 0, 144, 329, 504, 741// we add approx 50 to those values and check to see if we are closeif(adc_key_in0>1000)returnbtnNONE;// We make this the 1st option for speed reasons since it will be the most likely resultif(adc_key_in0<50)returnbtnRIGHT;if(adc_key_in0<150)returnbtnUP;if(adc_key_in0<300)returnbtnDOWN;if(adc_key_in0<450)returnbtnLEFT;if(adc_key_in0<700)returnbtnSELECT;returnbtnNONE;// when all others fail, return this...}//the clock timer call functionvoidthedisplayfunction(){semicol=!semicol;thesec++;if(thesec>9){thesec=0;thesec2++;if(thesec2>5){thesec2=0;themin++;if(themin>9){themin=0;themin2++;if(themin2>5){themin2=0;thehr++;if(thehr>9&&thehr2==0){thehr=0;thehr2++;if(thehr2>1){thehr2=0;}}if(thehr>2&&thehr2==1){thehr=1;thehr2++;if(thehr2>1){thehr2=0;ampm=!ampm;}}}}}}theclock();}//print the value of the varaiable in its positionvoidtheclock(){numberprinter(themin,12);numberprinter(themin2,8);numberprinter(thehr,4);numberprinter(thehr2,0);if(ampm){lcdam(15);}else{lcdpm(15);}if(semicol){lcdnumbersemicolon(7);}else{lcdnumbersemicolonoff(7);}}//find the needed characer and added it to the needed positionvoidnumberprinter(intnum,intpos){if(num==0){lcdnumber0(pos);}if(num==1){lcdnumber1(pos);}if(num==2){lcdnumber2(pos);}if(num==3){lcdnumber3(pos);}if(num==4){lcdnumber4(pos);}if(num==5){lcdnumber5(pos);}if(num==6){lcdnumber6(pos);}if(num==7){lcdnumber7(pos);}if(num==8){lcdnumber8(pos);}if(num==9){lcdnumber9(pos);}}//initiate the lcd custom charctersvoidlcdcharinitiate(){byteC0[8]={0x1F,0x1F,0x1F,0x00,0x00,0x00,0x00,0x00};byteC1[8]={0x1F,0x1F,0x1F,0x00,0x00,0x1F,0x1F,0x1F};byteC2[8]={0x00,0x00,0x00,0x00,0x00,0x1F,0x1F,0x1F};byteC3[8]={0x00,0x00,0x0E,0x0A,0x0A,0x0E,0x00,0x00};lcd.createChar(0,C0);lcd.createChar(1,C1);lcd.createChar(2,C2);lcd.createChar(3,C3);lcd.begin(16,2);}//lcd draw character functionsvoidlcdnumber0(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(255));lcd.setCursor(startposition+1,0);lcd.write(byte(0));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(255));lcd.setCursor(startposition+1,1);lcd.write(byte(2));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber1(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(0));lcd.setCursor(startposition+1,0);lcd.write(byte(255));lcd.setCursor(startposition+2,0);lcd.write(byte(32));lcd.setCursor(startposition+0,1);lcd.write(byte(2));lcd.setCursor(startposition+1,1);lcd.write(byte(255));lcd.setCursor(startposition+2,1);lcd.write(byte(2));}voidlcdnumber2(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(0));lcd.setCursor(startposition+1,0);lcd.write(byte(0));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(255));lcd.setCursor(startposition+1,1);lcd.write(byte(1));lcd.setCursor(startposition+2,1);lcd.write(byte(2));}voidlcdnumber3(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(1));lcd.setCursor(startposition+1,0);lcd.write(byte(1));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(1));lcd.setCursor(startposition+1,1);lcd.write(byte(1));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber4(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(255));lcd.setCursor(startposition+1,0);lcd.write(byte(2));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(32));lcd.setCursor(startposition+1,1);lcd.write(byte(32));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber5(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(255));lcd.setCursor(startposition+1,0);lcd.write(byte(1));lcd.setCursor(startposition+2,0);lcd.write(byte(1));lcd.setCursor(startposition+0,1);lcd.write(byte(2));lcd.setCursor(startposition+1,1);lcd.write(byte(2));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber6(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(255));lcd.setCursor(startposition+1,0);lcd.write(byte(0));lcd.setCursor(startposition+2,0);lcd.write(byte(0));lcd.setCursor(startposition+0,1);lcd.write(byte(255));lcd.setCursor(startposition+1,1);lcd.write(byte(1));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber7(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(0));lcd.setCursor(startposition+1,0);lcd.write(byte(1));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(32));lcd.setCursor(startposition+1,1);lcd.write(byte(0));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber8(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(255));lcd.setCursor(startposition+1,0);lcd.write(byte(1));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(255));lcd.setCursor(startposition+1,1);lcd.write(byte(1));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumber9(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(255));lcd.setCursor(startposition+1,0);lcd.write(byte(1));lcd.setCursor(startposition+2,0);lcd.write(byte(255));lcd.setCursor(startposition+0,1);lcd.write(byte(2));lcd.setCursor(startposition+1,1);lcd.write(byte(2));lcd.setCursor(startposition+2,1);lcd.write(byte(255));}voidlcdnumbersemicolon(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(3));lcd.setCursor(startposition+0,1);lcd.write(byte(3));}voidlcdnumbersemicolonoff(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(32));lcd.setCursor(startposition+0,1);lcd.write(byte(32));}voidlcdam(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(97));lcd.setCursor(startposition+0,1);lcd.write(byte(109));}voidlcdpm(intstartposition){lcd.setCursor(startposition+0,0);lcd.write(byte(112));lcd.setCursor(startposition+0,1);lcd.write(byte(109));}//the function recive time from pc bye the application using serialvoidserialEvent(){while(Serial.available()){// get the stringdate:Stringthetime=Serial.readString();if(thetime[6]=='A'){ampm=true;}else{ampm=false;}thesec=(int)thetime[5]-48;thesec2=(int)thetime[4]-48;themin=(int)thetime[3]-48;themin2=(int)thetime[2]-48;thehr=(int)thetime[1]-48;thehr2=(int)thetime[0]-48;}}