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.
11 years, 8 months ago.
Led Brightness control program - class doubts
host terminal LED dimmer control
- include "mbed.h"
Serial pc(USBTX, USBRX); tx, rx
PwmOut led(LED1);
float brightness=0.0;
int main() { pc.printf(“Control of LED dimmer by host terminal\n\r");
pc.printf("Press 'u‘ = brighter, 'd‘ = dimmer\n\r");
while(1) {
char c = pc.getc();
wait(0.001);
if((c == 'u') && (brightness < 0.1))
{ brightness += 0.001; led = brightness; }
if((c == 'd') && (brightness > 0.0))
{ brightness -= 0.001; led = brightness; }
pc.printf("%c %1.3f \n \r",c,brightness); } }
Here in this program how does "float brightness" gets updated with the Led's brightness.
In my version, I had included the following: float brightness = 0.0; brightness = led but this statement seems absurd due to two different kind of variables.
Can anyone say how does brightness in the original source gets updated? please send the link of the library which mentions so.
Thanks
1 Answer
11 years, 8 months ago.
Led is defined as instance of the pwmout class. The pwm dutycycle can be set with a value between 0.0 and 1.0. This will change the led brightness between off and full on. The pwmout class has overloaded the = operator to set the dutycycle. So led=1.0 will set the LED to max brightness.
Please use <<code>>
brackets for better readability of your posted code.