Edits on the 'stopwatch" project (#1210)

This commit is contained in:
Galen Nickel 2018-09-11 13:10:34 -07:00 committed by Peli de Halleux
parent e77e43d970
commit 3cd4b6e6bc

View File

@ -1,29 +1,44 @@
# Stop watch # Stopwatch
![A @boardname@ stop watch toon image](/static/mb/projects/stop-watch.png) ![A @boardname@ stopwatch toon image](/static/mb/projects/stop-watch.png)
This project turns the @boardname@ into a simple stop-watch. You can use it the [Watch project] This project turns the @boardname@ into a simple stopwatch. Pressing **A** will start the timer. Pressing **A** again will show the amount of elapsed time and reset the timer. Let's get started.
if you've build it already. Pressing **A** will start the timer
and pressing **A** again will display the resulting time. Let's get started. ## ~ hint
If you built a watch in the [make](/projects/watch/make) portion of the of the [Watch](/projects/watch) project, you can use the code from this project with it too.
## ~
## Program state ## Program state
The stop watch has two states: it is either stopped or measuring time. When coding we often use the idea of _state_ to decide what action needs to happen next. The state of the program is the condition or situation of the program depending on what events it's concerned about. It may decide to remain in its current state or take an action to change the state.
Let's create a new variable ``start_time`` to track the state:
* if ``start_time`` is equal to ``0``, the watch is stopped. We do this in real life too. For example, if you feel hungry, your present state is **hunger**. Eating a meal will change your state from **hunger** to **satisfied**. Also, if you're thirsty, drinking enough water might change your state from **thirst** to **satisfied**. But, if you wait too long to eat or drink, you're state could change back to **hunger** or **thirst**.
* if ``start_time`` is **not** equal to ``0``, the watch is running and the value of ``start_time`` is
the time it started. The stopwatch has two states: it is either **(1) stopped** or **(2) measuring time**. Let's create a new variable ``start_time`` to track the state:
* if ``start_time`` is equal to ``0``, the watch is stopped - **state 1**
* if ``start_time`` is **not** equal to ``0``, the watch is running and the value of ``start_time`` is the time recorded when started - **state 2**
## Pseudo code ## Pseudo code
The rough outline of the code is as follows: when a user presses **A**, we start by determining in which This is the _rough_ outline of the code:
state we are. If the watch is stopped (``start time`` is 0), we start the counter and store the current time. If the watch is running (``start time`` is not 0), we compute the duration and reset ``start time`` to 0.
The ``||input:runningTime||`` block returns the number of **milli**-seconds elapsed since the @boardname@ was turned on. To compute the duration in seconds, we use the following formula: >When a user presses **A**, we start by determining which state we're in.
duration in seconds = (current time - start time) / 1000.0 >If the watch is stopped, (``start time`` is 0 and this means we're in **state 1**), we start the counter and store the current time. The watch now goes from **state 1** to **state 2**.
>If the watch is running (``start time`` is not 0 and this means we're in **state 2**), we calculate the time duration and reset ``start time`` to 0. The watch goes back from **state 2** to **state 1**.
The ``||input:running time||`` block returns the number of **milli**-seconds elapsed since the @boardname@ was turned on. To compute the duration in seconds, we use the following formula:
```
duration in seconds = (current time - start time) / 1000.0
```
In pseudo code, it could look like this:
In pseudo code, this could look like this:
``` ```
on button pressed on button pressed
if start time is 0 if start time is 0
@ -33,6 +48,8 @@ on button pressed
reset start time reset start time
``` ```
## Real code
If you translate the pseudo-code line by line into blocks, it might end up like this. If you translate the pseudo-code line by line into blocks, it might end up like this.
```blocks ```blocks