We are going to start a project: develop a digital watch. This is actually going to be a multi-session project that we can expand as much as we like (depending on the level of attending students).
We will start with the easiest solution. Once that is working, we will ask ourselves if there are any features our watch is missing and how we can implement them. Then we will start the process all over again.
This approach makes the project adaptable to different levels of knowledge: the younger kids can do only the initial part and still have a working project, while the older ones can go deeper and learn more in the process.
1. Display the hour and minutes when a button is pressed
Pretty simple! The Forever loop keeps updating the time every second and pressing button_A displays the hour and minute.
What can be improved?
- add a “0” in front of single digit minutes (ex: instead of “14:2” display “14:02”)
- time setting is difficult: one has to change the values of variables “hours”, “minutes” and “seconds”, then download the .hex file once again and copy on the micro:bit;
- reading the time takes several seconds and that is not very practical!
2. Add a “0” in front of single digit minutes
This is the only place where the program changes from the original version.
3. Use buttons to set the time
We are going to change the program so that setting of time will be done using the buttons: button_A to change the hour and button_B to change the minute. Then use “on shake” event to display the time.
Note that for each press of the buttons we need to make sure that the hour and minute are in range; otherwise we could go over the limit and the the program would not see this anomaly until the program in the forever loop would check (every minute for minutes and every hour for hours).
4. Change the display format
In order to display the time in such a manner that the whole time is visible at once (without waiting for the characters to scroll). In order to achieve such a goal, a different system for displaying the time should be considered. For example: https://blog.tokyoflash.com/2012/07/30/binary-led-watch-minimal-sleek-and-geeky/
Binary system
Numbers can be represented with “0” and “1” as such:
0 - 0000, 1 - 0001, 2 - 0010, 3 - 0011 etc (see more at https://www.mathsisfun.com/binary-number-system.html)
So, in order to display the hour, we would use the first column of LEDs, starting from the bottom LED (that being the least significant bit) going up. We would use four LEDs, thus being able to display all hours from 1 to 12.
For minutes we would use next two columns: the second and the third column on the display. On the second column we would display tens of minutes and on the third - units of minutes. The largest value for minutes is 59 so for the tens of minutes we use only 3 LEDs and for the units we use only 4 LEDs.
Now, the display works well. We can see the time at once (no scrolling delay). However, the program is long and hard to maintain. Can we make it shorter?
In order to answer this question we need to understand how a decimal number get converted into its binary representation.
How is a decimal number transformed into binary? By dividing the number to 2 and using the remainders as bits.
For example: 19 is 10011.
Step 1: 19 / 2 = 9 remainder is 1
Step 2: 9 / 2 = 4 remainder is 1
Step 3: 4 / 2 = 2 remainder is 0
Step 4: 2 / 2 = 1 remainder is 0
Step 5: 1 / 2 = 0 remainder is 1
So, the first remainder is the last bit (also called LSB - least significant bit). The second remainder is the next to last bit and so forth. We keep dividing until the quotient is 0. The first bit (also called MSB - most significant bit) is remainder of the last division.
It is implemented in the program below:
How do we implement this in the watch project?
Are there more steps to take in order to improve our watch?
I think so. Some possible options:
- We could display seconds on the last two columns of LEDs.
- We could add an alarm.
- We could add date (month and day of month) on the last two last columns of LEDs. If we do this, then we have to develop a system of setting the date using the buttons. So, we could use button A to set the mode (for example: hour setting mode, minute setting mode, month setting mode, day setting mode, alarm hour setting mode, alarm minute setting mode, normal mode). Then we could use button B to increase the value dictated by the current mode set at the previous mode.