Components and supplies
1
Alphanumeric LCD, 20 x 4
1
Buzzer
5
Resistor 10k ohm
5
Pushbutton switch 12mm
1
Breadboard (generic)
1
Real Time Clock (RTC)
1
Arduino UNO
Tools and machines
1
Soldering iron (generic)
1
3D Printer (generic)
Apps and platforms
1
Arduino IDE
Project description
Code
Alarm Clock
arduino
1/* Alarm Clock 2 by smi1100 - 06/06/2019 3 My first project. The following components are needed: 4 - Arduino Uno 5 - DS3231 real time clock 6 - 20x4 I2C Display 7 - buzzer 8 - 5 buttons 9 - 5 resistors 10k Ohm 10 - bread board 11 - casing 12 13 14/* modul 0 - I2C Bus 15 - I2C adresses 16 - LCD 0x27 17 - real time clock 18 RTC DS3231 0x68 19 EEPROM AT24C32 0x57 (not in use) 20*/ 21 22#include <Wire.h> 23 24 25/* modul 1 - DS3231 real time clock 26 - connections (from right side) - GND, VCC, SDA, SCL 27 - RTClib.h is required 28 - I2C connection 29*/ 30 31#include "RTClib.h" 32RTC_DS1307 rtc; 33char weekday[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerst.", "Freitag", "Samstag"}; 34// in english char weekday[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; 35 36 37/* modul 2 - Joy-IT SBC-LCD 20x4 display 38 - 4 rows, 20 characters 39 - connections (from right side) - GND, VCC, SDA, SCL 40 - LiquidCrystal_I2C.h is required 41 - I2C connection 42*/ 43 44#include <LiquidCrystal_I2C.h> 45LiquidCrystal_I2C lcd(0x27, 20, 4); // adress 0x27 46//LiquidCrystal_I2C lcd(0x3F,20,4); // if the lcd is not working, change adress to 0x3F 47 48 49/* modul 3 - buttons to adjust the alarm time and button for the buzzer 50*/ 51 52int buzzer = 4; 53int buttonhourup = A3; 54int buttonhourdown = A2; 55int buttonminuteup = A1; 56int buttonminutedown = A0; 57int readbuttonhourup; 58int readbuttonhourdown; 59int readbuttonminuteup; 60int readbuttonminutedown; 61 62// alarmtime 63int alarmtimehour; 64int alarmtimeminute; 65 66 67/* modul 4 - switch for alarm on/off 68 - OneButton.h is required 69 - see internet resource https://www.makerblog.at/2015/01/arduino-und-pushbuttons-drucktaster-einbinden-mit-der-onebutton-library/ 70*/ 71 72#include "OneButton.h" 73int buttonalarm = 5; 74OneButton button(buttonalarm, false); 75int alarmstate = 1; // equal to alarm is on 76int alarmtimeequaltime = 0; // active when alarm time is equal to time 77 78 79/* modul 5 - EEPROM - backup of alarmtime in the EEPROM 80 - EEPROM.h is required 81*/ 82 83#include "EEPROM.h" 84 85 86/* code 1 - summer or winter time 87 - see internet resource https://kaufma.net/blog/article/2016/08/24/sommerzeit-im-microcontroller/ 88 - see internet resource https://electronicfreakblog.wordpress.com/2014/03/06/die-zeit-im-sommer-und-im-winter/ 89 DST = Daylight Saving Time, equal to summer time 90*/ 91 92int DST; 93 94 95/* code 2 - changing buzzer sound 96*/ 97 98unsigned long previousMillis = 0; 99const long interval = 1000; 100 101 102void setup () 103{ 104 Serial.begin(9600); 105 106 // read alarmtime from EEPROM 107 108 alarmtimehour = EEPROM.read(0); 109 alarmtimeminute = EEPROM.read(1); 110 111 112 // initialize DS3231 RTC 113 114 Serial.begin(9600); 115 while (!Serial); // for Leonardo/Micro/Zero 116 117 if (! rtc.begin()) 118 { 119 Serial.println("Couldn't find RTC"); 120 while (1); 121 } 122 123 if (! rtc.isrunning()) 124 { 125 // define format once 126 Serial.println("RTC is NOT running!"); 127 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 128 Serial.println("RTC adjusted!"); 129 } 130 131 132 // initialize display 133 134 lcd.init(); 135 lcd.backlight(); 136 137 138 // initialize buttons and buzzer 139 140 pinMode(buttonhourup, INPUT); 141 pinMode(buttonhourdown, INPUT); 142 pinMode(buttonminuteup, INPUT); 143 pinMode(buttonminutedown, INPUT); 144 pinMode(buzzer, OUTPUT); 145 146 147 // initialize button as a switch 148 149 pinMode(buttonalarm, INPUT); 150 button.attachClick(clickedIt); 151 152} 153 154void loop () 155{ 156 157//determine summer or winter time 158 159int DST; 160 161DateTime now = rtc.now(); 162 163if (now.month() == 3 && now.day() > 24 && now.dayOfTheWeek() == 1 && now.hour() >= 2 || //exakter Tag Beginn 164 now.month() == 3 && now.day() + 8 - now.dayOfTheWeek() > 31 && now.dayOfTheWeek() != 1 || // restlichen Tage des Mrz 165 now.month() > 3 && now.month() < 10 || //April bis September 166 now.month() == 10 && now.day() + 8 - now.dayOfTheWeek() < 32 || //Oktober bis Tag vor letztem Sonntag 167 now.month() == 10 && now.day() > 24 && now.dayOfTheWeek() == 1 && now.hour() <= 3) //letzter Sonntag 168 { 169 DST = 1; 170 Serial.print("summertime - "); 171 Serial.print("time: "); 172 Serial.println(now.hour()+DST); 173 } 174else 175 { 176 DST = 0; 177 Serial.print("wintertime - "); 178 Serial.print("time: "); 179 Serial.println(now.hour()); 180 } 181 182 183// button as a switch 184 185 button.tick(); 186 delay(10); 187 188// shown data on the LCD 189 190 lcd.setCursor(1, 0); 191 192 lcd.print(weekday[now.dayOfTheWeek()]); 193 lcd.print(" "); 194 195 lcd.setCursor(13, 0); 196 197 if (now.day() < 10) lcd.print("0"); // less than 10 -> add 0 before it 198 lcd.print(now.day(), DEC); 199 lcd.print("."); 200 201 if (now.month() < 10) lcd.print("0"); // less than 10 -> add 0 before it 202 lcd.print(now.month(), DEC); 203 //lcd.print("."); 204 //lcd.print(now.year(), DEC); 205 206 207 lcd.setCursor(0, 1); 208 lcd.print(" Zeit Alarm"); 209 // in english lcd.print(" time alarm"); 210 211 lcd.setCursor(0, 2); 212 213 lcd.print(" "); 214 if ((now.hour() + DST) < 10) lcd.print("0"); // less than 10 -> add 0 before it 215 lcd.print((now.hour() + DST), DEC); 216 lcd.print(":"); 217 218 if (now.minute() < 10) lcd.print("0"); // less than 10 -> add 0 before it 219 lcd.print(now.minute(), DEC); 220 lcd.print(":"); 221 222 if (now.second() < 10) lcd.print("0"); // less than 10 -> add 0 before it 223 lcd.print(now.second(), DEC); 224 lcd.print(" "); 225 226 if (alarmtimehour < 10) lcd.print("0"); // less than 10 -> add 0 before it 227 lcd.print(alarmtimehour, DEC); 228 lcd.print(':'); 229 230 if (alarmtimeminute < 10) lcd.print("0"); // less than 10 -> add 0 before it 231 lcd.print(alarmtimeminute, DEC); 232 233 lcd.setCursor(14, 3); 234 if (alarmstate == 1)lcd.print("EIN"); 235 else lcd.print("AUS"); 236 /* in english if (alarmstate == 1)lcd.print("ON"); 237 else lcd.print("OFF"); 238 */ 239 240 // adjust the alarm time (hour) 241 242 readbuttonhourup = digitalRead(buttonhourup); 243 244 if (readbuttonhourup == HIGH) 245 { 246 delay(100); 247 if (alarmtimehour >= 23) alarmtimehour = 0; 248 else alarmtimehour ++; 249 } 250 251 252 readbuttonhourdown = digitalRead(buttonhourdown); 253 254 if (readbuttonhourdown == HIGH) 255 { 256 delay(100); 257 if (alarmtimehour <= 0) alarmtimehour = 23; 258 else alarmtimehour --; 259 } 260 261 262 // adjust the alarm time (minute) 263 264 readbuttonminuteup = digitalRead(buttonminuteup); 265 if (readbuttonminuteup == HIGH) 266 { 267 delay(100); 268 if (alarmtimeminute >= 59) alarmtimeminute = 0; 269 else alarmtimeminute ++; 270 } 271 272 273 readbuttonminutedown = digitalRead(buttonminutedown); 274 if (readbuttonminutedown == HIGH) 275 { 276 delay(100); 277 if (alarmtimeminute <= 0) alarmtimeminute = 59; 278 else alarmtimeminute --; 279 } 280 281 // write alarmtime to EEPROM 282 283 EEPROM.write(0, alarmtimehour); // first character = position, second character = value 284 EEPROM.write(1, alarmtimeminute); 285 286 287 // check alarm time 288 289 if ((now.hour() + DST) == alarmtimehour && now.minute() == alarmtimeminute && now.second() == 0 && alarmstate == 1) 290 { 291 alarmtimeequaltime = 1; 292 } 293 294 // activate alarm 295 296 if (alarmtimeequaltime == 1 && alarmstate == 1) 297 // it is not possible to use the command "delay" - the display of the seconds will stop 298 { 299 unsigned long currentMillis = millis(); 300 301 if (currentMillis - previousMillis >= interval) 302 { 303 tone(buzzer, 1000); 304 if (currentMillis - previousMillis >= 2*interval) 305 { 306 previousMillis = currentMillis; 307 } 308 } 309 310 else 311 { 312 tone(buzzer, 300); 313 } 314 } 315 316 if (alarmtimeequaltime == 1 && alarmstate == 0) 317 { 318 noTone(buzzer); 319 alarmtimeequaltime = 0; 320 } 321 322} 323 324 325void clickedIt() 326{ 327 alarmstate = !alarmstate; 328} 329
Downloadable files
Fritzing
Fritzing
Documentation
Case - Backside
Case - Backside
Case - Frontside
Case - Frontside
Comments
Only logged in users can leave comments