Test the PWM functionality in conjunction with an analog input - drive the brightness of a LCD backlight with the ambient light level. Serves as testcase of a PWM problem, see code comment.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "math.h"
00004 
00005 /*
00006     read the light value from a photo transistor connected to P20 and
00007     drive a LCDs LED backlight connected to P25.
00008     The backlight should dimm in the dark and brighten up when the lights go on.
00009     
00010     NOTE: the version below doesn't work, remove the printf in line 35!
00011 */
00012 int main() {
00013     DigitalOut led1(LED1);
00014     float f=0.0;
00015     PwmOut display(p25);
00016     display.period_ms(1);
00017     display=f;
00018     
00019     AnalogIn light(p20);
00020    
00021     for (int i=0;i<100;i++)
00022     {
00023         f=f+0.01;
00024         display=f;
00025         wait (0.03);
00026     }
00027     printf("%f\n",display.read());
00028     led1=1;
00029         
00030     while (true)
00031     {
00032         double d=light;
00033         double sq=pow(d,0.6);
00034         display.write(sq*3);
00035         printf("%f / %f\n",d,sq);
00036         wait(1);
00037     }
00038 }