![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
My Example PWM program with method 2
Diff: main.cpp
- Revision:
- 0:77fda962237d
diff -r 000000000000 -r 77fda962237d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Feb 15 20:01:59 2015 +0000 @@ -0,0 +1,34 @@ +#include "mbed.h" + +PwmOut mypwm(LED1); + + + +int main() { + + + + printf("pwm set to %.2f %%\n", mypwm.read() * 100); + + float pwm=0.0; + + while(1) { + + pwm = pwm + 0.05; // instead of this pattern we can also write like check below + mypwm.write(pwm); + if(pwm >= 1.0) + pwm = 0.0; + wait(0.03); + + /* + mypwm = mypwm + 0.01; + if(mypwm >= 1.0) + mypwm = 0.0; + wait(0.05); + //and here we will not need any "float pwm=0.0;" type vars + //another thing is that even if we don't set the pwm.period default period will be used which will be + //same across the whole program for any number of pwms used on that chip. + */ + + } +}