AnimatedLED9

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TouchSense.h"
00003 
00004 /*
00005 // 1\
00006 //  |\
00007 //  | \
00008 //  |  \
00009 //  +---\---------> t
00010 //       1
00011 float brightness(float t) {
00012 if (t < 0)
00013 return 0;
00014 if (t > 1)
00015 return 0;
00016 return 1-t;
00017 }
00018 */
00019 
00020 /*
00021 //   1| /\
00022 //    |/  \
00023 //  .5|    \
00024 //    |     \
00025 //    +--|---\---------> t
00026 //      1/3   1
00027 float brightness(float t) {
00028 if (t < 0)
00029 return 0;
00030 if (0 <= t && t < 1/3.0)
00031 return .5+t*1.5;
00032 if (1/3.0 <= t && t < 1)
00033 return 1.5-t*1.5;
00034 if (1 <= t)
00035 return 0;
00036 }
00037 */
00038 
00039 // 1|   /\
00040 //  |  /  \
00041 //  | /    \
00042 //  |/      \
00043 //  +---|----\---------> t
00044 //     1/2   1
00045 float brightness(float t)
00046 {
00047     if (t < 0)
00048         return 0;
00049     if (0 <= t && t < 0.5)
00050         return t*2;
00051     if (0.5 <= t && t < 1)
00052         return 2-t*2;
00053     if (1 <= t)
00054         return 0;
00055 }
00056 
00057 int main()
00058 {
00059     PwmOut led1(p21), led2(p22), led3(p23);
00060     Timer timer;
00061 
00062     DigitalOut out(p19);
00063     AnalogIn in(p20);
00064 
00065     TouchSense sensor1(out, in);
00066     //sensor1.calibrate();
00067 
00068     for (;;) {
00069         if (! sensor1.touched()) {
00070             Thread::wait(1);
00071             continue;
00072         }
00073 
00074         timer.reset();
00075         timer.start();
00076         for (;;) {
00077             float t = timer.read();
00078             if (t > 1.2)
00079                 break;
00080             led1 = brightness(t);
00081             led2 = brightness(t-0.1);
00082             led3 = brightness(t-0.2);
00083             Thread::wait(1);
00084         }
00085     }
00086 }
00087 
00088 // Local Variables: **
00089 // c-basic-offset:4 **
00090 // End: **