Qisi Wang
/
ADJD-S311_HelloWorld
A simple hello_world program for the ADJD-S311 color sensor
main.cpp@1:01a7d7326f80, 2014-03-24 (annotated)
- Committer:
- CheeseW
- Date:
- Mon Mar 24 05:35:39 2014 +0000
- Revision:
- 1:01a7d7326f80
- Parent:
- 0:b3232f332f48
Out for publication!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CheeseW | 0:b3232f332f48 | 1 | #include "mbed.h" |
CheeseW | 0:b3232f332f48 | 2 | #include "ADJDs311.h" |
CheeseW | 0:b3232f332f48 | 3 | |
CheeseW | 1:01a7d7326f80 | 4 | /** |
CheeseW | 1:01a7d7326f80 | 5 | * An example program using the ADJD-S311 color sensor to obtain the reflexive |
CheeseW | 1:01a7d7326f80 | 6 | * color of objects. |
CheeseW | 1:01a7d7326f80 | 7 | */ |
CheeseW | 1:01a7d7326f80 | 8 | |
CheeseW | 0:b3232f332f48 | 9 | ADJDs311 colorSensor(p28, p27, p23); |
CheeseW | 0:b3232f332f48 | 10 | Serial pc(USBTX, USBRX); |
CheeseW | 0:b3232f332f48 | 11 | |
CheeseW | 0:b3232f332f48 | 12 | int main() { |
CheeseW | 0:b3232f332f48 | 13 | RGBC color; |
CheeseW | 0:b3232f332f48 | 14 | RGBC cap; |
CheeseW | 0:b3232f332f48 | 15 | RGBC inte; |
CheeseW | 0:b3232f332f48 | 16 | RGBC offset; |
CheeseW | 0:b3232f332f48 | 17 | |
CheeseW | 0:b3232f332f48 | 18 | |
CheeseW | 0:b3232f332f48 | 19 | colorSensor.ledMode(true); // turn on the on board led |
CheeseW | 0:b3232f332f48 | 20 | pc.printf("Calibartion started.\n\rPress any key to continue...\n\r"); |
CheeseW | 0:b3232f332f48 | 21 | |
CheeseW | 0:b3232f332f48 | 22 | while(!pc.readable()); // waiting for the setup of calibration (the brightest condition to be measued) |
CheeseW | 0:b3232f332f48 | 23 | |
CheeseW | 0:b3232f332f48 | 24 | colorSensor.calibrate(); // calibrate the sensor to get the optimised gain |
CheeseW | 0:b3232f332f48 | 25 | |
CheeseW | 0:b3232f332f48 | 26 | while(pc.readable()) // discard extra char input |
CheeseW | 0:b3232f332f48 | 27 | { |
CheeseW | 0:b3232f332f48 | 28 | pc.getc(); |
CheeseW | 0:b3232f332f48 | 29 | } |
CheeseW | 0:b3232f332f48 | 30 | |
CheeseW | 0:b3232f332f48 | 31 | pc.printf("Getting offset\n\rPress any key to continue...\n\r"); |
CheeseW | 0:b3232f332f48 | 32 | while(!pc.readable()); // waiting for the set up of calibration (the brightest condition to be measued) |
CheeseW | 0:b3232f332f48 | 33 | |
CheeseW | 0:b3232f332f48 | 34 | offset = colorSensor.setOffset(); // get and set the color offset to compensate on board LED color |
CheeseW | 0:b3232f332f48 | 35 | |
CheeseW | 0:b3232f332f48 | 36 | while(pc.readable()) // discard extra char input |
CheeseW | 0:b3232f332f48 | 37 | { |
CheeseW | 0:b3232f332f48 | 38 | pc.getc(); |
CheeseW | 0:b3232f332f48 | 39 | } |
CheeseW | 0:b3232f332f48 | 40 | |
CheeseW | 0:b3232f332f48 | 41 | cap = colorSensor.getColorCap(); // get the gain of the capacitors |
CheeseW | 0:b3232f332f48 | 42 | inte = colorSensor.getColorInt(); // get the gain of integration time slot |
CheeseW | 0:b3232f332f48 | 43 | pc.printf("\n\n\n\rCalibartion completed.\n\r"); |
CheeseW | 0:b3232f332f48 | 44 | pc.printf("\nCaps:\t\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", cap.red, cap.green, cap.blue, cap.clear); |
CheeseW | 0:b3232f332f48 | 45 | pc.printf("Ints:\t\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", inte.red, inte.green, inte.blue, inte.clear); |
CheeseW | 0:b3232f332f48 | 46 | pc.printf("Offsets:\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", offset.red, offset.green, offset.blue, offset.clear); |
CheeseW | 0:b3232f332f48 | 47 | pc.printf("\nPress any key to continue\n\r"); |
CheeseW | 0:b3232f332f48 | 48 | while(!pc.readable()); // waiting calibration and offset info confirmation |
CheeseW | 0:b3232f332f48 | 49 | while(pc.readable()) // discard extra char input |
CheeseW | 0:b3232f332f48 | 50 | { |
CheeseW | 0:b3232f332f48 | 51 | pc.getc(); |
CheeseW | 0:b3232f332f48 | 52 | } |
CheeseW | 0:b3232f332f48 | 53 | colorSensor.ledMode(true); // node that wetOffset will turn off the on board LED automatically. Need to turn it on again |
CheeseW | 0:b3232f332f48 | 54 | while(1) { |
CheeseW | 0:b3232f332f48 | 55 | color = colorSensor.read(); // get the rgb and clear data from sensor |
CheeseW | 0:b3232f332f48 | 56 | pc.printf("red: %4d,\tgreen: %4d,\tblue: %4d,\tclear: %4d\r", color.red, color.green, color.blue, color.clear); |
CheeseW | 0:b3232f332f48 | 57 | wait(0.15); |
CheeseW | 0:b3232f332f48 | 58 | } |
CheeseW | 0:b3232f332f48 | 59 | } |