Code Monkey home page Code Monkey logo

microbit-pxt-timeanddate's Introduction

time-and-date

microbit-pxt-timeanddate=github:bsiever/microbit-pxt-timeanddate

This extension allows the micro:bit to keep track of the time and date. It can also be used for primitive stopwatch-like capabilities.

It's importantant to be aware that:

  • This extension uses a counter with differing accuracy on the v1 and v2 micro:bits:
    • The v1 stability is about 10 parts per million, which is approximately 0.864 seconds per day and
    • The v2 stability is 50 parts per million, which is about 4.32 seconds per day.
  • The accuracy may change based on the environment (heat/cold) and from micro:bit to micro:bit.
  • If accuracy is important, you can use the stopwatch experiment described below to estimate the accuracy of your micro:bit where you plan to use it.
  • The time needs to be set each time the micro:bit is reprogrammed or restarts.

Compatability

  • Bluetooth is not currently supported on v1 (it is on v2)
  • Some v2 features, like low power mode or playing a melody continuously, may interfer with time keeping.

Setting the Time #setting-the-time

There are three common approaches to setting the time:

  1. Synchronize at startup (easiest, but requires updating the program)
  2. Time advancing / rewinding
  3. Digits count up / count down

Using a reasonable "startup value", as described in Synchronize at startup, will make the last two approaches easier.

1. Synchronize at startup #1-synchronize-at-startup

Synchronizing the time at startup is the easiest approach, but it requires re-programming the micro:bit every time the time needs to be set, like whenever it is restarted. The start up process just needs to include setting the time, like:

timeanddate.setDate(1, 20, 2022)
timeanddate.set24HourTime(13, 30, 0)

~hint

Date is Optional!

If you just care about time and not the date, you don't have to set the date.

~

Once you're ready to program the micro:bit:

  1. Update the time/date being used so the time is approximately 1 minute in the future.
  2. Program the micro:bit.
  3. Watch the real time carefully.
  4. About 1-2 seconds before the programmed time, press the reset button on the back of the micro:bit.
    • The micro:bit takes about 1-2 seconds to restart. This causes the "set" block to run at the correct time.

For the example above, the micro:bit would be reset at 13:29.58s on Jan. 20, 2022. It would set the date and time at almost exactly the time indicated in the set block.

2. Time advancing / rewinding #2-time-advancing-rewinding

This is the approach used by mechanical clocks, where time is set by moving the minute hand forward (or, possibly, backwards). Moving the minutes forward may cause the hours to change too. And as hours change the date could change, etc.

This is a tedious way to set dates and should probably only be used when the date will never be needed and just the time needs to be set.

2.1 Simplest Approach

Here's the simplest approach, where the buttons are dedicated to setting the time :

input.onButtonPressed(Button.A, function () {
    timeanddate.advanceBy(1, timeanddate.TimeUnit.Minutes)
})

input.onButtonPressed(Button.B, function () {
    timeanddate.advanceBy(-1, timeanddate.TimeUnit.Minutes)
})

The buttons change the time one minute at a time (forward or backward). Advancing the minutes beyond 59 or before 0 will cause the hour to change.

2.2 Alternate Approach: Setting at startup

Here's an alternate approach that can be used to set the time at startup by advancing hours and minutes.

  1. Hold "A" until the correct hour is shown.
  2. Hold "B" until the message says "min:".
  3. Hold "A" until the correct minute is shown.
  4. Hold "B" again.
input.onButtonPressed(Button.A, function () {
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMM))
})
basic.showString("hr:")
while (!(input.buttonIsPressed(Button.B))) {
    if (input.buttonIsPressed(Button.A)) {
        timeanddate.advanceBy(1, timeanddate.TimeUnit.Hours)
    }
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMM))
}
basic.showString("min:")
while (!(input.buttonIsPressed(Button.B))) {
    if (input.buttonIsPressed(Button.A)) {
        timeanddate.advanceBy(1, timeanddate.TimeUnit.Minutes)
    }
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMM))
}

