The 2nd of 5 mbed demonstration projects. Unsigned short analog read demonstration. Reads from analog input, streams ASCII text to std serial using printf and lights on board LED. Also demonstrates use of integer literal suffixes and int constants for HIGH and LOW.
main.cpp@0:753e269a13b9, 2017-03-27 (annotated)
- Committer:
- CSTritt
- Date:
- Mon Mar 27 12:49:08 2017 +0000
- Revision:
- 0:753e269a13b9
Version 1.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:753e269a13b9 | 1 | /* |
CSTritt | 0:753e269a13b9 | 2 | Project: analogRead_u16 |
CSTritt | 0:753e269a13b9 | 3 | File: main.cpp |
CSTritt | 0:753e269a13b9 | 4 | |
CSTritt | 0:753e269a13b9 | 5 | Reads from analog input, streams ASCII text to std serial using printf and |
CSTritt | 0:753e269a13b9 | 6 | lights onboard LED. Also demonstrates use of integer literal suffixes |
CSTritt | 0:753e269a13b9 | 7 | and constants for HIGH and LOW. |
CSTritt | 0:753e269a13b9 | 8 | |
CSTritt | 0:753e269a13b9 | 9 | Written by: Dr. C. S. Tritt |
CSTritt | 0:753e269a13b9 | 10 | Created: 3/26/17 (v. 1.0) |
CSTritt | 0:753e269a13b9 | 11 | |
CSTritt | 0:753e269a13b9 | 12 | */ |
CSTritt | 0:753e269a13b9 | 13 | #include "mbed.h" |
CSTritt | 0:753e269a13b9 | 14 | |
CSTritt | 0:753e269a13b9 | 15 | const int HIGH = 1; // Optional, but makes code more readable. |
CSTritt | 0:753e269a13b9 | 16 | const int LOW = 0; // Optional, but makes code more readable. |
CSTritt | 0:753e269a13b9 | 17 | |
CSTritt | 0:753e269a13b9 | 18 | AnalogIn analog_value(A0); |
CSTritt | 0:753e269a13b9 | 19 | |
CSTritt | 0:753e269a13b9 | 20 | DigitalOut led(LED1); |
CSTritt | 0:753e269a13b9 | 21 | |
CSTritt | 0:753e269a13b9 | 22 | int main() |
CSTritt | 0:753e269a13b9 | 23 | { |
CSTritt | 0:753e269a13b9 | 24 | uint16_t iValue; // Value to be read. Must be unsigned int 16. |
CSTritt | 0:753e269a13b9 | 25 | |
CSTritt | 0:753e269a13b9 | 26 | printf("\nAnalogIn example\n"); |
CSTritt | 0:753e269a13b9 | 27 | |
CSTritt | 0:753e269a13b9 | 28 | while(true) { |
CSTritt | 0:753e269a13b9 | 29 | iValue = analog_value.read_u16(); // Read analog value as uint16. |
CSTritt | 0:753e269a13b9 | 30 | printf("Value = %u\n", iValue); // Unsigned (u) specifier required. |
CSTritt | 0:753e269a13b9 | 31 | if (iValue > 32768u) { // Activate built-in LED. The u is required. |
CSTritt | 0:753e269a13b9 | 32 | led = HIGH; |
CSTritt | 0:753e269a13b9 | 33 | } else { |
CSTritt | 0:753e269a13b9 | 34 | led = LOW; |
CSTritt | 0:753e269a13b9 | 35 | } |
CSTritt | 0:753e269a13b9 | 36 | wait(0.25); // 250 ms |
CSTritt | 0:753e269a13b9 | 37 | } |
CSTritt | 0:753e269a13b9 | 38 | } |