From 221d84d3bf6f86e845d1ef6c0b2615f846ffa44c Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 24 May 2017 08:47:19 -0700 Subject: [PATCH] added stopwatch --- docs/examples.md | 5 +++++ docs/examples/stop-watch.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 docs/examples/stop-watch.md 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