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:
Wed Apr 14 19:38:19 2021 +0000
Revision:
2:9a801aa42762
Parent:
1:0978d8c208b7
Confirmed operation with L476RG with GikFun board.

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 1:0978d8c208b7 11 Last revised: 5/8/18 (ported to CE Dev Board)
CSTritt 0:fe3cc7e39d4d 12
CSTritt 0:fe3cc7e39d4d 13 */
CSTritt 0:fe3cc7e39d4d 14 #include "mbed.h"
CSTritt 0:fe3cc7e39d4d 15
CSTritt 1:0978d8c208b7 16 AnalogIn analog_value(A5); // A5 is unused on CE Dev Board.
CSTritt 0:fe3cc7e39d4d 17
CSTritt 2:9a801aa42762 18 PwmOut led(LED1); // D13 on Nucleo boards.
CSTritt 0:fe3cc7e39d4d 19
CSTritt 0:fe3cc7e39d4d 20 int main()
CSTritt 0:fe3cc7e39d4d 21 {
CSTritt 0:fe3cc7e39d4d 22 float value; // Value to be read and sent to serial port.
CSTritt 0:fe3cc7e39d4d 23
CSTritt 0:fe3cc7e39d4d 24 printf("\nAnalog to PWM example.\n");
CSTritt 0:fe3cc7e39d4d 25
CSTritt 0:fe3cc7e39d4d 26 while(true) {
CSTritt 0:fe3cc7e39d4d 27 value = analog_value.read(); // Read the analog input value (0 to 1)
CSTritt 0:fe3cc7e39d4d 28 printf("Value = %f\n", value); // Send value as text via serial port.
CSTritt 0:fe3cc7e39d4d 29 led.write(value); // Proportionally light LED.
CSTritt 0:fe3cc7e39d4d 30 wait(0.25); // 250 ms
CSTritt 0:fe3cc7e39d4d 31 }
CSTritt 0:fe3cc7e39d4d 32 }