Stopwatch
Overview
Here is a two-button stopwatch that uses the OLED display. The first button is called the Start/Stop button. When you first press start it starts to add up the time since it was pressed. Pressing it again will stop collecting time but still remember the total accumulated time while the stopwatch was running.
The second button is the Reset button. When you press Reset the total time accumulated will be reset back to 0.
The MicroSimulation
Program Structure
Let me break down this stopwatch program in a way that's easy to understand!
Let's think of this like building a digital stopwatch step by step:
STEP 1 - The Parts ("Hardware Setup")
- We're using a small computer called a Raspberry Pi Pico
- It has two buttons connected to it: one for Start/Stop and one for Reset
- There's a small screen (called an OLED display) that shows us the time
- The display connects to the Pico using special pins, like plugging in a cable
STEP 2 - Setting Up Our Variables ("Global Variables")
- Think of these like creating storage boxes for important information
- We have boxes for:
- Whether the stopwatch is running or stopped
- When we started timing
- How much time has passed in total
- When we last pressed a button (to avoid accidental double-presses)
STEP 3 - The Start/Stop Button Function
- When you press the Start/Stop button:
- If the stopwatch was stopped → it starts running and remembers the start time
- If it was running → it stops and saves how much time has passed
- It's like pressing the start/stop on a real stopwatch!
STEP 4 - The Reset Button Function
- When you press Reset:
- It stops the stopwatch
- Sets all the saved time back to zero
- Like clearing your stopwatch to start fresh
STEP 5 - Making Time Look Nice ("format_time")
- This part takes the raw time (which is in milliseconds) and makes it look readable
- Instead of showing "12500 milliseconds", it shows "00:12.500" (12.5 seconds)
- It's like converting 60 minutes into 1 hour - just more precise!
STEP 6 - Updating the Display
- This happens many times per second
- If the stopwatch is running:
- It takes the current time and subtracts the start time
- Adds any time saved from previous runs
- Shows this on the screen
- If the stopwatch is stopped:
- It just shows the saved time
STEP 7 - The Main Program Loop
- This is like the heartbeat of the program
- Every 1/10th of a second:
- It checks if any buttons were pressed
- Updates the time on the display
- Keeps everything running smoothly
Think of it like this: Imagine you're timing a race with a regular stopwatch. You: 1. Press start when the race begins (Start button) 2. Press stop when someone finishes (Stop button) 3. Can pause and restart for multiple racers (Start/Stop button again) 4. Clear it to zero for a new race (Reset button)
This program does all that, but digitally! The main difference is that it's much more precise - it can measure down to milliseconds (thousandths of a second), which would be impossible with a regular handheld stopwatch.
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|