Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of analogRead by
main.cpp@7:a0da6751e601, 2019-03-28 (annotated)
- Committer:
- CSTritt
- Date:
- Thu Mar 28 17:47:36 2019 +0000
- Revision:
- 7:a0da6751e601
- Parent:
- 6:f05a4d991702
Updated library for 2019.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| CSTritt | 0:2254358fce87 | 1 | /* |
| CSTritt | 3:1cff703d6eb4 | 2 | Project: CE_Temp_Read |
| CSTritt | 0:2254358fce87 | 3 | File: main.cpp |
| CSTritt | 0:2254358fce87 | 4 | |
| CSTritt | 0:2254358fce87 | 5 | Reads from analog input, streams ASCII text to std serial using printf and |
| CSTritt | 6:f05a4d991702 | 6 | lights onboard LED. |
| CSTritt | 0:2254358fce87 | 7 | |
| CSTritt | 0:2254358fce87 | 8 | Written by: Dr. C. S. Tritt |
| CSTritt | 6:f05a4d991702 | 9 | Created: 3/18/19 (v. 1.1) |
| CSTritt | 0:2254358fce87 | 10 | */ |
| CSTritt | 0:2254358fce87 | 11 | #include "mbed.h" |
| CSTritt | 0:2254358fce87 | 12 | |
| CSTritt | 5:c58c75c5fe90 | 13 | const int SIZE = 20; // Number of samples per average. |
| CSTritt | 5:c58c75c5fe90 | 14 | const float PAUSE = 0.05; // Seconds between samples. |
| CSTritt | 5:c58c75c5fe90 | 15 | const float INTERVAL = 1.0; // Seconds between averages. |
| CSTritt | 3:1cff703d6eb4 | 16 | |
| CSTritt | 3:1cff703d6eb4 | 17 | AnalogIn analog_value(PB_0); // Same as A3 |
| CSTritt | 0:2254358fce87 | 18 | |
| CSTritt | 0:2254358fce87 | 19 | DigitalOut led(LED1); |
| CSTritt | 0:2254358fce87 | 20 | |
| CSTritt | 0:2254358fce87 | 21 | int main() { |
| CSTritt | 5:c58c75c5fe90 | 22 | float value; // Value to be sent to serial port: 1 mV = 1 deg. C. |
| CSTritt | 0:2254358fce87 | 23 | |
| CSTritt | 3:1cff703d6eb4 | 24 | printf("\nCE Dev Board Temperature Read\n"); |
| CSTritt | 6:f05a4d991702 | 25 | led = 0; // Start with LED off. |
| CSTritt | 0:2254358fce87 | 26 | |
| CSTritt | 0:2254358fce87 | 27 | while(true) { |
| CSTritt | 6:f05a4d991702 | 28 | int i = 0; // Initialize loop count. |
| CSTritt | 6:f05a4d991702 | 29 | float sum = 0.0; // Initialize sum. |
| CSTritt | 6:f05a4d991702 | 30 | while (i < (SIZE - 1)) { // Take SIZE readings and average them. |
| CSTritt | 6:f05a4d991702 | 31 | value = analog_value.read(); // Get analog value (between 0 and 1). |
| CSTritt | 6:f05a4d991702 | 32 | sum = sum + value; // Add it to running sum. |
| CSTritt | 6:f05a4d991702 | 33 | i++; // Increment loop count. |
| CSTritt | 6:f05a4d991702 | 34 | wait(PAUSE); // Wait PAUSE seconds between samples. |
| CSTritt | 0:2254358fce87 | 35 | } |
| CSTritt | 6:f05a4d991702 | 36 | value = (sum/(float) SIZE); // Calculate average. |
| CSTritt | 5:c58c75c5fe90 | 37 | printf("Value = %f\n", value); // Send value as text via serial port. |
| CSTritt | 6:f05a4d991702 | 38 | led = !led; // Toggle LED. |
| CSTritt | 6:f05a4d991702 | 39 | wait(INTERVAL); // Wait INTERVAL seconds between averages. |
| CSTritt | 0:2254358fce87 | 40 | } |
| CSTritt | 5:c58c75c5fe90 | 41 | } |
