A simple hello_world program for the ADJD-S311 color sensor

Dependencies:   ADJDs311 mbed

Committer:
CheeseW
Date:
Mon Mar 24 05:33:13 2014 +0000
Revision:
0:b3232f332f48
Child:
1:01a7d7326f80
Out for publication!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CheeseW 0:b3232f332f48 1 #include "mbed.h"
CheeseW 0:b3232f332f48 2 #include "ADJDs311.h"
CheeseW 0:b3232f332f48 3
CheeseW 0:b3232f332f48 4 ADJDs311 colorSensor(p28, p27, p23);
CheeseW 0:b3232f332f48 5 Serial pc(USBTX, USBRX);
CheeseW 0:b3232f332f48 6
CheeseW 0:b3232f332f48 7 int main() {
CheeseW 0:b3232f332f48 8 RGBC color;
CheeseW 0:b3232f332f48 9 RGBC cap;
CheeseW 0:b3232f332f48 10 RGBC inte;
CheeseW 0:b3232f332f48 11 RGBC offset;
CheeseW 0:b3232f332f48 12
CheeseW 0:b3232f332f48 13
CheeseW 0:b3232f332f48 14 colorSensor.ledMode(true); // turn on the on board led
CheeseW 0:b3232f332f48 15 pc.printf("Calibartion started.\n\rPress any key to continue...\n\r");
CheeseW 0:b3232f332f48 16
CheeseW 0:b3232f332f48 17 while(!pc.readable()); // waiting for the setup of calibration (the brightest condition to be measued)
CheeseW 0:b3232f332f48 18
CheeseW 0:b3232f332f48 19 colorSensor.calibrate(); // calibrate the sensor to get the optimised gain
CheeseW 0:b3232f332f48 20
CheeseW 0:b3232f332f48 21 while(pc.readable()) // discard extra char input
CheeseW 0:b3232f332f48 22 {
CheeseW 0:b3232f332f48 23 pc.getc();
CheeseW 0:b3232f332f48 24 }
CheeseW 0:b3232f332f48 25
CheeseW 0:b3232f332f48 26 pc.printf("Getting offset\n\rPress any key to continue...\n\r");
CheeseW 0:b3232f332f48 27 while(!pc.readable()); // waiting for the set up of calibration (the brightest condition to be measued)
CheeseW 0:b3232f332f48 28
CheeseW 0:b3232f332f48 29 offset = colorSensor.setOffset(); // get and set the color offset to compensate on board LED color
CheeseW 0:b3232f332f48 30
CheeseW 0:b3232f332f48 31 while(pc.readable()) // discard extra char input
CheeseW 0:b3232f332f48 32 {
CheeseW 0:b3232f332f48 33 pc.getc();
CheeseW 0:b3232f332f48 34 }
CheeseW 0:b3232f332f48 35
CheeseW 0:b3232f332f48 36 cap = colorSensor.getColorCap(); // get the gain of the capacitors
CheeseW 0:b3232f332f48 37 inte = colorSensor.getColorInt(); // get the gain of integration time slot
CheeseW 0:b3232f332f48 38 pc.printf("\n\n\n\rCalibartion completed.\n\r");
CheeseW 0:b3232f332f48 39 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 40 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 41 pc.printf("Offsets:\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", offset.red, offset.green, offset.blue, offset.clear);
CheeseW 0:b3232f332f48 42 pc.printf("\nPress any key to continue\n\r");
CheeseW 0:b3232f332f48 43 while(!pc.readable()); // waiting calibration and offset info confirmation
CheeseW 0:b3232f332f48 44 while(pc.readable()) // discard extra char input
CheeseW 0:b3232f332f48 45 {
CheeseW 0:b3232f332f48 46 pc.getc();
CheeseW 0:b3232f332f48 47 }
CheeseW 0:b3232f332f48 48 colorSensor.ledMode(true); // node that wetOffset will turn off the on board LED automatically. Need to turn it on again
CheeseW 0:b3232f332f48 49 while(1) {
CheeseW 0:b3232f332f48 50 color = colorSensor.read(); // get the rgb and clear data from sensor
CheeseW 0:b3232f332f48 51 pc.printf("red: %4d,\tgreen: %4d,\tblue: %4d,\tclear: %4d\r", color.red, color.green, color.blue, color.clear);
CheeseW 0:b3232f332f48 52 wait(0.15);
CheeseW 0:b3232f332f48 53 }
CheeseW 0:b3232f332f48 54 }