Charles Tritt
/
potSerial
main.cpp@3:f879eeeb5e8d, 2020-02-08 (annotated)
- Committer:
- CSTritt
- Date:
- Sat Feb 08 18:39:55 2020 +0000
- Revision:
- 3:f879eeeb5e8d
- Parent:
- 2:e0faf9e57796
Initial version of potentiometer (analog) to serial program.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:2254358fce87 | 1 | /* |
CSTritt | 3:f879eeeb5e8d | 2 | Project: potSerial |
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 | 1:8e3c0c69a6ca | 6 | lights onboard LED. Also demonstrates use of floating point literal suffix |
CSTritt | 3:f879eeeb5e8d | 7 | to eliminate warning and int constants for HIGH and LOW. |
CSTritt | 0:2254358fce87 | 8 | |
CSTritt | 0:2254358fce87 | 9 | Written by: Dr. C. S. Tritt |
CSTritt | 3:f879eeeb5e8d | 10 | Created: 2/8/20 (v. 1.0) |
CSTritt | 0:2254358fce87 | 11 | |
CSTritt | 0:2254358fce87 | 12 | */ |
CSTritt | 0:2254358fce87 | 13 | #include "mbed.h" |
CSTritt | 0:2254358fce87 | 14 | |
CSTritt | 0:2254358fce87 | 15 | const int HIGH = 1; // Optional, but makes code more readable. |
CSTritt | 0:2254358fce87 | 16 | const int LOW = 0; // Optional, but makes code more readable. |
CSTritt | 3:f879eeeb5e8d | 17 | const float WAIT = 0.25f; // Wait time between samples (s). |
CSTritt | 3:f879eeeb5e8d | 18 | const float LIGHT = 0.5f; // Analog value (0 to 1 scale) to light LED. |
CSTritt | 0:2254358fce87 | 19 | |
CSTritt | 0:2254358fce87 | 20 | AnalogIn analog_value(A0); |
CSTritt | 0:2254358fce87 | 21 | |
CSTritt | 0:2254358fce87 | 22 | DigitalOut led(LED1); |
CSTritt | 0:2254358fce87 | 23 | |
CSTritt | 0:2254358fce87 | 24 | int main() { |
CSTritt | 0:2254358fce87 | 25 | float value; // Value to be read and sent to serial port. |
CSTritt | 0:2254358fce87 | 26 | |
CSTritt | 3:f879eeeb5e8d | 27 | printf("\nAnalog to serial example.\n"); |
CSTritt | 0:2254358fce87 | 28 | |
CSTritt | 0:2254358fce87 | 29 | while(true) { |
CSTritt | 0:2254358fce87 | 30 | value = analog_value.read(); // Read the analog input value (0 to 1) |
CSTritt | 3:f879eeeb5e8d | 31 | printf("%f\n", value); // Send value as text via serial port. |
CSTritt | 3:f879eeeb5e8d | 32 | if (value > LIGHT) { // Activate built-in LED based on LIGHT value. |
CSTritt | 0:2254358fce87 | 33 | led.write(HIGH); |
CSTritt | 0:2254358fce87 | 34 | } |
CSTritt | 0:2254358fce87 | 35 | else { |
CSTritt | 0:2254358fce87 | 36 | led.write(LOW); |
CSTritt | 0:2254358fce87 | 37 | } |
CSTritt | 3:f879eeeb5e8d | 38 | wait(WAIT); // Wait WAIT seconds. |
CSTritt | 0:2254358fce87 | 39 | } |
CSTritt | 3:f879eeeb5e8d | 40 | } |