Charles Tritt
/
WhatTime
Sends the interval between user button press and release via serial connection to PC. Uses a timer.
main.cpp@0:7931706dde3d, 2017-09-17 (annotated)
- Committer:
- CSTritt
- Date:
- Sun Sep 17 12:37:43 2017 +0000
- Revision:
- 0:7931706dde3d
My initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:7931706dde3d | 1 | /* |
CSTritt | 0:7931706dde3d | 2 | Project: WhatTime |
CSTritt | 0:7931706dde3d | 3 | File: main.cpp |
CSTritt | 0:7931706dde3d | 4 | |
CSTritt | 0:7931706dde3d | 5 | Uses a timer to display the interval between button press and release. |
CSTritt | 0:7931706dde3d | 6 | |
CSTritt | 0:7931706dde3d | 7 | Created 12 Aug 2017 by Sheila Ross |
CSTritt | 0:7931706dde3d | 8 | Last modified 9/17/17 by C. S. Tritt |
CSTritt | 0:7931706dde3d | 9 | */ |
CSTritt | 0:7931706dde3d | 10 | #include "mbed.h" |
CSTritt | 0:7931706dde3d | 11 | |
CSTritt | 0:7931706dde3d | 12 | // Construct a USER_BUTTON digital input. |
CSTritt | 0:7931706dde3d | 13 | DigitalIn myButton(USER_BUTTON); |
CSTritt | 0:7931706dde3d | 14 | |
CSTritt | 0:7931706dde3d | 15 | // Construct a timer object. |
CSTritt | 0:7931706dde3d | 16 | Timer myTimer; |
CSTritt | 0:7931706dde3d | 17 | |
CSTritt | 0:7931706dde3d | 18 | // Construct a transmit only serial connection over our USB. |
CSTritt | 0:7931706dde3d | 19 | Serial pc(USBTX, NC, 9600); |
CSTritt | 0:7931706dde3d | 20 | |
CSTritt | 0:7931706dde3d | 21 | |
CSTritt | 0:7931706dde3d | 22 | int main() |
CSTritt | 0:7931706dde3d | 23 | { |
CSTritt | 0:7931706dde3d | 24 | myTimer.start(); // Start the timer. |
CSTritt | 0:7931706dde3d | 25 | |
CSTritt | 0:7931706dde3d | 26 | while(true) { // Main loop. |
CSTritt | 0:7931706dde3d | 27 | if (myButton == 0) { // Button is pressed (active low). |
CSTritt | 0:7931706dde3d | 28 | // Save the time on timer. |
CSTritt | 0:7931706dde3d | 29 | float current_time = myTimer.read(); |
CSTritt | 0:7931706dde3d | 30 | |
CSTritt | 0:7931706dde3d | 31 | // Send the time as text via the serial port. |
CSTritt | 0:7931706dde3d | 32 | pc.printf("Time difference %f seconds.\n",current_time ); |
CSTritt | 0:7931706dde3d | 33 | |
CSTritt | 0:7931706dde3d | 34 | // Reset the timer. |
CSTritt | 0:7931706dde3d | 35 | myTimer.reset(); |
CSTritt | 0:7931706dde3d | 36 | |
CSTritt | 0:7931706dde3d | 37 | // Wait for the user to release the button. |
CSTritt | 0:7931706dde3d | 38 | while(myButton == 0) { |
CSTritt | 0:7931706dde3d | 39 | } |
CSTritt | 0:7931706dde3d | 40 | } |
CSTritt | 0:7931706dde3d | 41 | } |
CSTritt | 0:7931706dde3d | 42 | } |