Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 10 months ago.
Confusion about PwnOut class
I am super confused how the pwm class works.
The first program is from the PwmOut referance file:
LED_Fade
// Fade a led on.
#include "mbed.h"
PwmOut led(LED1);
int main() {
while(1) {
led = led + 0.01;
wait(0.2);
if(led == 1.0) {
led = 0;
}
}
}
Base o the previous example I wrote this next program, that should act similar to the previous program, but for some reason it is backwards (adding to led dims LED1, subtracting from led brightens LED 1):
Control LED brightness using keyboard
//Author: collinan
//Control LED brightness using keyboard
//Tested on FRDM-KL46Z (using Terminal, on MacBook pro)
//'u' for brightness up
//'d' for brightness down
//Program compiles
#include "mbed.h"
Serial pc(USBTX , USBRX);//tx, rx
PwmOut led(LED1);
int main()
{
led=0;
while (1) {
pc.printf("Press 'u' to brighten LED1, and 'd' to dim LED1 \n\r");
char c = pc.getc();//get user input
//this "if" should brighten but it dims LED
if((c == 'u')) {
pc.printf("IN Bright\n\r");
if(led!=1) {
led =led + 0.1;
} else {
pc.printf("Max reached.\n\r");
}
}
//this "if" should dim but it brightens LED
if((c == 'd') ) {
pc.printf("IN Dim\n\r");
if(led!=0) {
led =led - 0.1;
} else {
pc.printf("Min reached.\n\r");
}
}
}
}
Any explanation wold be appreciated. Thanks in Advance.