The 4th of 5 siimple mbed demonstration projects. Provides functionality similar to my analogRead project, but uses PWM rather than digital output. Does not use operator overloading.

Dependencies:   mbed

Committer:
CSTritt
Date:
Mon Mar 27 12:48:07 2017 +0000
Revision:
0:fe3cc7e39d4d
Child:
1:0978d8c208b7
Version 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:fe3cc7e39d4d 1 /*
CSTritt 0:fe3cc7e39d4d 2 Project: analog2pwm
CSTritt 0:fe3cc7e39d4d 3 File: main.cpp
CSTritt 0:fe3cc7e39d4d 4
CSTritt 0:fe3cc7e39d4d 5 Reads from analog input, streams ASCII text to std serial using printf and
CSTritt 0:fe3cc7e39d4d 6 and proportionally lights onboard LED. Also demonstrates floating point
CSTritt 0:fe3cc7e39d4d 7 literal sufix to eliminate warning.
CSTritt 0:fe3cc7e39d4d 8
CSTritt 0:fe3cc7e39d4d 9 Written by: Dr. C. S. Tritt
CSTritt 0:fe3cc7e39d4d 10 Created: 3/27/17 (v. 1.0)
CSTritt 0:fe3cc7e39d4d 11
CSTritt 0:fe3cc7e39d4d 12 */
CSTritt 0:fe3cc7e39d4d 13 #include "mbed.h"
CSTritt 0:fe3cc7e39d4d 14
CSTritt 0:fe3cc7e39d4d 15 AnalogIn analog_value(A0);
CSTritt 0:fe3cc7e39d4d 16
CSTritt 0:fe3cc7e39d4d 17 PwmOut led(LED1);
CSTritt 0:fe3cc7e39d4d 18
CSTritt 0:fe3cc7e39d4d 19 int main()
CSTritt 0:fe3cc7e39d4d 20 {
CSTritt 0:fe3cc7e39d4d 21 float value; // Value to be read and sent to serial port.
CSTritt 0:fe3cc7e39d4d 22
CSTritt 0:fe3cc7e39d4d 23 printf("\nAnalog to PWM example.\n");
CSTritt 0:fe3cc7e39d4d 24
CSTritt 0:fe3cc7e39d4d 25 while(true) {
CSTritt 0:fe3cc7e39d4d 26 value = analog_value.read(); // Read the analog input value (0 to 1)
CSTritt 0:fe3cc7e39d4d 27 printf("Value = %f\n", value); // Send value as text via serial port.
CSTritt 0:fe3cc7e39d4d 28 led.write(value); // Proportionally light LED.
CSTritt 0:fe3cc7e39d4d 29 wait(0.25); // 250 ms
CSTritt 0:fe3cc7e39d4d 30 }
CSTritt 0:fe3cc7e39d4d 31 }