Measuring the reflected light by using a TCRT5000 sensor

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 AnalogIn cds(A0);                           // Analog input at PA0
00004 DigitalOut led(D10);                        // Led contolled by D10
00005 
00006 uint32_t measLight(int nmeas)
00007 {
00008     uint32_t sum0, sum1, diff;
00009     led = 0;
00010     sum0 = 0;
00011     for (int i = 0; i < nmeas; i++) {
00012         sum0 += cds.read_u16();
00013     }
00014     led = 1;
00015     sum1 = 0;
00016     for (int i = 0; i < nmeas; i++) {
00017         sum1 += cds.read_u16();
00018     }
00019     if (sum0 > sum1) {
00020         diff= 0;
00021     } else {
00022         diff = (sum1 - sum0) / nmeas;
00023     }
00024     printf("L1: %5d  L0: %5d  Diff: %5d\r\n",sum1/nmeas, sum0/nmeas, diff);
00025     return diff;
00026 }
00027 
00028 int main()
00029 {
00030     printf("\r\n Lab03_light_sensor - with averaging\r\n");
00031     while(1) {
00032         measLight(1000);
00033         wait(2);
00034     }
00035 }