Tuan Anh Nguyen
/
TSL2561_Light_sensor
TSL2561_demo_program
Fork of TSL2561_Townsend by
main.cpp@1:0f3c4d6b818a, 2014-01-09 (annotated)
- Committer:
- anhnt2407
- Date:
- Thu Jan 09 10:18:58 2014 +0000
- Revision:
- 1:0f3c4d6b818a
- Parent:
- 0:9ab53ff6cf89
TSL2561(demo program)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
anhnt2407 | 0:9ab53ff6cf89 | 1 | #include "mbed.h" |
anhnt2407 | 0:9ab53ff6cf89 | 2 | #include "TSL2561.h" |
anhnt2407 | 0:9ab53ff6cf89 | 3 | |
anhnt2407 | 0:9ab53ff6cf89 | 4 | Serial PC(USBTX, USBRX); |
anhnt2407 | 0:9ab53ff6cf89 | 5 | |
anhnt2407 | 0:9ab53ff6cf89 | 6 | #define PC_PRINTX(z,x) if(z==1) PC.printf(x); |
anhnt2407 | 0:9ab53ff6cf89 | 7 | #define PC_PRINTLNX(z,x) if(z==1) {PC.printf(x); PC.printf("\r\n");} |
anhnt2407 | 0:9ab53ff6cf89 | 8 | #define PC_PRINTXY(z,x, y) if(z==1) PC.printf(x, y); |
anhnt2407 | 0:9ab53ff6cf89 | 9 | #define PC_PRINTLNXY(z,x, y) if(z==1) {PC.printf(x, y); PC.printf("\r\n");} |
anhnt2407 | 0:9ab53ff6cf89 | 10 | |
anhnt2407 | 0:9ab53ff6cf89 | 11 | DigitalOut myled(LED1); |
anhnt2407 | 0:9ab53ff6cf89 | 12 | TSL2561 tsl2561(TSL2561_ADDR_FLOAT); |
anhnt2407 | 0:9ab53ff6cf89 | 13 | |
anhnt2407 | 1:0f3c4d6b818a | 14 | Timer setuptimer; |
anhnt2407 | 1:0f3c4d6b818a | 15 | Timer executetimer; |
anhnt2407 | 1:0f3c4d6b818a | 16 | |
anhnt2407 | 0:9ab53ff6cf89 | 17 | void setup(void){ |
anhnt2407 | 0:9ab53ff6cf89 | 18 | |
anhnt2407 | 0:9ab53ff6cf89 | 19 | if (tsl2561.begin()) { |
anhnt2407 | 0:9ab53ff6cf89 | 20 | PC_PRINTLNX(1,"TSL2561 Sensor Found"); |
anhnt2407 | 0:9ab53ff6cf89 | 21 | } else { |
anhnt2407 | 0:9ab53ff6cf89 | 22 | PC_PRINTLNX(1,"TSL2561 Sensor not Found"); |
anhnt2407 | 0:9ab53ff6cf89 | 23 | } |
anhnt2407 | 0:9ab53ff6cf89 | 24 | |
anhnt2407 | 0:9ab53ff6cf89 | 25 | // You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations |
anhnt2407 | 0:9ab53ff6cf89 | 26 | tsl2561.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations) |
anhnt2407 | 0:9ab53ff6cf89 | 27 | //tsl2561.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations) |
anhnt2407 | 0:9ab53ff6cf89 | 28 | |
anhnt2407 | 0:9ab53ff6cf89 | 29 | // Changing the integration time gives you a longer time over which to sense tsl2561 |
anhnt2407 | 0:9ab53ff6cf89 | 30 | // longer timelines are slower, but are good in very low tsl2561 situtations! |
anhnt2407 | 0:9ab53ff6cf89 | 31 | //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright tsl2561) |
anhnt2407 | 0:9ab53ff6cf89 | 32 | //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium tsl2561) |
anhnt2407 | 0:9ab53ff6cf89 | 33 | tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim tsl2561) |
anhnt2407 | 0:9ab53ff6cf89 | 34 | |
anhnt2407 | 0:9ab53ff6cf89 | 35 | // Now we're ready to get readings! |
anhnt2407 | 0:9ab53ff6cf89 | 36 | } |
anhnt2407 | 0:9ab53ff6cf89 | 37 | |
anhnt2407 | 0:9ab53ff6cf89 | 38 | int main() { |
anhnt2407 | 0:9ab53ff6cf89 | 39 | |
anhnt2407 | 0:9ab53ff6cf89 | 40 | PC_PRINTLNX(1,"----------START-------------"); |
anhnt2407 | 1:0f3c4d6b818a | 41 | setuptimer.start(); |
anhnt2407 | 0:9ab53ff6cf89 | 42 | setup(); |
anhnt2407 | 1:0f3c4d6b818a | 43 | setuptimer.stop(); |
anhnt2407 | 1:0f3c4d6b818a | 44 | PC_PRINTLNXY(1,"Setup time: %f",setuptimer.read()); |
anhnt2407 | 1:0f3c4d6b818a | 45 | setuptimer.reset(); |
anhnt2407 | 0:9ab53ff6cf89 | 46 | |
anhnt2407 | 0:9ab53ff6cf89 | 47 | uint16_t x,y,z; |
anhnt2407 | 0:9ab53ff6cf89 | 48 | |
anhnt2407 | 0:9ab53ff6cf89 | 49 | while(1) { |
anhnt2407 | 0:9ab53ff6cf89 | 50 | |
anhnt2407 | 0:9ab53ff6cf89 | 51 | |
anhnt2407 | 0:9ab53ff6cf89 | 52 | // Simple data read example. Just read the infrared, fullspecrtrum diode |
anhnt2407 | 0:9ab53ff6cf89 | 53 | // or 'visible' (difference between the two) channels. |
anhnt2407 | 0:9ab53ff6cf89 | 54 | // This can take 13-402 milliseconds! Uncomment whichever of the following you want to read |
anhnt2407 | 1:0f3c4d6b818a | 55 | |
anhnt2407 | 1:0f3c4d6b818a | 56 | executetimer.start(); |
anhnt2407 | 0:9ab53ff6cf89 | 57 | x = tsl2561.getLuminosity(TSL2561_VISIBLE); |
anhnt2407 | 0:9ab53ff6cf89 | 58 | y = tsl2561.getLuminosity(TSL2561_FULLSPECTRUM); |
anhnt2407 | 0:9ab53ff6cf89 | 59 | z = tsl2561.getLuminosity(TSL2561_INFRARED); |
anhnt2407 | 1:0f3c4d6b818a | 60 | executetimer.stop(); |
anhnt2407 | 0:9ab53ff6cf89 | 61 | |
anhnt2407 | 0:9ab53ff6cf89 | 62 | PC_PRINTLNXY(1,"Visible: %d",x); |
anhnt2407 | 0:9ab53ff6cf89 | 63 | PC_PRINTLNXY(1,"Full Spectrum: %d",y); |
anhnt2407 | 0:9ab53ff6cf89 | 64 | PC_PRINTLNXY(1,"Infrared: %d",z); |
anhnt2407 | 1:0f3c4d6b818a | 65 | PC_PRINTLNXY(1,"Execution Time: %f",executetimer.read()); |
anhnt2407 | 1:0f3c4d6b818a | 66 | executetimer.reset(); |
anhnt2407 | 0:9ab53ff6cf89 | 67 | |
anhnt2407 | 0:9ab53ff6cf89 | 68 | //More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum |
anhnt2407 | 0:9ab53ff6cf89 | 69 | //That way you can do whatever math and comparisons you want! |
anhnt2407 | 0:9ab53ff6cf89 | 70 | // uint32_t lum = tsl2561.getFullLuminosity(); |
anhnt2407 | 0:9ab53ff6cf89 | 71 | // uint16_t ir, full; |
anhnt2407 | 0:9ab53ff6cf89 | 72 | // ir = lum >> 16; |
anhnt2407 | 0:9ab53ff6cf89 | 73 | // full = lum & 0xFFFF; |
anhnt2407 | 0:9ab53ff6cf89 | 74 | |
anhnt2407 | 0:9ab53ff6cf89 | 75 | // PC_PRINTLNXY(1,"Visible: %d",full - ir); |
anhnt2407 | 0:9ab53ff6cf89 | 76 | // PC_PRINTLNXY(1,"Full Spectrum: %d",full); |
anhnt2407 | 0:9ab53ff6cf89 | 77 | // PC_PRINTLNXY(1,"Infrared: %d",ir); |
anhnt2407 | 0:9ab53ff6cf89 | 78 | |
anhnt2407 | 0:9ab53ff6cf89 | 79 | // wait(1); |
anhnt2407 | 0:9ab53ff6cf89 | 80 | PC_PRINTLNX(1,"----------COMPLETE-------------"); |
anhnt2407 | 0:9ab53ff6cf89 | 81 | } |
anhnt2407 | 0:9ab53ff6cf89 | 82 | } |