If setting the date this way it's best to use the [timeanddate.setDate()] to select a year near the current year.

This may not work correctly when the total time is before the year specified in [timeanddate.setDate()]. That is, if [timeanddate.setDate(1, 20, 2024)] specifies 2024, then negative values should not "rollback" before Jan 1, 2024.

3. Digits count up / count down #3-digits-count-up-count-down

This approach should be done on each digits of the time (minutes, hours, and if the date is important too, day, month, and year).

3.1 Simplest Approach Example

Here's an example that focuses on just the minutes (additional code is needed for hours, etc.):

input.onButtonPressed(Button.B, function () {
    timeanddate.numericTime(function (hour, minute, second,  month, day, year) {
        timeanddate.set24HourTime(hour, minute + -1, second)
    })
})

input.onButtonPressed(Button.A, function () {
    timeanddate.numericTime(function (hour, minute, second,  month, day, year) {
        timeanddate.set24HourTime(hour, minute + 1, second)
    })
})

Since the time setting commands use modular arithmetic, adding and subtracting to the prior value will "rollover" in the expected way and will not impact the hours, like the advancing approach would.

3.2 Alternate Approach: Setting Hour/Minute at Start

The following can be used to set the time at startup. It will scroll the time continuously until you are done setting it.

  1. Set the Hour by holding "A" to advance it or "B" to decrease it.
  2. Hold "A+B" until it says "Set Min".
  3. Then repeat the process for the minute.
  4. When done hold "A+B" until the message stops scrolling.

Once the time has been set the additional code that displays the time when "A" is pressed.

input.onButtonPressed(Button.A, function () {
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMMAMPM))
})
timeanddate.setTime(11, 30, 0, timeanddate.MornNight.AM)
basic.showString("Set Hour")
while (!(input.buttonIsPressed(Button.AB))) {
    timeanddate.numericTime(function (hour, minute, second, month, day, year) {
        if (input.buttonIsPressed(Button.A)) {
            timeanddate.set24HourTime(hour + 1, minute, 0)
        } else if (input.buttonIsPressed(Button.B)) {
            timeanddate.set24HourTime(hour - 1, minute, 0)
        }
    })
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMMAMPM))
}
basic.showString("Set Min")
while (!(input.buttonIsPressed(Button.AB))) {
    timeanddate.numericTime(function (hour, minute, second, month, day,  year) {
        if (input.buttonIsPressed(Button.A)) {
            timeanddate.set24HourTime(hour, minute + 1, 0)
        } else if (input.buttonIsPressed(Button.B)) {
            timeanddate.set24HourTime(hour, minute - 1, 0)
        }
    })
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMMAMPM))
}

Full API

Setting Time Using 24-hour Format #timeanddate-set24hourtime

Use [timeanddate.set24HourTime()] to set the time using 24-hour format (hours from 0-23, minutes from 0-59, seconds from 0-59). See Setting the Time.

Setting Time Using AM/PM Format #timeanddate-settime

Use [timeanddate.setTime()] to set the time using am/pm format (hours from 1-12, minutes from 0-59, seconds from 0-59). See Setting the Time.

Setting the Date #timeanddate-setdate

Use [timeanddate.setDate()] to set the date (month from 1-12, day from 1-31, year from 0000-9999). See Setting the Time.

Getting the Time (string) #timeanddate-time

timeanddate.time(timeanddate.TimeFormat.HMM)

Provides the current time in the selected format.

If no time has been set, the time will be based on when the micro:bit started (was reset). By default time starts at 00:00.00 on 0000-01-01.

Getting the Date (string) #timeanddate-date

timeanddate.date(timeanddate.DateFormat.MD)

Provides the current date in the specified format.

If no date is set, it will start at 0000-01-01 when the micro:bit starts.

~alert

Avoid using separate time and date if making a log of events

