O Watch Programming: Basic Watch
Let us start with a basic watch program. The way the watch works is you first set the time and date on the micro-controller and then retrieve the current value when you need to show it.
We will use the Arduino Time Library to make the watch program.
First downloaded the latest version of Arduino Time library.
Unzip the folder and rename it to ‘Time”. Place the folder under Documents->Arduino->Library.
When we adapt a standard Arduino program for O Watch, you need to keep in mind two things: a) Include and Initialize the TinyScreen library and b) All output will be via the TinyScreen display.
Simple Watch 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
/* * Simple Watch * * Demonstrates the use of the Arduino Time library to make a simple digital watch * * Uses Arduino Time library http://playground.arduino.cc/code/time * Maintained by Paul Stoffregen https://github.com/PaulStoffregen/Time * * This code is for the TinyScreen+ board by TinyCircuits used by O Watch http://tiny-circuits.com * * This example is created by O Watch on 6 March 2016 http://theowatch.com * */ #include <TinyScreen.h> //include TinyScreen library #include <TimeLib.h> //include the Arduino Time library TinyScreen display = TinyScreen(TinyScreenPlus); //Create the TinyScreen object void setup() { display.begin(); //Initializes TinyScreen board display.setFlip(1); //Flips the TinyScreen rightside up for O Watch display.on(); //Turns TinyScreen display on display.fontColor(TS_8b_White,TS_8b_Black); //Set the font color, font background display.setBrightness(10); //Set display brightness 0 - 15 // Set the time and date. Change this to your current date and time. setTime(13,19,55,6,3,2016); //values in the order hr,min,sec,day,month,year } void loop() { display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type // Print date in US format MM:DD:YY (Switch the order in which day, month, year that you like to use) display.setCursor(15,8); //Set the cursor where you want to start printing the date display.print(monthShortStr(month())); display.print(" "); display.print(day()); display.print(", "); display.print(year()); display.setCursor(30,25); //Set the cursor where you want to start printing the date display.print(dayStr(weekday())); display.setFont(liberationSansNarrow_16ptFontInfo); //Set the font type // display time in HH:MM:SS 24 hour format display.setCursor(20,45); //Set the cursor where you want to start printing the time if(hour()<10) display.print(0); //print a leading 0 if hour value is less than 0 display.print(hour()); display.print(":"); if(minute()<10) display.print(0); //print a leading 0 if minute value is less than 0 display.print(minute()); display.print(":"); if(second()<10) display.print(0); //print a leading 0 if seconds value is less than 0 display.print(second()); display.print(" "); //just a empty space after the seconds delay(1000); //delay 1 second } |
You can copy and paste the above program in to your Arduino IDE and upload it to O Watch.
The following are the main functions used here from the Arduino Time library.
- setTime(hr,min,sec,day,mnth,yr): Sets the time
- hour(): The hour now (0-23)
- minute(): The minute now (0-59)
- second(): The second now (0-59)
- day(): The day now (1-31)
- weekday(): Day of the week, Sunday is day 0
- month(): The month now (1-12)
- year() The full four digit year: (2009, 2010 etc)
- monthStr(month()): Gives the month as text string for the retrieved month() value
- dayStr(day()): Gives the month as text string for the retrieved day() value
Simple Watch 2
Now let us take it a step forward and write a watch with functions to update the time and date. In this we will also see how to use the buttons to select the part of the program (or function) you want to run. We will also see how to use the Arduino for and switch/case statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
/* * Simple Watch 2 * * Demonstrates the use of the Arduino Time library to make a simple digital watch * In this we write functions to set time and date and learn how to use the buttons * Also learn Arduino 'switch' and 'for' statements/commands * * Uses Arduino Time library http://playground.arduino.cc/code/time * Maintained by Paul Stoffregen https://github.com/PaulStoffregen/Time * * This code is for the TinyScreen+ board by TinyCircuits used by O Watch http://tiny-circuits.com * * This example is created by O Watch on 6 March 2016 http://theowatch.com * */ #include <TinyScreen.h> //include TinyScreen library #include <TimeLib.h> //include the Arduino Time library TinyScreen display = TinyScreen(TinyScreenPlus); //Create the TinyScreen object void setup() { display.begin(); //Initializes TinyScreen board display.setFlip(1); //Flips the TinyScreen rightside up for O Watch display.on(); //Turns TinyScreen display on display.fontColor(TS_8b_White,TS_8b_Black); //Set the font color, font background display.setBrightness(10); //Set display brightness 0 - 15 // Set the time and date. Change this to your current date and time. setTime(13,19,55,6,3,2016); //values in the order hr,min,sec,day,month,year //Show info at start to say what each button does display.setFont(liberationSansNarrow_14ptFontInfo); display.setCursor(35,30); display.print("Set"); display.setFont(liberationSansNarrow_10ptFontInfo); display.setCursor(5,7); display.print("Shows Time 10s>"); display.setCursor(60,45); display.print("Date>"); display.setCursor(5,45); display.print("<Time"); delay(8000); display.clearScreen(); } void loop() { //Switch statement to select part of code to run based on button selection switch (display.getButtons()) { //Run updateTime function case TSButtonLowerLeft: display.on(); updateTime(); delay(200); display.off(); break; //Run updateDate function case TSButtonLowerRight: delay(200); display.on(); updateDate(); delay(200); display.off(); break; //Show time for 10 seconds case TSButtonUpperRight: display.on(); for(int i=0; i<11; i++) { display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type // Print date in US format MM:DD:YY (Switch the order in which day, month, year that you like to use) display.setCursor(15,8); //Set the cursor where you want to start printing the date display.print(monthShortStr(month())); display.print(" "); display.print(day()); display.print(", "); display.print(year()); display.setCursor(30,25); //Set the cursor where you want to start printing the date display.print(dayStr(weekday())); display.setFont(liberationSansNarrow_16ptFontInfo); //Set the font type // display time in HH:MM:SS 24 hour format display.setCursor(20,45); //Set the cursor where you want to start printing the time if(hour()<10) display.print(0); //print a leading 0 if hour value is less than 0 display.print(hour()); display.print(":"); if(minute()<10) display.print(0); //print a leading 0 if minute value is less than 0 display.print(minute()); display.print(":"); if(second()<10) display.print(0); //print a leading 0 if seconds value is less than 0 display.print(second()); display.print(" "); //just a empty space after the seconds delay(1000); //delay 1 second } delay(200); display.clearScreen(); display.off(); break; } delay(200); } //Function to update time void updateTime() { //Set hour display.clearScreen(); display.setFont(liberationSansNarrow_8ptFontInfo); //Set the font type display.setCursor(2,15); display.print("<Save | Change>"); display.setCursor(15,40); display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type display.print("Set Hour:"); int myhour=hour(); while(!display.getButtons(TSButtonUpperLeft)) { display.setCursor(75,40); if(myhour<10) display.print("0"); display.print(myhour); if(display.getButtons(TSButtonUpperRight)) myhour++; if(display.getButtons(TSButtonLowerRight)) myhour--; if(myhour<0) myhour=24; if(myhour>24) myhour=0; delay(200); } setTime(myhour, minute(), second(), day(), month(), year()); delay(500); display.clearScreen(); display.setCursor(10,25); display.print("Hour Saved"); delay(1000); //Set minute display.clearScreen(); display.setFont(liberationSansNarrow_8ptFontInfo); //Set the font type display.setCursor(2,15); display.print("<Save | Change>"); display.setCursor(15,40); display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type display.print("Set Mins:"); int myminute=minute(); while(!display.getButtons(TSButtonUpperLeft)) { display.setCursor(75,40); if(myminute<10) display.print("0"); display.print(myminute); if(display.getButtons(TSButtonUpperRight)) myminute++; if(display.getButtons(TSButtonLowerRight)) myminute--; if(myminute<0) myminute=60; if(myminute>60) myminute=0; delay(200); } setTime(hour(), myminute, second(), day(), month(), year()); delay(500); display.clearScreen(); display.setCursor(10,25); display.print("Minute Saved"); delay(1000); //Set second display.clearScreen(); display.setFont(liberationSansNarrow_8ptFontInfo); //Set the font type display.setCursor(2,15); display.print("<Save | Change>"); display.setCursor(15,40); display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type display.print("Set Secs:"); int mysecond=second(); while(!display.getButtons(TSButtonUpperLeft)) { display.setCursor(75,40); if(mysecond<10) display.print("0"); display.print(mysecond); if(display.getButtons(TSButtonUpperRight)) mysecond++; if(display.getButtons(TSButtonLowerRight)) mysecond--; if(mysecond<0) mysecond=60; if(mysecond>60) mysecond=0; delay(200); } setTime(hour(), minute(), mysecond, day(), month(), year()); delay(500); display.clearScreen(); display.setCursor(10,25); display.print("Second Saved"); delay(1000); display.clearScreen(); } //Function to update time void updateDate() { //Set day display.clearScreen(); display.setFont(liberationSansNarrow_8ptFontInfo); //Set the font type display.setCursor(2,15); display.print("<Save | Change>"); display.setCursor(10,40); display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type display.print("Set Day:"); int myday=day(); while(!display.getButtons(TSButtonUpperLeft)) { display.setCursor(75,40); if(myday<10) display.print("0"); display.print(myday); if(display.getButtons(TSButtonUpperRight)) myday++; if(display.getButtons(TSButtonLowerRight)) myday--; if(myday<0) myday=31; if(myday>31) myday=0; delay(200); } setTime(hour(), minute(), second(), myday, month(), year()); delay(500); display.clearScreen(); display.setCursor(10,25); display.print("Day Saved"); delay(1000); //Set month display.clearScreen(); display.setFont(liberationSansNarrow_8ptFontInfo); //Set the font type display.setCursor(2,15); display.print("<Save | Change>"); display.setCursor(10,40); display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type display.print("Set Month:"); int mymonth=month(); while(!display.getButtons(TSButtonUpperLeft)) { display.setCursor(75,40); if(mymonth<10) display.print("0"); display.print(mymonth); if(display.getButtons(TSButtonUpperRight)) mymonth++; if(display.getButtons(TSButtonLowerRight)) mymonth--; if(mymonth<0) mymonth=60; if(mymonth>60) mymonth=0; delay(200); } setTime(hour(), minute(), second(), day(), mymonth, year()); delay(500); display.clearScreen(); display.setCursor(10,25); display.print("Month Saved"); delay(1000); //Set Year display.clearScreen(); display.setFont(liberationSansNarrow_8ptFontInfo); //Set the font type display.setCursor(2,15); display.print("<Save | Change>"); display.setCursor(10,40); display.setFont(liberationSansNarrow_12ptFontInfo); //Set the font type display.print("Set Year:"); int myyear=year(); while(!display.getButtons(TSButtonUpperLeft)) { display.setCursor(68,40); if(myyear<10) display.print("0"); display.print(myyear); if(display.getButtons(TSButtonUpperRight)) myyear++; if(display.getButtons(TSButtonLowerRight)) myyear--; if(myyear<2010) myyear=2030; if(myyear>2030) myyear=2010; delay(200); } setTime(hour(), minute(), second(), day(), month(), myyear); delay(500); display.clearScreen(); display.setCursor(10,25); display.print("Year Saved"); delay(1000); display.clearScreen(); } |
Note the following from this second program:
- User Defined Functions: You see two functions updateTime() and updateDate() that are written by the user and called from the main loop.
- Switch/Case: The use of Arduino Switch/Case statements to select the code block/function you want to run.
- For: Arduino for statements to create a loop for a specific number of runs.
- display.on() and off(): We use these TinyScreen functions to switch off the display when not needed to conserve power.
There you have your basic digital watch that can last most of the day. Hope this gives you enough of an idea to make your own versions of the watch.
This is a simple digital watch using the Arduino Time Library that essentially keeps time using an internal counter in the Arduino micro-controller. Though this is fairly accurate, you will notice the time drift a by a minute or two every hour. To keep accurate time you need to use the Arduino RTC library that uses the Real-Time-Clock built into the O Watch main board. Refer to this O Watch RTC example from J Koger, one of our awesome backers.