Initial version. Demonstrates the use of PwmOut.

Dependencies:   mbed

Fork of Fade by MSOE EE2905

Committer:
CSTritt
Date:
Wed Sep 13 18:33:44 2017 +0000
Revision:
2:819a248bbe75
Parent:
1:3efb8a158c32
Initial version. Demonstrates PwmOut.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rossatmsoe 0:7e3885152044 1 /*
CSTritt 2:819a248bbe75 2 Project: Fade
CSTritt 2:819a248bbe75 3 File: main.cpp
CSTritt 2:819a248bbe75 4 Last revised by: Dr. C. S. Tritt
CSTritt 2:819a248bbe75 5 Revision date: 9/13/17 (v. 1.0)
rossatmsoe 0:7e3885152044 6
CSTritt 2:819a248bbe75 7 This example shows how to fade an LED on pin D15 using PWM.
rossatmsoe 0:7e3885152044 8
rossatmsoe 0:7e3885152044 9 This example code is in the public domain.
rossatmsoe 0:7e3885152044 10 */
rossatmsoe 0:7e3885152044 11 #include "mbed.h"
rossatmsoe 0:7e3885152044 12 // Similar to DigitalOut, you can declare a pin to flicker on and off
rossatmsoe 0:7e3885152044 13 // at a desired frequency and duty cycle using PwmOut.
CSTritt 2:819a248bbe75 14 PwmOut my_LED(D15); // Construct my PwmOut object.
rossatmsoe 0:7e3885152044 15
CSTritt 2:819a248bbe75 16 // Create variables to control brightness and fading:
rossatmsoe 0:7e3885152044 17 float fadeAmount = 0.05;
rossatmsoe 0:7e3885152044 18 float brightness = 0;
rossatmsoe 0:7e3885152044 19
rossatmsoe 0:7e3885152044 20 int main() {
CSTritt 2:819a248bbe75 21 while(true) {
CSTritt 2:819a248bbe75 22
CSTritt 2:819a248bbe75 23 // The LED brightness is set by writing a value between 0.0 and 1.0.
CSTritt 2:819a248bbe75 24 // This sets the duty cycle (0.5 = 50%, etc.).
rossatmsoe 0:7e3885152044 25
CSTritt 2:819a248bbe75 26 my_LED = brightness; // Set brightness.
CSTritt 2:819a248bbe75 27 brightness = brightness + fadeAmount; // Change for next time through.
CSTritt 2:819a248bbe75 28
rossatmsoe 0:7e3885152044 29 // Reverse the direction of the fading at the ends of the fade.
rossatmsoe 0:7e3885152044 30 // It's hard for floats to be exactly equal, so reverse if we hit
rossatmsoe 0:7e3885152044 31 // or go past full dark or full bright.
rossatmsoe 0:7e3885152044 32
CSTritt 2:819a248bbe75 33 if (brightness <= 0 || brightness >= 1) { // Reverse at limits.
CSTritt 2:819a248bbe75 34 fadeAmount = -fadeAmount ; // Reverse fade direction.
CSTritt 2:819a248bbe75 35 }
CSTritt 2:819a248bbe75 36 wait(0.03); // Wait for 30 milliseconds to see the dimming effect .
rossatmsoe 0:7e3885152044 37 }
CSTritt 2:819a248bbe75 38 }