Retrieving the time and date separately can cause problems. For example, if the date is accessed at 23:59.59 and then time is then accessed, the time may have changed to 00:00.00 (the following day) and the date won't be correct for the time.

Use the timestamp or the [timeanddate.numericTime()] block to get numeric values for the date.

~

Timestamps (string) #timeanddate-datetime

Often a "timestamp" is needed to record events, like when measurements are taken for a science experiment. Use:

timeanddate.dateTime()

It provides the date and time in the format: YYYY-MM-DD HH:MM.SS. This format can easily be "sorted" in spreadsheets. This ensures that the date and time are retrieved at the same time (accessing them separately may lead to a date after the time if the the time is checked at almost exactly the end of the day). See Setting the Time.

~hint

When to use Timestamps

Use timestamps when logging events that happen over multiple days (more than 24 hours). For example, an experiment recoding temperature every 10 minutes for a week should use timestamps.

~

Accessing numeric values of time / date #timeanddate-numerictime

Numeric values of time/date can be useful for things like alarm clocks. Use:

timeanddate.numericTime(function (hour, minute, second, month, day, year) {
})
  • Hour will be 0-23 (in 24-hour format)
  • Minute will be 0-59
  • Second will be 0-59.
  • Day will be 1-31
  • Month will be 1-12
  • Year is the year 0-65535

~tip

Numeric Time vs. separate hour, minute, second

The [timeanddate.numericTime()] avoids errors that can occur if separate things had been used to get each part of the time. For example, if one block provided the current time of day and another block provided the date, it would be possible to access the time at 23:59.59 on 2020-01-01 and then the date a fraction of time later, when it had changed to 2020-01-02. The combined date and time would appear to be 23:59.59 on 2020-01-02 even though the 23:59.59 was actually on 2020-01-01.

~

Advancing and Adjusting Time #timeanddate-advanceby

timeanddate.advanceBy(amount: number, unit: TimeUnit)

