Jian Chen
/
mini
demo program of color sensor
Revision 1:131a940a952c, committed 2013-10-17
- Comitter:
- chenjian221
- Date:
- Thu Oct 17 00:05:21 2013 +0000
- Parent:
- 0:07ac8eb1700b
- Commit message:
- add comments, change enable from DigitalOut to PwmOut
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Wed Oct 16 04:32:34 2013 +0000 +++ b/main.cpp Thu Oct 17 00:05:21 2013 +0000 @@ -1,14 +1,20 @@ -#include "mbed.h" +/* This program demonstrates the ability of a color sensor made by + * Atlas Scientific through a shiftbrite LED chasing game. + * Bi Ge bge6@gatech.edu + */ + +#include "mbed.h" DigitalOut latch(p15); -DigitalOut enable(p16); +PwmOut enable(p23); // use PWM instead of DigitalOut to dim the LED Serial color_sensor(p9, p10); DigitalOut out_latch(p22); -DigitalOut out_enable(p21); -SPI spi(p11, p12, p13); -SPI out_spi(p5, p6, p7); +PwmOut out_enable(p21); +SPI spi(p11, p12, p13); // the random shiftbrite +SPI out_spi(p5, p6, p7); // this shiftbrite is attached to the color sensor +// http://mbed.org/users/4180_1/notebook/shiftbrite1/ void RGB_LED(int red, int green, int blue) { unsigned int low_color=0; @@ -20,7 +26,9 @@ latch=1; latch=0; } - +/* This function is exactly the same as the one above, the only + * difference is now it outputs value to the second shiftbrite + */ void out_RGB_LED(int red, int green, int blue) { unsigned int low_color=0; @@ -34,8 +42,9 @@ } void split_RGB(char* color_str) { - char *save_ptr; - char *p = strtok_r(color_str, ",", &save_ptr); + char *save_ptr; // this ptr is used by strtok_r to save + // the current parsing state + char *p = strtok_r(color_str, ",", &save_ptr); // parse with "," int red = strtol(p, NULL, 10); p = strtok_r(NULL, ",", &save_ptr); int green = strtol(p, NULL, 10); @@ -47,12 +56,17 @@ void get_color() { if(color_sensor.readable()) { - printf("get into the function\n"); char str[11]; int i = 0; char c; + /* + * The color sensor sends its output in form + * "rrr,ggg,bbb\r" in char. It can also output the + * light intensity. See product document. + */ + while(c=color_sensor.getc()) { - //printf("read: %c\n", c); + // wait for the carriage return if(c!='\r') { str[i] = c; i++; @@ -60,10 +74,6 @@ break; } } - //color_sensor.scanf("%s", &str); - printf("another read: "); - printf("%s\n", str); - printf("i=%d\n", i); split_RGB(str); } return; @@ -77,9 +87,9 @@ spi.frequency(500000); out_spi.format(16,0); out_spi.frequency(500000); - enable=0; + enable=0.5; // set PWM to half, full on is too bright latch=0; - out_enable=0; + out_enable=0.5; out_latch=0; color_sensor.baud(38400); wait(2); @@ -88,10 +98,12 @@ green = rand() % 50; blue = rand() % 50; RGB_LED( red, green, blue); - + /* IMPORTANT: The color sensor can only take a measurement every 620ms + * Have to wait for the instruction to complete. + */ get_color(); wait(1); } - + }