TSL2561_demo_program

Dependencies:   TSL2561 mbed

Fork of TSL2561_Townsend by Tuan Anh Nguyen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TSL2561.h"
00003 
00004 Serial PC(USBTX, USBRX);
00005 
00006 #define PC_PRINTX(z,x)             if(z==1) PC.printf(x);
00007 #define PC_PRINTLNX(z,x)           if(z==1) {PC.printf(x);        PC.printf("\r\n");}
00008 #define PC_PRINTXY(z,x, y)         if(z==1) PC.printf(x, y);
00009 #define PC_PRINTLNXY(z,x, y)       if(z==1) {PC.printf(x, y);     PC.printf("\r\n");}
00010 
00011 DigitalOut myled(LED1);
00012 TSL2561 tsl2561(TSL2561_ADDR_FLOAT);
00013 
00014 Timer setuptimer;
00015 Timer executetimer;
00016 
00017 void setup(void){
00018 
00019     if (tsl2561.begin()) {    
00020         PC_PRINTLNX(1,"TSL2561 Sensor Found");        
00021     } else {    
00022         PC_PRINTLNX(1,"TSL2561 Sensor not Found");   
00023     }
00024     
00025     // You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations
00026   tsl2561.setGain(TSL2561_GAIN_0X);         // set no gain (for bright situtations)
00027   //tsl2561.setGain(TSL2561_GAIN_16X);      // set 16x gain (for dim situations)
00028   
00029   // Changing the integration time gives you a longer time over which to sense tsl2561
00030   // longer timelines are slower, but are good in very low tsl2561 situtations!
00031   //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_13MS);  // shortest integration time (bright tsl2561)
00032   //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_101MS);  // medium integration time (medium tsl2561)
00033   tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS);  // longest integration time (dim tsl2561)
00034   
00035   // Now we're ready to get readings!
00036 }
00037 
00038 int main() {  
00039     
00040     PC_PRINTLNX(1,"----------START-------------");
00041     setuptimer.start();
00042     setup();    
00043     setuptimer.stop();
00044     PC_PRINTLNXY(1,"Setup time: %f",setuptimer.read());
00045     setuptimer.reset();
00046     
00047     uint16_t x,y,z;
00048       
00049     while(1) {      
00050     
00051        
00052         // Simple data read example. Just read the infrared, fullspecrtrum diode 
00053         // or 'visible' (difference between the two) channels.
00054         // This can take 13-402 milliseconds! Uncomment whichever of the following you want to read
00055         
00056         executetimer.start();
00057         x = tsl2561.getLuminosity(TSL2561_VISIBLE);     
00058         y = tsl2561.getLuminosity(TSL2561_FULLSPECTRUM);
00059         z = tsl2561.getLuminosity(TSL2561_INFRARED);
00060         executetimer.stop();
00061         
00062         PC_PRINTLNXY(1,"Visible: %d",x);
00063         PC_PRINTLNXY(1,"Full Spectrum: %d",y);
00064         PC_PRINTLNXY(1,"Infrared: %d",z);
00065         PC_PRINTLNXY(1,"Execution Time: %f",executetimer.read());
00066         executetimer.reset();
00067         
00068         //More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
00069         //That way you can do whatever math and comparisons you want!
00070          // uint32_t lum = tsl2561.getFullLuminosity();
00071        // uint16_t ir, full;
00072       //  ir = lum >> 16;
00073       //  full = lum & 0xFFFF;
00074   
00075      //   PC_PRINTLNXY(1,"Visible: %d",full - ir);
00076      //   PC_PRINTLNXY(1,"Full Spectrum: %d",full);
00077      //   PC_PRINTLNXY(1,"Infrared: %d",ir);  
00078   
00079     //    wait(1);
00080      PC_PRINTLNX(1,"----------COMPLETE-------------");
00081     }
00082 }