Can be used to advance or set back the current time. It works by adding time to the current time in the given units, with carries. Adding to minutes may cause an overflow and change the hours. Negative amounts can be used to set the clock backwards, however the clock can not be set back before the beginning of the year originally used to set the date (or year 0 if the date wasn't set).

It behaves like a mechanical clock: Advancing the minutes causes the hour to advance as well. It may be useful for adjusting the clocks accuracy (adding seconds occasionally if it's slow) or adjusting for changes in timezone or daylight savings time.

Seconds the Micro:bit has been running #timeanddate-secondssincereset

timeanddate.secondsSinceReset()

Reports the time (in seconds) since the micro:bit started.

Minute Changing #timeanddate-onminutechanged

timeanddate.onMinuteChanged(handler: () => void)

The provided code will run when the minute changes.

Hour Changing #timeanddate-onhourchanged

timeanddate.onHourChanged(handler: () => void)

The provided code will run when the hour changes.

Day Changing #timeanddate-ondaychanged

timeanddate.onDayChanged(handler: () => void)

The provided code will run when the day changes.

Weekdays #timeanddate-datetodayofweek

Once you have the day, month, and year, you can use them on the:

timeanddate.dateToDayOfWeek()

to determine the day of the week. It returns a numeric code, where 0 is Monday, 1 is Tuesday, etc.

Day of the Year #timeanddate-datetodayofyear

Once you have the day, month, and year, you can use them on the:

timeanddate.dateToDayOfYear()

to determine the ordinal day of the year. 1 is Jan. 1, 2, is Jan 2, etc. Dec. 31 is either 365 or 366 (in a leap year).

Measuring Accuracy and Calibrating #measuring-accuracy-and-calibrating

Accuracy can be estimated by comparing your micro:bit to an actual, accurate stopwatch.

Use the following program, which will start the time at 00:00.00 (just like a stop watch).

input.onButtonPressed(Button.A, function () {
    basic.showString(timeanddate.dateTime())
})
input.onButtonPressed(Button.B, function () {
    basic.clearScreen()
})
basic.showIcon(IconNames.Heart)
timeanddate.set24HourTime(0, 0, 0)

Use a stopwatch or stopwatch app on a computer/phone. To start measurement:

  1. To estimate the accuracy of the micro:bit you'll need to compare it's estimate of time to the stopwatch for a few hours (or even days). Be sure that it's set up someplace where it will have power continuously, either via battery or a USB cable, for a long time (hours or days). Some computers will suspend power to USB devices if the computer isn't in use for a while, so you may want to use a USB charger.
    • The micro:bit's accuracy can be impacted by large changes in temperature. It's best to test it in an environment that you'll be using it in. That is, if it'll be left outside in cold weather, test the accuracy in similar conditions.
  2. Prepare to start the stopwatch
  3. Reset the micro:bit
  4. As soon as you see the heart on the micro:bit start the stopwatch

The micro:bit should be accurate to within about 1 second per day or better. This experiment depends on your reaction time, which is probably about 0-2 seconds. Consequently you may need to let it run for several days to estimate the accuracy.

To estimate the accuracy:

  1. Look at the stopwatch. Pick a precise instant that you'll take the measurement and keep it in mind. For example, if the stop watch reads 2:13.20 you may decide to make the measurement at 2:14.00.
  2. At that precise instant press the "A" button. The micro:bit will scroll it's current record of the time. Compare it to the expected time. For example, if the microbit indicated 0000-01-01 02:13.22 you'd estimate that the microbit is about 2 seconds ahead. Since it's been running for 134 minutes, you'd estimate that it's gaining 2/134 seconds/minute. Since there are 1440 minutes in a day, this works out to about 21.5 seconds per day.

~tip

Improving Accuracy

If you measure the accuracy and it's consistent/predictable, you may be able to use the [timeanddate.advanceBy()] and [timeanddate.onHourChanged()] blocks to periodically adjust the accuracy.

Be careful setting time backward while using [timeanddate.onHourChanged()]! It's possible to get stuck in a "loop" that continually resets the time. You may need to use a variable to identify which "hour" was the last one to be adjusted.

~

Stopwatch behavior #stopwatch-behavior

By use of setting time to 0:0.0 this can be used as a simple stopwatch. For example, for timing things that are less than 24 hours:

input.onButtonPressed(Button.A, function () {
    timeanddate.set24HourTime(0, 0, 0)
})
input.onButtonPressed(Button.B, function () {
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HHMMSS24hr))
})
timeanddate.set24HourTime(0, 0, 0)

"A" starts counting and "B" shows the time elapsed since "A" was pressed (or the start)

Challenges!!!

  1. Find new ways to set the time or date.
  2. Create an alarm clock! (Hint: Use [timeanddate.numericTime()])
    • Start with a simple alarm that is pre-programmed. For example, the alarm will go off at 3pm each day. The [timeanddate.numericTime()] uses 24-hour format time, so 3pm is 15:00.
  3. Add a snooze feature to your alarm clock!
  4. Create an adjustable alarm clork that allows the alarm time to be changed by using buttons or actions (rather than reprogramming)
  5. Create a clock without these blocks! Hints: You'll need a few counter variables, a forever loop, and the ability to [basic.pause()].

Bonus: A Binary Clock

Here's a simple clock that will show time in binary code. Each column represents a digit of the current 12-hour time. The bottom most LED in each column is the 1's digit, the second from the bottom is the 2's, etc. The middle column of the display will blink a pattern off and on to indicate each second.

Click here to open the shared project

