Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:ab1ec7031081, 2020-05-12 (annotated)
- Committer:
- CSTritt
- Date:
- Tue May 12 18:37:22 2020 +0000
- Revision:
- 4:ab1ec7031081
- Parent:
- 3:f879eeeb5e8d
Analog input to PWM output with serial stream output to PC.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:2254358fce87 | 1 | /* |
CSTritt | 4:ab1ec7031081 | 2 | Project: PWM_Driver_Serial |
CSTritt | 0:2254358fce87 | 3 | File: main.cpp |
CSTritt | 0:2254358fce87 | 4 | |
CSTritt | 4:ab1ec7031081 | 5 | Reads from analog input, sends to PWM, streams ASCII text to std serial |
CSTritt | 4:ab1ec7031081 | 6 | using printf. Use to test higher voltage and current transistor driver |
CSTritt | 4:ab1ec7031081 | 7 | circuits. |
CSTritt | 0:2254358fce87 | 8 | |
CSTritt | 0:2254358fce87 | 9 | Written by: Dr. C. S. Tritt |
CSTritt | 4:ab1ec7031081 | 10 | Created: 5/12/20 (v. 1.0) |
CSTritt | 0:2254358fce87 | 11 | |
CSTritt | 0:2254358fce87 | 12 | */ |
CSTritt | 0:2254358fce87 | 13 | #include "mbed.h" |
CSTritt | 0:2254358fce87 | 14 | |
CSTritt | 4:ab1ec7031081 | 15 | const float WAIT = 0.5f; // Wait time between samples (s). |
CSTritt | 0:2254358fce87 | 16 | |
CSTritt | 4:ab1ec7031081 | 17 | AnalogIn myAIn(A0); // Analog input, connect to pot V divider. |
CSTritt | 4:ab1ec7031081 | 18 | PwmOut myPWM(D10); // PWM output, Bargraph bit 1, connect to driver. |
CSTritt | 0:2254358fce87 | 19 | |
CSTritt | 0:2254358fce87 | 20 | DigitalOut led(LED1); |
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 | 4:ab1ec7031081 | 25 | printf("\nAnalog to serial & PWM example.\n"); |
CSTritt | 0:2254358fce87 | 26 | |
CSTritt | 0:2254358fce87 | 27 | while(true) { |
CSTritt | 4:ab1ec7031081 | 28 | value = myAIn.read(); // Read the analog input value (0 to 1) |
CSTritt | 4:ab1ec7031081 | 29 | myPWM.write(value); // Write value read to PWM pin. |
CSTritt | 3:f879eeeb5e8d | 30 | printf("%f\n", value); // Send value as text via serial port. |
CSTritt | 3:f879eeeb5e8d | 31 | wait(WAIT); // Wait WAIT seconds. |
CSTritt | 0:2254358fce87 | 32 | } |
CSTritt | 3:f879eeeb5e8d | 33 | } |