diff --git a/docs/examples.md b/docs/examples.md index f8c797dc..9a32fcf8 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -50,6 +50,11 @@ Here are some fun programs for your @boardname@! "description": "chart analog input on the LED screen", "url":"/examples/plot-analog-pin", "cardType": "example" +}, { + "name": "Stop Watch", + "description": "easy time tracking", + "url":"/examples/stop-watch", + "cardType": "example" }] ``` diff --git a/docs/examples/stop-watch.md b/docs/examples/stop-watch.md new file mode 100644 index 00000000..a9aeb1fc --- /dev/null +++ b/docs/examples/stop-watch.md @@ -0,0 +1,30 @@ +# Stop watch + +Press ``A`` to start the counter and + +```blocks +let msec = 0 +let sec = 0 +let end = 0 +let d = 0 +let start = 0 +input.onButtonPressed(Button.A, () => { + if (!(start)) { + start = input.runningTime() + end = 0 + } else { + d = input.runningTime() - start + start = 0 + basic.clearScreen() + basic.pause(1000) + sec = d / 1000 + msec = d % 1000 + basic.showString("" + sec + "." + msec) + } +}) +basic.forever(() => { + if (start) { + led.toggle(Math.random(5), Math.random(5)) + } +}) +``` \ No newline at end of file