TSL2561_Townsend program for test

Dependencies:   TSL2561 mbed

Committer:
anhnt2407
Date:
Tue Sep 10 15:15:20 2013 +0000
Revision:
0:9ab53ff6cf89
20130911

Who changed what in which revision?

UserRevisionLine numberNew 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
anhnt2407 0:9ab53ff6cf89 5 Serial PC(USBTX, USBRX);
anhnt2407 0:9ab53ff6cf89 6
anhnt2407 0:9ab53ff6cf89 7 #define PC_PRINTX(z,x) if(z==1) PC.printf(x);
anhnt2407 0:9ab53ff6cf89 8 #define PC_PRINTLNX(z,x) if(z==1) {PC.printf(x); PC.printf("\r\n");}
anhnt2407 0:9ab53ff6cf89 9 #define PC_PRINTXY(z,x, y) if(z==1) PC.printf(x, y);
anhnt2407 0:9ab53ff6cf89 10 #define PC_PRINTLNXY(z,x, y) if(z==1) {PC.printf(x, y); PC.printf("\r\n");}
anhnt2407 0:9ab53ff6cf89 11
anhnt2407 0:9ab53ff6cf89 12 DigitalOut myled(LED1);
anhnt2407 0:9ab53ff6cf89 13 TSL2561 tsl2561(TSL2561_ADDR_FLOAT);
anhnt2407 0:9ab53ff6cf89 14
anhnt2407 0:9ab53ff6cf89 15 void setup(void){
anhnt2407 0:9ab53ff6cf89 16
anhnt2407 0:9ab53ff6cf89 17 if (tsl2561.begin()) {
anhnt2407 0:9ab53ff6cf89 18 PC_PRINTLNX(1,"TSL2561 Sensor Found");
anhnt2407 0:9ab53ff6cf89 19 } else {
anhnt2407 0:9ab53ff6cf89 20 PC_PRINTLNX(1,"TSL2561 Sensor not Found");
anhnt2407 0:9ab53ff6cf89 21 }
anhnt2407 0:9ab53ff6cf89 22
anhnt2407 0:9ab53ff6cf89 23 // You can change the gain on the fly, to adapt to brighter/dimmer tsl2561 situations
anhnt2407 0:9ab53ff6cf89 24 tsl2561.setGain(TSL2561_GAIN_0X); // set no gain (for bright situtations)
anhnt2407 0:9ab53ff6cf89 25 //tsl2561.setGain(TSL2561_GAIN_16X); // set 16x gain (for dim situations)
anhnt2407 0:9ab53ff6cf89 26
anhnt2407 0:9ab53ff6cf89 27 // Changing the integration time gives you a longer time over which to sense tsl2561
anhnt2407 0:9ab53ff6cf89 28 // longer timelines are slower, but are good in very low tsl2561 situtations!
anhnt2407 0:9ab53ff6cf89 29 //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_13MS); // shortest integration time (bright tsl2561)
anhnt2407 0:9ab53ff6cf89 30 //tsl2561.setTiming(TSL2561_INTEGRATIONTIME_101MS); // medium integration time (medium tsl2561)
anhnt2407 0:9ab53ff6cf89 31 tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS); // longest integration time (dim tsl2561)
anhnt2407 0:9ab53ff6cf89 32
anhnt2407 0:9ab53ff6cf89 33 // Now we're ready to get readings!
anhnt2407 0:9ab53ff6cf89 34 }
anhnt2407 0:9ab53ff6cf89 35
anhnt2407 0:9ab53ff6cf89 36 int main() {
anhnt2407 0:9ab53ff6cf89 37
anhnt2407 0:9ab53ff6cf89 38 PC_PRINTLNX(1,"----------START-------------");
anhnt2407 0:9ab53ff6cf89 39
anhnt2407 0:9ab53ff6cf89 40 setup();
anhnt2407 0:9ab53ff6cf89 41
anhnt2407 0:9ab53ff6cf89 42 uint16_t x,y,z;
anhnt2407 0:9ab53ff6cf89 43
anhnt2407 0:9ab53ff6cf89 44 while(1) {
anhnt2407 0:9ab53ff6cf89 45
anhnt2407 0:9ab53ff6cf89 46
anhnt2407 0:9ab53ff6cf89 47 // Simple data read example. Just read the infrared, fullspecrtrum diode
anhnt2407 0:9ab53ff6cf89 48 // or 'visible' (difference between the two) channels.
anhnt2407 0:9ab53ff6cf89 49 // This can take 13-402 milliseconds! Uncomment whichever of the following you want to read
anhnt2407 0:9ab53ff6cf89 50 x = tsl2561.getLuminosity(TSL2561_VISIBLE);
anhnt2407 0:9ab53ff6cf89 51 y = tsl2561.getLuminosity(TSL2561_FULLSPECTRUM);
anhnt2407 0:9ab53ff6cf89 52 z = tsl2561.getLuminosity(TSL2561_INFRARED);
anhnt2407 0:9ab53ff6cf89 53
anhnt2407 0:9ab53ff6cf89 54 PC_PRINTLNXY(1,"Visible: %d",x);
anhnt2407 0:9ab53ff6cf89 55 PC_PRINTLNXY(1,"Full Spectrum: %d",y);
anhnt2407 0:9ab53ff6cf89 56 PC_PRINTLNXY(1,"Infrared: %d",z);
anhnt2407 0:9ab53ff6cf89 57
anhnt2407 0:9ab53ff6cf89 58 //More advanced data read example. Read 32 bits with top 16 bits IR, bottom 16 bits full spectrum
anhnt2407 0:9ab53ff6cf89 59 //That way you can do whatever math and comparisons you want!
anhnt2407 0:9ab53ff6cf89 60 // uint32_t lum = tsl2561.getFullLuminosity();
anhnt2407 0:9ab53ff6cf89 61 // uint16_t ir, full;
anhnt2407 0:9ab53ff6cf89 62 // ir = lum >> 16;
anhnt2407 0:9ab53ff6cf89 63 // full = lum & 0xFFFF;
anhnt2407 0:9ab53ff6cf89 64
anhnt2407 0:9ab53ff6cf89 65 // PC_PRINTLNXY(1,"Visible: %d",full - ir);
anhnt2407 0:9ab53ff6cf89 66 // PC_PRINTLNXY(1,"Full Spectrum: %d",full);
anhnt2407 0:9ab53ff6cf89 67 // PC_PRINTLNXY(1,"Infrared: %d",ir);
anhnt2407 0:9ab53ff6cf89 68
anhnt2407 0:9ab53ff6cf89 69 // wait(1);
anhnt2407 0:9ab53ff6cf89 70 PC_PRINTLNX(1,"----------COMPLETE-------------");
anhnt2407 0:9ab53ff6cf89 71 }
anhnt2407 0:9ab53ff6cf89 72 }