Homemade Retro Digital Wristwatch (Arduino)
After years of thinking about it, I finally made my own digital watch. Here's the process of how it came to be.
V Watch Code V 1 #include <LedDisplay.h> #include <TimeLib.h> #include <Bounce.h> #include <Metro.h> #define pinButtonLeft 30 #define pinButtonOk 31 #define pinButtonRight 32 #define dataPin 6 // connects to the display's data in #define registerSelect 7 // the display's register select pin #define clockPin 8 // the display's clock pin #define enable 9 // the display's chip enable pin #define reset 10 // the display's reset pin #define displayLength 8 // number of characters in the display LedDisplay myDisplay = LedDisplay(dataPin, registerSelect, clockPin, enable, reset, displayLength); Bounce buttonLeft = Bounce(pinButtonLeft, 10); Bounce buttonOk = Bounce(pinButtonOk, 10); Bounce buttonRight = Bounce(pinButtonRight, 10); Metro rtcUpdate = Metro(1000); String timeHoursString = "00"; String timeMinutesString = "00"; String dateString = " JAN 01 "; String dowNameString = "SUNMONTUEWEDTHUFRISAT"; String dowString = "ZZZ"; String monthNameString = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC"; String hourString = "00"; String minuteString = "00"; String domString = "00"; String monthString = "ZZZ"; String yearString = "0000"; String timerMinutesString = "00"; String timerSecondsString = "00"; String tempVolumeString; String tempHourString; String tempMinuteString; String tempDateString; String tempMonthString; boolean isAwake = true; boolean buttonLeftPressed = false; boolean buttonOkPressed = false; boolean buttonRightPressed = false; boolean timerIsActive = false; boolean timeIs12Hr = true; boolean flashState = true; boolean isSettingTime = false; elapsedMillis flashRate; elapsedMillis timerCounter; int brightness = 1; // screen brightness 0 - 15 int curMode = 1; int curAction = 1; int curSelection = 0; int curTimerMinutes = 0; int curTimerSeconds = 0; int lastTimerMinutes = -1; int lastTimerSeconds = -1; int curHour = 0; int lastHour = 0; int curMinute = 0; int lastMinute = 0; int curDow = 0; int curDay = 0; int lastDay = 0; int curMonth = 0; int lastMonth = 0; int curYear = 2017; int monthLengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; void setup() { // be sure to reduce clock speed to a minimum (with USB) to save power setSyncProvider(getTeensy3Time); // set the time provider to the internal RTC time setSyncInterval(3600); //every hour pinMode(pinButtonLeft, INPUT_PULLUP); pinMode(pinButtonOk, INPUT_PULLUP); pinMode(pinButtonRight, INPUT_PULLUP); myDisplay.begin(); myDisplay.setBrightness(brightness); // if time > 7 pm < 7 am set brightness low, else higher myDisplay.print("V-Watch"); delay(1250); myDisplay.clear(); myDisplay.home(); // Serial.begin(9600); getTimeInfo(); formatTimeInfo(); lastMinute = curMinute; } // ************************************************************************ END SETUP **************************************************************** void loop() { switch (curAction){ case 0: // waiting for input getInput(); if (rtcUpdate.check()){ // check every second if (isSettingTime == false){ getTimeInfo(); // don't update the watch from the RTC while we're adjusting it, gets wonky formatTimeInfo(); } } break; case 1: // update and load time myDisplay.clear(); myDisplay.home(); myDisplay.setCursor(1); myDisplay.print(timeHoursString); myDisplay.print(":"); myDisplay.print(timeMinutesString); delay(100); curAction = 0; // return to waiting for user feedback break; case 2: // update and load date myDisplay.clear(); myDisplay.home(); myDisplay.print(dateString); delay(100); curAction = 0; break; case 3: // update and load timer myDisplay.clear(); myDisplay.home(); myDisplay.print("TM :"); myDisplay.setCursor(3); myDisplay.print(timerMinutesString); myDisplay.setCursor(6); myDisplay.print(timerSecondsString); delay(100); curAction = 0; break; } // ************************************************************************ END ACTION SWITCHCASE switch (curMode){ case 0: // asleep break; case 1: // display time if (buttonLeftPressed){ curMode = 3; curAction = 3; //enter timer mode } if (buttonOkPressed){ curMode = 4; curAction = 0; //enter set time mode isSettingTime = true; } if (buttonRightPressed){ curMode = 2; curAction = 2; //enter date mode } if (lastMinute != curMinute){ curAction = 1; lastMinute = curMinute; } break; case 2: // display date if (buttonLeftPressed){ curMode = 1; curAction = 1; //enter time mode } if (buttonOkPressed){ curMode = 5; curAction = 0; //enter set date mode isSettingTime = true; } if (buttonRightPressed){ curMode = 3; curAction = 3; //enter timer mode } break; case 3: // display timer if (buttonLeftPressed){ curMode = 2; curAction = 2; //enter date mode } if (buttonOkPressed){ curMode = 6; curAction = 0; //enter set timer mode } if (buttonRightPressed){ curMode = 1; curAction = 1; //enter time mode } if (timerIsActive){ // timer only decrements in timer mode, could be changed to be universal if (timerCounter > 1000){ curTimerSeconds-=1; if (curTimerSeconds < 0 && curTimerMinutes >= 1){ curTimerSeconds = 59; curTimerMinutes-=1; } if (curTimerSeconds < 0 && curTimerMinutes == 0){ curTimerSeconds = 0; timerIsActive = false; // times up! } formatTimerMinutes();formatTimerSeconds(); myDisplay.setCursor(3); myDisplay.print(timerMinutesString); myDisplay.setCursor(6); myDisplay.print(timerSecondsString); timerCounter = 0; } } break; // ************************************************************************ END DISPLAY MODES case 4: // set time if (curSelection == 0){ // set hours if (buttonLeftPressed){ curHour--; if (curHour < 0) curHour = 23; } if (buttonOkPressed){ curSelection = 1; myDisplay.setCursor(1); myDisplay.print(timeHoursString); flashState = true; // lock in so that the display remains on here delay(100); // allow finger release time flushInput(); } if (buttonRightPressed){ curHour++; if (curHour > 23) curHour = 0; } if (lastHour != curHour){ lastHour = curHour; formatTimeInfo(); } if (flashRate > 250){ if (flashState){ myDisplay.setCursor(1); myDisplay.print(timeHoursString); } else { myDisplay.setCursor(1); myDisplay.print(" "); } flashRate = 0; flashState = !flashState; } } if (curSelection == 1){ // set minutes if (buttonLeftPressed){ curMinute--; if (curMinute < 0) curMinute = 59; } if (buttonOkPressed){ curMode = 1; curSelection = 0; flashState = true; myDisplay.setCursor(4); myDisplay.print(timeMinutesString); setTime(curHour, curMinute, 0, curDay, curMonth, curYear); Teensy3Clock.set(now()); isSettingTime = false; delay(100); // allow finger release time } if (buttonRightPressed){ curMinute++; if (curMinute > 59) curMinute = 0; } if (lastMinute != curMinute){ lastMinute = curMinute; formatTimeInfo(); } if (flashRate > 250){ if (flashState){ myDisplay.setCursor(4); myDisplay.print(timeMinutesString); } else { myDisplay.setCursor(4); myDisplay.print(" "); } flashRate = 0; flashState = !flashState; } } break; case 5: // set date if (curSelection == 0){ // set month if (buttonLeftPressed){ curMonth--; if (curMonth < 1) curMonth = 12; } if (buttonOkPressed){ curSelection = 1; myDisplay.setCursor(1); myDisplay.print(monthString); flashState = true; // lock in so that the display remains on here delay(100); // allow finger release time flushInput(); } if (buttonRightPressed){ curMonth++; if (curMonth > 12) curMonth = 1; } if (lastMonth != curMonth){ lastMonth = curMonth; formatTimeInfo(); } if (flashRate > 250){ if (flashState){ myDisplay.setCursor(1); myDisplay.print(monthString); } else { myDisplay.setCursor(1); myDisplay.print(" "); } flashRate = 0; flashState = !flashState; } } if (curSelection == 1){ // set day if (buttonLeftPressed){ curDay--; if (curDay < 1) curDay = monthLengths[curMonth-1]; } if (buttonOkPressed){ curMode = 2; curSelection = 0; flashState = true; myDisplay.setCursor(5); myDisplay.print(domString); setTime(curHour, curMinute, 0, curDay, curMonth, curYear); Teensy3Clock.set(now()); isSettingTime = false; delay(100); // allow finger release time } if (buttonRightPressed){ curDay++; if (curDay > monthLengths[curMonth-1]) curDay = 1; } if (lastMinute != curDay){ lastMinute = curDay; formatTimeInfo(); } if (flashRate > 250){ if (flashState){ myDisplay.setCursor(5); myDisplay.print(domString); } else { myDisplay.setCursor(5); myDisplay.print(" "); } flashRate = 0; flashState = !flashState; } } break; case 6: // set timer if (curSelection == 0){ // set minutes if (buttonLeftPressed){ curTimerMinutes--; if (curTimerMinutes < 0) curTimerMinutes = 59; } if (buttonOkPressed){ curSelection = 1; myDisplay.setCursor(3); myDisplay.print(timerMinutesString); flashState = true; // lock in so that the display remains on here delay(100); // allow finger release time flushInput(); } if (buttonRightPressed){ curTimerMinutes++; if (curTimerMinutes > 59) curTimerMinutes = 0; } if (lastTimerMinutes != curTimerMinutes){ lastTimerMinutes = curTimerMinutes; formatTimerMinutes(); } if (flashRate > 250){ if (flashState){ myDisplay.setCursor(3); myDisplay.print(timerMinutesString); } else { myDisplay.setCursor(3); myDisplay.print(" "); } flashRate = 0; flashState = !flashState; } } if (curSelection == 1){ // set minutes if (buttonLeftPressed){ curTimerSeconds--; if (curTimerSeconds < 0) curTimerSeconds = 59; } if (buttonOkPressed){ if (curTimerSeconds > 0 || curTimerMinutes > 0){ timerIsActive = true; } curMode = 3; curSelection = 0; flashState = true; myDisplay.setCursor(6); myDisplay.print(timerSecondsString); delay(100); // allow finger release time timerCounter = 0; } if (buttonRightPressed){ curTimerSeconds++; if (curTimerSeconds > 59) curTimerSeconds = 0; } if (lastTimerSeconds != curTimerSeconds){ lastTimerSeconds = curTimerSeconds; formatTimerSeconds(); } if (flashRate > 250){ if (flashState){ myDisplay.setCursor(6); myDisplay.print(timerSecondsString); } else { myDisplay.setCursor(6); myDisplay.print(" "); } flashRate = 0; flashState = !flashState; } } break; } flushInput(); } // ************************************************************************ END MAIN LOOP **************************************************************** void formatTimerMinutes(){ String tempCurTimerMinutesString = String(curTimerMinutes); if (curTimerMinutes < 10){ timerMinutesString.setCharAt(0, '0'); // add a leading 0 timerMinutesString.setCharAt(1, tempCurTimerMinutesString.charAt(0)); } else { timerMinutesString.setCharAt(0, tempCurTimerMinutesString.charAt(0)); timerMinutesString.setCharAt(1, tempCurTimerMinutesString.charAt(1)); } } void formatTimerSeconds(){ String tempCurTimerSecondsString = String(curTimerSeconds); if (curTimerSeconds < 10){ timerSecondsString.setCharAt(0, '0'); // add a leading 0 timerSecondsString.setCharAt(1, tempCurTimerSecondsString.charAt(0)); } else { timerSecondsString.setCharAt(0, tempCurTimerSecondsString.charAt(0)); timerSecondsString.setCharAt(1, tempCurTimerSecondsString.charAt(1)); } } void getTimeInfo(){ curHour = hour(); curMinute = minute(); curDay = day(); curMonth = month(); curYear = year(); } void formatTimeInfo(){ if (timeIs12Hr && curHour > 12){ curHour -= 12; } if (curHour == 0)curHour = 12; tempHourString = String(curHour); tempMinuteString = String(curMinute); tempDateString = String(curDay); //tempMonthString = String(curMonth); yearString = String(curYear); curDow = getDayOfWeek(curYear, curMonth, curDay); dowString.setCharAt(0, dowNameString.charAt(curDow * 3)); dowString.setCharAt(1, dowNameString.charAt(curDow * 3 + 1)); dowString.setCharAt(2, dowNameString.charAt(curDow * 3 + 2)); monthString.setCharAt(0, monthNameString.charAt((curMonth-1) * 3)); monthString.setCharAt(1, monthNameString.charAt((curMonth-1) * 3 + 1)); monthString.setCharAt(2, monthNameString.charAt((curMonth-1) * 3 + 2)); if (curHour < 10){ hourString.setCharAt(0, ' '); // add a leading space hourString.setCharAt(1, tempHourString.charAt(0)); } else { hourString.setCharAt(0, tempHourString.charAt(0)); hourString.setCharAt(1, tempHourString.charAt(1)); } if (curMinute < 10){ minuteString.setCharAt(0, '0'); // add a leading 0 minuteString.setCharAt(1, tempMinuteString.charAt(0)); } else { minuteString.setCharAt(0, tempMinuteString.charAt(0)); minuteString.setCharAt(1, tempMinuteString.charAt(1)); } if (curDay < 10){ domString.setCharAt(0, '0'); // add a leading 0 domString.setCharAt(1, tempDateString.charAt(0)); } else { domString.setCharAt(0, tempDateString.charAt(0)); domString.setCharAt(1, tempDateString.charAt(1)); } timeHoursString.setCharAt(0, hourString.charAt(0)); timeHoursString.setCharAt(1, hourString.charAt(1)); timeMinutesString.setCharAt(0, minuteString.charAt(0)); timeMinutesString.setCharAt(1, minuteString.charAt(1)); dateString.setCharAt(1, monthString.charAt(0)); dateString.setCharAt(2, monthString.charAt(1)); dateString.setCharAt(3, monthString.charAt(2)); dateString.setCharAt(5, domString.charAt(0)); dateString.setCharAt(6, domString.charAt(1)); /* if (curMonth < 10){ // This is always displayed in 3 letter characters, but could be in numbers monthString.setCharAt(0, '0'); // add a leading 0 monthString.setCharAt(1, tempMonthString.charAt(0)); } else { monthString.setCharAt(0, tempMonthString.charAt(0)); monthString.setCharAt(1, tempMonthString.charAt(1)); }*/ } int getDayOfWeek(int tempYear, int tempMonth, int tempDay){ // 1 <= m <= 12, y > 1752 (in the U.K.) Credit to Tomohiko Sakamoto via Wikipedia const int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; tempYear -= tempMonth < 3; return (tempYear + tempYear/4 - tempYear/100 + tempYear/400 + t[tempMonth-1] + tempDay) % 7; // 0 for Sunday, 1 for Monday etc. } void getInput(){ if (buttonLeft.update()){ if (buttonLeft.fallingEdge()) buttonLeftPressed = true; } if (buttonOk.update()){ if (buttonOk.fallingEdge()) buttonOkPressed = true; } if (buttonRight.update()){ if (buttonRight.fallingEdge()) buttonRightPressed = true; } } void flushInput(){ // clear any button flags that haven't been processed in the main loop if (buttonLeftPressed) buttonLeftPressed = false; if (buttonOkPressed) buttonOkPressed = false; if (buttonRightPressed) buttonRightPressed = false; } time_t getTeensy3Time() { return Teensy3Clock.get(); }
LED Display Library:
LedDisplay Library For details, see http://www.arduino.cc/playground/Main/LedDisplay This library allows you to send text to an Avago HCMS-29xx LED display. The HCMS 29xx displays are pretty little displays that contain a row of 5x7 LED matrices. The displays have a synchronous serial interface. You'll need five digital output lines to control them. The pins are as follows: * data - bits in from the microcontroller * register select - selects whether you're sending display data or control data * clock - timing clock from the microcontroller * enable - enables or disables the display * reset - resets the display The library manages all the necessary pin control and data shifting for you. Methods: LedDisplay(int dataPin, int registerSelect, int clockPin, int chipEnable, int resetPin, int displayLength) - instantiates the library. The first five parameters are the Arduino pin numbers that are connected to the display. The last sets the length of the display (8 for the HCMS-291x and HCMS-297x, 4 for the HCMS-290x and 296x). example: LedDisplay myDisplay = LedDisplay(2,3,4,5,6,8); begin() - initializes and resets the display. example: myDisplay.begin(); Printing methods write(char whatCharacter, byte whatPosition) - writes a single character to the display at a particular position example: myDisplay.write('A', 4); LedDisplay inherits all the print() and println() methods from the Print library, so you can use those methods to print to the display, as long as what you print fits within the display. For example here's how to print the millis and a title string to the display: myDisplay.print("ms:"); myDisplay.print(millis()); You can also do the usual Print things, like: int myInt = 12; myDisplay.print(myInt, DEC); // or myDisplay.print(myInt, HEX); Scrolling methods If you want to be able to automatically scroll a string of text, you need to use the setString() method first. This method stores a character string in the library instance, so you can then use the scroll() method to move it left and right. setString(char* stringToDisplay) - displays a string on the display. If the string is longer than the display, the beginning of the string is displayed. You can use the scroll() method to step the string forward on the display. getString() - returns the string to display. example: // print out the display string for debugging: Serial.println(myDisplay.getString()); stringLength() - returns the length of the display string. See the scrolling example below for usage. scroll(int direction) - scrolls the display using the string set by setString(). Negative numbers scroll left, positive numbers scroll right. example: ... void setup() { ... // scroll a string that's longer than the display: myDisplay.setString("This is a very long string"); } void loop() { // when the string scrolls off the display, reverse scroll direction. // On the right, it scrolls off at position 8. // on the left, it scrolls off when the cursor is less than -(the length of the string): if ((myDisplay.cursorPosition() > displayLength) || (myDisplay.cursorPosition() <= -(myDisplay.stringLength()))) { myDirection = -myDirection; delay(1000); } // scroll: myDisplay.scroll(myDirection); delay(100); } Cursor control methods clear() - clear the display. example: myDisplay.clear(); home() - set cursor to far left hand position. example: myDisplay.home(); setCursor(int whichPosition) - set cursor to any position. example: myDisplay.serCursor(4); getCursor() - get the cursor position. example: int cursorPosition = myDisplay.getCursor(); Display Control methods setBrightness(int bright) - lets you set the brightness from 0 to 15. example: myDisplay.setBrightness(15); If you want to set the opcodes of the display directly (you can learn them from the data sheet), the following methods will do the trick. loadControlRegister(int dataByte) - sends 8 bits to one of the control registers. loadDotRegister() - sends 320 bits to the dot register Version method version() - returns the version number of the library.