Page 272 - ARM Based Microcontroller Projects Using MBED
P. 272
258 8. INTERMEDIATE LEVEL PROJECTS
/***************************************************************************
LED DIMMER
==========
In this project the brightness of the User LED on the Nucleo F411RE
development board is varied. A PWM waveform with the 50Hz frequency
is sent to the LED. The Duty Cycle of this waveform is increased in 10
increments in a second. i.e. every 100ms so that the brightness of the
LED increases. When full brightness is reached, the Duty Cycle is
decremented in 10 steps in a second so that the LED becomes dimmer.
Author: Dogan Ibrahim
Date : September 2018
File : Dimmer
****************************************************************************/
#include "mbed.h"
Serial MyPC(USBTX, USBRX);
PwmOut pwm(LED1);
float frequency = 50.0; // Frequency = 50Hz
float period = 1.0f / frequency; // Period
float DutyCycle,inc;
int main()
{
pwm.period(period); // Start the PWM
inc = period / 10.0f; // Increment
DutyCycle = 0.0; // Starting Duty Cycle
while(1) // Do Forever
{
for(int k = 0; k < 10; k++) // Do 10 times
{
pwm.pulsewidth(DutyCycle); // Set Duty Cycle
DutyCycle = DutyCycle + inc; // Increment Duty Cycle
wait(1.0); // Wait 1 second
}
for(int k = 10; k > 0; k--) // Do 10 times
{
pwm.pulsewidth(DutyCycle); // Set Duty Cycle
DutyCycle = DutyCycle - inc; // Decrement Duty Cycle
wait(1.0); // Wait 1 second
}
}
}
FIG. 8.126 Program listing.