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.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002     Project: analogRead_u16
00003     File: main.cpp
00004 
00005     Reads from analog input, streams ASCII text to std serial using printf and
00006     lights onboard LED. Also demonstrates use of integer literal suffixes 
00007     and constants for HIGH and LOW.
00008 
00009     Written by: Dr. C. S. Tritt
00010     Created: 3/26/17 (v. 1.0)
00011 
00012 */
00013 #include "mbed.h"
00014 
00015 const int HIGH = 1; // Optional, but makes code more readable.
00016 const int LOW = 0; // Optional, but makes code more readable.
00017 
00018 AnalogIn analog_value(A0);
00019 
00020 DigitalOut led(LED1);
00021 
00022 int main()
00023 {
00024     uint16_t iValue; // Value to be read. Must be unsigned int 16.
00025 
00026     printf("\nAnalogIn example\n");
00027 
00028     while(true) {
00029         iValue = analog_value.read_u16(); // Read analog value as uint16.
00030         printf("Value = %u\n", iValue); // Unsigned (u) specifier required.
00031         if (iValue > 32768u) { // Activate built-in LED. The u is required.
00032             led = HIGH;
00033         } else {
00034             led = LOW;
00035         }
00036         wait(0.25); // 250 ms
00037     }
00038 }