function binaryDisplayOf (num: number, col: number) {
    for (let index = 0; index <= 4; index++) {
        if (Math.idiv(num, 2 ** index) % 2 == 1) {
            led.plot(col, 4 - index)
        } else {
            led.unplot(col, 4 - index)
        }
    }
}
input.onButtonPressed(Button.B, function () {
    timeanddate.advanceBy(1, timeanddate.TimeUnit.Minutes)
})
input.onButtonPressed(Button.A, function () {
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMM))
})
input.onButtonPressed(Button.AB, function () {
    timeanddate.advanceBy(15, timeanddate.TimeUnit.Minutes)
})
basic.showString("hr:")
while (!(input.buttonIsPressed(Button.B))) {
    if (input.buttonIsPressed(Button.A)) {
        timeanddate.advanceBy(1, timeanddate.TimeUnit.Hours)
    }
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMM))
}
basic.showString("min:")
while (!(input.buttonIsPressed(Button.B))) {
    if (input.buttonIsPressed(Button.A)) {
        timeanddate.advanceBy(1, timeanddate.TimeUnit.Minutes)
    }
    basic.showString(timeanddate.time(timeanddate.TimeFormat.HMM))
}
let blink = false
basic.forever(function () {
    timeanddate.numericTime(function (hour, minute, second, month, day, year) {
        hour = 0 % 12
        if (hour == 0) {
            hour = 12
        }
        binaryDisplayOf(Math.idiv(hour, 10), 0)
        binaryDisplayOf(hour % 10, 1)
        binaryDisplayOf(Math.idiv(minute, 10), 3)
        binaryDisplayOf(minute % 10, 4)
    })
    basic.pause(1000)
    blink = !(blink)
    if (blink) {
        binaryDisplayOf(10, 2)
    } else {
        binaryDisplayOf(0, 2)
    }
})

Bonus: A Stopwatch

Here's a simple stopwatch. "A" starts and stops the count. The count will be automatically displayed when it's stopped, but pressing "B" will re-display it.

Click here to open the shared project

input.onButtonPressed(Button.A, function () {
    running = !(running)
    if (running) {
        timeanddate.set24HourTime(0, 0, 0)
    } else {
        captured = timeanddate.time(timeanddate.TimeFormat.HHMMSS24hr)
        basic.showString(captured)
    }
})
input.onButtonPressed(Button.B, function () {
    if (!(running)) {
        basic.showString(captured)
    }
})
let dotLocation = 0
let xy = 0
let captured = ""
let running = false
running = false
let coords = [11, 21, 31, 32, 33, 23, 13, 12]
basic.forever(function () {
    if (running) {
        basic.clearScreen()
        xy = coords[dotLocation]
        led.toggle(Math.idiv(xy, 10), xy % 10)
        basic.pause(1000/8)
        dotLocation = (dotLocation + 1) % 8
    } else {
        basic.showIcon(IconNames.No)
    }
})

Misc Links & References

Supported targets

  • for PXT/microbit
<script src="https://makecode.com/gh-pages-embed.js"></script> <script>makeCodeRender("{{ site.makecode.home_url }}", "{{ site.github.owner_name }}/{{ site.github.repository_name }}");</script>

microbit-pxt-timeanddate's People

Contributors

bsiever avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bluebaritone21

microbit-pxt-timeanddate's Issues

Compatibility with micro:bit v2

Hi @bsiever ,

As you might have seen from microsoft/pxt-microbit#3506 this extension is currently not compiling against the latest version of Micro:bit CODAL. This means it will not work with micro:bit V2.

Software for micro:bit V2 boards is built using CODAL rather than the DAL, and no longer includes the mbed layer. We’ve implemented the most frequently used mbed classes to support easy porting of existing extensions, but there may still be corner cases with this compatibility layer.

As your extension is currently not building, it will be disabled for micro:bit V2. People attempting to use your extension with a micro:bit V2 board will get error 929. This extension will only be re-enabled on micro:bit V2 once it is fixed.

