Components and supplies
Jumper wires (generic)
I2C 16x2 Arduino LCD Display Module
Arduino UNO
Apps and platforms
Arduino IDE
Project description
Code
Repository link:
Code:
arduino
1/* Arduino 'Clock' with LCD (20x4) I2C. 2 3 This I2C LCD backpack contains the PCF8574 port-expander 4 IC. Beware that this sketch can work with backpacks that 5 contains this IC, but may not work with variations. 6 7 Components: 8 - Arduino Uno 9 - LCD I2C (20x4) 10 11 Libraries: 12 - LiquidCrystal_I2C library 13 14 Created on 25 June 2022 by c010rblind3ngineer 15*/ 16 17#include <LiquidCrystal_I2C.h> 18 19LiquidCrystal_I2C lcd(0x27, 20, 4); 20 21const int a_sec = 900; // clock second hand 22 23int Hrs, Mins; 24int Secs = 0; 25String AmPm; // to store the string data to display 'AM' or 'PM' 26int ampm; // to store user input 27int day, d, m, yr; 28char *DAYS[] = {"Mon", "Tues", "Wed", "Thurs", "Fri", "Sat", "Sun"}; 29char *MONTHS[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", 30 "July", "Aug", "Sep", "Oct", "Nov", "Dec" 31 }; 32 33void setup() 34{ 35 Serial.begin(9600); 36 lcd.init(); 37 lcd.backlight(); 38 clkInit(); 39} 40void loop() 41{ 42 //...Display Time to LCD... 43 lcd.setCursor(0, 1); 44 lcd.print("Time: "); 45 printolcd(Hrs); 46 lcd.print(":"); 47 printolcd(Mins); 48 lcd.print(":"); 49 printolcd(Secs); 50 lcd.print(AmPm); 51 52 //...Clock starts... 53 Secs++; 54 if (Secs == 60) { 55 Secs = 0; 56 Mins++; 57 } 58 if (Mins == 60) { 59 Mins = 0; 60 Hrs++; 61 } 62 if (Hrs == 13) { 63 Hrs = 1; 64 } 65 if (Hrs == 12 && Mins == 00 && Secs == 00) { 66 if (AmPm == "AM") { 67 AmPm = "PM"; 68 } 69 else { 70 AmPm = "AM"; 71 d++; 72 reset_dom(m); // reset the day to 1 when the days of the month exceed it's limit 73 74 //...Reset LCD row 0 and update date... 75 lcd.setCursor(0, 0); 76 lcd.print(" "); 77 lcd.setCursor(0, 0); 78 lcd.print(DAYS[day]); // mon, tues, wed... etc. 79 lcd.print(", "); 80 printolcd(d); 81 lcd.print(" "); 82 lcd.print(MONTHS[m - 1]); 83 lcd.print(" "); 84 lcd.print(yr); 85 day++; 86 } 87 } 88 delay(a_sec); // delay acts as the clock second hand 89} 90 91 92/////////////////////////////////////// 93////Initialise the 'clock' settings//// 94/////////////////////////////////////// 95void clkInit() { 96 97 Serial.println("What is the day today? (1 - 7)"); 98 Serial.println("\ (1) Monday\ 99\ (2) Tuesday\ 100\ (3) Wednesday\ 101\ (4) Thursday\ 102\ (5) Friday\ 103\ (6) Saturday\ 104\ (7) Sunday"); 105 while (Serial.available() == 0) {}; // wait for user input 106 day = Serial.parseInt(); // store what day is today in integer variable 'day' 107 108 delay(500); 109 110 Serial.println("\ 111What is today's date?"); 112 Serial.print("Day (1 - 31): "); 113 while (Serial.available() == 0) {}; // wait for user input 114 d = Serial.parseInt(); // store day in integer variable 'd' 115 printoserial(d); 116 117 Serial.print("\ 118Month (1 - 12): "); 119 while (Serial.available() == 0) {}; // wait for user input 120 m = Serial.parseInt(); // store month in integer variable 'm' 121 Serial.print(MONTHS[m - 1]); 122 Serial.print("\ 123Year (YYYY): "); 124 while (Serial.available() == 0) {}; // wait for user input 125 yr = Serial.parseInt(); // store year in integer variable 'yr' 126 Serial.print(yr); 127 128 delay(500); 129 130 Serial.print("\ 131\ 132\ Today is "); 133 Serial.print(DAYS[day - 1]); 134 Serial.print(", "); 135 printoserial(d); 136 Serial.print(" "); 137 Serial.print(MONTHS[m - 1]); 138 Serial.print(" "); 139 Serial.print(yr); 140 Serial.print("\ 141\ 142Is it - (1)AM or (2)PM ?"); 143 while (Serial.available() == 0) {}; 144 ampm = Serial.parseInt(); 145 146 delay(500); 147 148 if (ampm == 1) { 149 AmPm = "AM"; 150 Serial.print("\ 151Clock set to AM\ 152\ 153"); 154 } 155 if (ampm == 2) { 156 AmPm = "PM"; 157 Serial.print("\ 158Clock set to PM\ 159\ 160"); 161 } 162 163 delay(500); 164 165 Serial.print("Input Hours (1 - 12) : "); 166 while (Serial.available() == 0) {} // wait till the user inputs data 167 Hrs = Serial.parseInt(); 168 printoserial(Hrs); 169 170 delay(500); 171 172 Serial.print("\ 173Input Minutes (0 - 59) : "); 174 while (Serial.available() == 0) {} // wait till the user inputs data 175 Mins = Serial.parseInt(); 176 printoserial(Mins); 177 178 delay(500); 179 180 Serial.print("\ 181\ 182\ Clock set to : "); 183 184 //...Display time to Serial Monitor... 185 printoserial(Hrs); 186 Serial.print(":"); 187 printoserial(Mins); 188 Serial.print(AmPm); 189 190 //... Display Date to LCD... 191 lcd.setCursor(0, 0); 192 lcd.print(DAYS[day - 1]); 193 lcd.print(", "); 194 printolcd(d); 195 lcd.print(" "); 196 lcd.print(MONTHS[m - 1]); 197 lcd.print(" "); 198 lcd.print(yr); 199} 200 201 202////////////////////////////////////////////////////////////////////////// 203////To put an extra '0' infront of the variables on the Serial Monitor//// 204////////////////////////////////////////////////////////////////////////// 205void printoserial(int val) 206{ 207 if (val < 10) { 208 Serial.print("0"); 209 Serial.print(val); 210 } 211 else { 212 Serial.print(val); 213 } 214} 215 216/////////////////////////////////////////////////////////////// 217////To put an extra '0' infront of the variables on the LCD//// 218/////////////////////////////////////////////////////////////// 219void printolcd(int val) 220{ 221 if (val < 10) { 222 lcd.print("0"); 223 lcd.print(val); 224 } 225 else { 226 lcd.print(val); 227 } 228} 229 230void reset_dom(int var) { 231 switch (var) { 232 case 1: // Jan - 31 days 233 if (d == 32) { 234 d = 1; 235 m++; 236 } 237 break; 238 case 2: // Feb - 28 days 239 if (d == 29) { 240 d = 1; 241 m++; 242 } 243 break; 244 case 3: // March - 31 days 245 if (d == 32) { 246 d = 1; 247 m++; 248 } 249 break; 250 case 4: // April - 30 days 251 if (d == 31) { 252 d = 1; 253 m++; 254 } 255 break; 256 case 5: // May - 31 days 257 if (d == 32) { 258 d = 1; 259 m++; 260 } 261 break; 262 case 6: // June - 30 days 263 if (d == 31) { 264 d = 1; 265 m++; 266 } 267 break; 268 case 7: // July - 31 days 269 if (d == 32) { 270 d = 1; 271 m++; 272 } 273 break; 274 case 8: // August - 31 days 275 if (d == 32) { 276 d = 1; 277 m++; 278 } 279 break; 280 case 9: // September - 30 days 281 if (d == 31) { 282 d = 1; 283 m++; 284 } 285 break; 286 case 10: // October - 31 days 287 if (d == 32) { 288 d = 1; 289 m++; 290 } 291 break; 292 case 11: // November - 30 days 293 if (d == 31) { 294 d = 1; 295 m++; 296 } 297 break; 298 case 12: // December - 31 days 299 if (d == 32) { 300 d = 1; 301 m++; 302 } 303 break; 304 } 305} 306
Repository link:
Downloadable files
Circuit:
Circuit:
Schematic:
Schematic:
Circuit:
Circuit:
Schematic:
Schematic:
Comments
Only logged in users can leave comments