The program reads an analog voltage from a potentiometer and lights the Nucleo board's built-in LED based on its value.

Dependencies:   mbed

Fork of analogRead by Charles Tritt

Committer:
CSTritt
Date:
Fri Sep 22 19:36:53 2017 +0000
Revision:
3:0756601ff19e
Parent:
2:e0faf9e57796
Initial version. To be used in BE-1005, Fall 2017.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:2254358fce87 1 /*
CSTritt 3:0756601ff19e 2 Project: aReadConditional (for analog read conditional)
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 1:8e3c0c69a6ca 7 toeliminate warning and int constants for HIGH and LOW.
CSTritt 0:2254358fce87 8
CSTritt 0:2254358fce87 9 Written by: Dr. C. S. Tritt
CSTritt 2:e0faf9e57796 10 Created: 3/27/17 (v. 1.1)
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 0:2254358fce87 17
CSTritt 3:0756601ff19e 18 AnalogIn analog_value(A0); // Construct AnalogIn object called analog_value.
CSTritt 0:2254358fce87 19
CSTritt 3:0756601ff19e 20 DigitalOut led(LED1); // Construct DigitalOut object called led.
CSTritt 0:2254358fce87 21
CSTritt 0:2254358fce87 22 int main() {
CSTritt 0:2254358fce87 23 float value; // Value to be read and sent to serial port.
CSTritt 0:2254358fce87 24
CSTritt 3:0756601ff19e 25 printf("\nAnalogIn example\n"); // Identify program.
CSTritt 0:2254358fce87 26
CSTritt 0:2254358fce87 27 while(true) {
CSTritt 0:2254358fce87 28 value = analog_value.read(); // Read the analog input value (0 to 1)
CSTritt 0:2254358fce87 29 printf("Value = %f\n", value); // Send value as text via serial port.
CSTritt 0:2254358fce87 30 if (value > 0.5f) { // Activate built-in LED. The f is optional.
CSTritt 0:2254358fce87 31 led.write(HIGH);
CSTritt 0:2254358fce87 32 }
CSTritt 0:2254358fce87 33 else {
CSTritt 0:2254358fce87 34 led.write(LOW);
CSTritt 0:2254358fce87 35 }
CSTritt 2:e0faf9e57796 36 printf("LED = %d\n", (int) led.read()); // Send LED state via serial.
CSTritt 3:0756601ff19e 37 wait(0.5); // Half a second (500 mS).
CSTritt 0:2254358fce87 38 }
CSTritt 0:2254358fce87 39 }