There are details on how to ensure extensions work on both version of the micro:bit here (lancaster-university/codal-microbit-v2#31).

Looking at your extension source I believe that your extension falls under this category:

  1. Use of mbed APIs that are not included in the compatibility layer, or microbit-dal APIs that need different constructors
/home/build/prjclone/pxtapp/microbit-pxt-timeanddate/timeanddate.cpp:61:28: error: 'us_ticker_read' was not declared in this scope

Missing Features

  • C code for more accurate clock / shim (test via disable behavior of shim temporarily)
  • Code for callbacks!
  • Review / update formats being used for strings returned
  • Testing, Testing, Testing (with radio and bluetooth)
  • Update comments and formatting / help
  • Tutorial / Docs / README.md updates
  • Icon
  • License
  • Use "pxt bump" to create a tagged release on GitHub
  • Get your package reviewed and approved https://makecode.microbit.org/extensions/approval

TODOs

  1. Consider replacing the forever loop (here) with a everyInterval() or some other variation that runs in background. (everyInterval() in eventually delegates to runInParallel (here), which may not be any better/different. Review.
  2. Add in a daily correction factor (# of ms to adjust +/- per day?). Maybe do this by modular updates to skip/freeze a second? Or just at single time per day? Prefer the continual approach.
  3. Update the event handler mechanism (here to be less wonky. Use event that are registered with DAL/CODAL? See regiterWithDAL() examples, like here and corresponding MicroBitEvent calls.

Open questions

Docs / Examples at: https://bsiever.github.io/microbit-pxt-softclock/

General features

  1. What other time/date formats should be included?
    1. Current ones may be common uses for U.S. notation, but what about international?
  2. Currently I'm ignoring the special cases for leap years at centuries, although including them is just another couple of mods and and AND statement. (Code is there/correct, just commented out). It seems like the major use-cases are "calendar from 0" and "calendar from now". Should I bother with the special cases for leap years? Eh...Decided it wasn't significant.
  3. Should I include anything that translates to weekday abbreviations or names ("Mon", "Tues", etc.) and month abbreviations/names? Or is that an exercise for the user? (Decided on "exercise for the user")
  4. The "stopwatch" behavior could be handy. Should I update the package to highlight this a bit more? Added completed example.
  5. I could expose access to milli-seconds, but I didn't think it'd be useful generally. Any thoughts? Decided accuracy undermined utility.
  6. How's the Palette color? I just used a default from the playground and am open to suggestions. Calling it done.
  7. Anything else? :)

Package Prep

  1. I should probably add a comment that indicates this assumes the micro:bit sleeps "regularly". (Time won't be kept if a background task can't run at least every 512s or so. The "minute changed" update requires updates at least once a minute)
  2. Action call backs: Is there a better approach than storing them like this?
    1. What about re-entrant? If there's a "on next minute" that includes a 2 minute sleep, will multiple calls be queued?
  3. How can I improve documentation? See: https://bsiever.github.io/microbit-pxt-softclock/ (Probably, but calling it good here for now)~~
  4. The block and in-line block environments aren't working, but blocks does. Just the attempts to show reporter blocks in isolation are failing.
  5. Should I use a docs directory?
  6. Blocks don't always seem to render correctly. Suggestions?
  7. Add tutorial?
  8. Is there a better way to do string formatting (padding with zeros) than the current approach, which calls leftZeroPadTo() in main.ts.
  9. Python support check? (looks ok so far)?
  10. Any guidance on test.ts for this? Or examples of effective tests are included in comparable types of extensions?
  11. Is there anything I can do to improve internationalization efforts?

Typescript vs. C++ & Performance Questions

  1. How does C++ performance/memory compare to TS. Should I move all the code to C++ with shims for all exported blocks?
    1. If I go to all C++, I've got blocks (shims) that call helper methods. Do I need to mark the helper methods as shims in my typescript to avoid compiling them? Or are unused typescript functions omitted from the executable?
  2. In typescript I'm using constants like 60*60 (for seconds in hour). I commonly do this in C++ for readability and know that the compiler will simplify. Will typescript or should I just use the final value?
  3. In typescript I'm using a const cdoy: uint16[] = [ ... ]. Will this consume ram or go to code space? (I'd want code space)

Return time: loop until good

Update the cpu time function to return the correct time via a loop (rather than using the old time when there's a failure)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.