![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed
main.cpp@0:fe5cb0a8fc5a, 2015-10-18 (annotated)
- Committer:
- dreamselec
- Date:
- Sun Oct 18 16:34:27 2015 +0000
- Revision:
- 0:fe5cb0a8fc5a
- Child:
- 1:92a2a5ef65a8
Fully featured colour sensor config program.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreamselec | 0:fe5cb0a8fc5a | 1 | #include "mbed.h" |
dreamselec | 0:fe5cb0a8fc5a | 2 | #include "WattBob_TextLCD.h" |
dreamselec | 0:fe5cb0a8fc5a | 3 | #include "TCS3472_I2C.h" |
dreamselec | 0:fe5cb0a8fc5a | 4 | #include "MCP23017.h" |
dreamselec | 0:fe5cb0a8fc5a | 5 | #include <string> |
dreamselec | 0:fe5cb0a8fc5a | 6 | |
dreamselec | 0:fe5cb0a8fc5a | 7 | #define BACKLIGHT_ON(INTERFACE) INTERFACE->write_bit(1, 4); |
dreamselec | 0:fe5cb0a8fc5a | 8 | #define BACKLIGHT_OFF(INTERFACE) INTERFACE->write_bit(0, 4); |
dreamselec | 0:fe5cb0a8fc5a | 9 | |
dreamselec | 0:fe5cb0a8fc5a | 10 | #define PRINT_LCD(TEXT) lcd->printf(TEXT); |
dreamselec | 0:fe5cb0a8fc5a | 11 | #define CLEAR_LCD() lcd->cls(); |
dreamselec | 0:fe5cb0a8fc5a | 12 | #define LCDFL() lcd->locate(0,0); |
dreamselec | 0:fe5cb0a8fc5a | 13 | #define LCDSL() lcd->locate(1,0); |
dreamselec | 0:fe5cb0a8fc5a | 14 | #define CLEAR_LEDS() i2cport->write_bit(0, 12); i2cport->write_bit(0, 13); i2cport->write_bit(0, 14); i2cport->write_bit(0, 15); |
dreamselec | 0:fe5cb0a8fc5a | 15 | |
dreamselec | 0:fe5cb0a8fc5a | 16 | DigitalOut myled(LED1); |
dreamselec | 0:fe5cb0a8fc5a | 17 | |
dreamselec | 0:fe5cb0a8fc5a | 18 | MCP23017 *i2cport; |
dreamselec | 0:fe5cb0a8fc5a | 19 | WattBob_TextLCD *lcd; |
dreamselec | 0:fe5cb0a8fc5a | 20 | |
dreamselec | 0:fe5cb0a8fc5a | 21 | TCS3472_I2C rgb_sensor(p9, p10); |
dreamselec | 0:fe5cb0a8fc5a | 22 | |
dreamselec | 0:fe5cb0a8fc5a | 23 | int main(){ |
dreamselec | 0:fe5cb0a8fc5a | 24 | i2cport = new MCP23017(p9, p10, 0x40); |
dreamselec | 0:fe5cb0a8fc5a | 25 | lcd = new WattBob_TextLCD(i2cport); |
dreamselec | 0:fe5cb0a8fc5a | 26 | BACKLIGHT_ON(i2cport); |
dreamselec | 0:fe5cb0a8fc5a | 27 | int rgb_results[4]; |
dreamselec | 0:fe5cb0a8fc5a | 28 | |
dreamselec | 0:fe5cb0a8fc5a | 29 | CLEAR_LCD(); |
dreamselec | 0:fe5cb0a8fc5a | 30 | LCDFL(); |
dreamselec | 0:fe5cb0a8fc5a | 31 | PRINT_LCD("Colour Sensor"); |
dreamselec | 0:fe5cb0a8fc5a | 32 | LCDSL(); |
dreamselec | 0:fe5cb0a8fc5a | 33 | PRINT_LCD("Stay in school!"); |
dreamselec | 0:fe5cb0a8fc5a | 34 | |
dreamselec | 0:fe5cb0a8fc5a | 35 | wait(2.0); |
dreamselec | 0:fe5cb0a8fc5a | 36 | CLEAR_LCD(); |
dreamselec | 0:fe5cb0a8fc5a | 37 | |
dreamselec | 0:fe5cb0a8fc5a | 38 | i2cport->config(0x0F00, 0x0F00, 0x0F00); |
dreamselec | 0:fe5cb0a8fc5a | 39 | |
dreamselec | 0:fe5cb0a8fc5a | 40 | rgb_sensor.enablePowerAndRGBC(); |
dreamselec | 0:fe5cb0a8fc5a | 41 | rgb_sensor.setIntegrationTime(100); |
dreamselec | 0:fe5cb0a8fc5a | 42 | i2cport->write_bit(1, 15); |
dreamselec | 0:fe5cb0a8fc5a | 43 | |
dreamselec | 0:fe5cb0a8fc5a | 44 | myled = 1; |
dreamselec | 0:fe5cb0a8fc5a | 45 | |
dreamselec | 0:fe5cb0a8fc5a | 46 | Serial pc(USBTX, USBRX); |
dreamselec | 0:fe5cb0a8fc5a | 47 | |
dreamselec | 0:fe5cb0a8fc5a | 48 | while (1){ |
dreamselec | 0:fe5cb0a8fc5a | 49 | rgb_sensor.getAllColors(rgb_results); |
dreamselec | 0:fe5cb0a8fc5a | 50 | CLEAR_LCD(); |
dreamselec | 0:fe5cb0a8fc5a | 51 | |
dreamselec | 0:fe5cb0a8fc5a | 52 | LCDFL(); |
dreamselec | 0:fe5cb0a8fc5a | 53 | lcd->printf("R:%d, G:%d", rgb_results[0], rgb_results[1]); |
dreamselec | 0:fe5cb0a8fc5a | 54 | LCDSL(); |
dreamselec | 0:fe5cb0a8fc5a | 55 | lcd->printf("B:%d, A:%d", rgb_results[2], rgb_results[3]); |
dreamselec | 0:fe5cb0a8fc5a | 56 | |
dreamselec | 0:fe5cb0a8fc5a | 57 | if (i2cport->read_bit(8)){ |
dreamselec | 0:fe5cb0a8fc5a | 58 | rgb_sensor.setIntegrationTime(50); |
dreamselec | 0:fe5cb0a8fc5a | 59 | |
dreamselec | 0:fe5cb0a8fc5a | 60 | CLEAR_LCD(); |
dreamselec | 0:fe5cb0a8fc5a | 61 | CLEAR_LEDS(); |
dreamselec | 0:fe5cb0a8fc5a | 62 | LCDFL(); |
dreamselec | 0:fe5cb0a8fc5a | 63 | |
dreamselec | 0:fe5cb0a8fc5a | 64 | PRINT_LCD("I-Time:50"); |
dreamselec | 0:fe5cb0a8fc5a | 65 | pc.printf("Integration Time set to %dms\r\n", 50); |
dreamselec | 0:fe5cb0a8fc5a | 66 | pc.printf("R,G,B,A,IT\r\n"); |
dreamselec | 0:fe5cb0a8fc5a | 67 | |
dreamselec | 0:fe5cb0a8fc5a | 68 | i2cport->write_bit(1, 12); |
dreamselec | 0:fe5cb0a8fc5a | 69 | wait(1.0); |
dreamselec | 0:fe5cb0a8fc5a | 70 | } else if (i2cport->read_bit(9)){ |
dreamselec | 0:fe5cb0a8fc5a | 71 | rgb_sensor.setIntegrationTime(200); |
dreamselec | 0:fe5cb0a8fc5a | 72 | |
dreamselec | 0:fe5cb0a8fc5a | 73 | CLEAR_LCD(); |
dreamselec | 0:fe5cb0a8fc5a | 74 | CLEAR_LEDS(); |
dreamselec | 0:fe5cb0a8fc5a | 75 | LCDFL(); |
dreamselec | 0:fe5cb0a8fc5a | 76 | |
dreamselec | 0:fe5cb0a8fc5a | 77 | PRINT_LCD("I-Time:200"); |
dreamselec | 0:fe5cb0a8fc5a | 78 | pc.printf("Integration Time set to %dms\r\n", 200); |
dreamselec | 0:fe5cb0a8fc5a | 79 | pc.printf("R,G,B,A,IT\r\n"); |
dreamselec | 0:fe5cb0a8fc5a | 80 | |
dreamselec | 0:fe5cb0a8fc5a | 81 | i2cport->write_bit(1, 13); |
dreamselec | 0:fe5cb0a8fc5a | 82 | wait(1); |
dreamselec | 0:fe5cb0a8fc5a | 83 | } else if (i2cport->read_bit(11)){ |
dreamselec | 0:fe5cb0a8fc5a | 84 | rgb_sensor.setIntegrationTime(100); |
dreamselec | 0:fe5cb0a8fc5a | 85 | |
dreamselec | 0:fe5cb0a8fc5a | 86 | CLEAR_LCD(); |
dreamselec | 0:fe5cb0a8fc5a | 87 | CLEAR_LEDS(); |
dreamselec | 0:fe5cb0a8fc5a | 88 | LCDFL(); |
dreamselec | 0:fe5cb0a8fc5a | 89 | |
dreamselec | 0:fe5cb0a8fc5a | 90 | PRINT_LCD("I-Time:100"); |
dreamselec | 0:fe5cb0a8fc5a | 91 | pc.printf("Integration Time set to %dms\r\n", 100); |
dreamselec | 0:fe5cb0a8fc5a | 92 | pc.printf("R,G,B,A,IT\r\n"); |
dreamselec | 0:fe5cb0a8fc5a | 93 | |
dreamselec | 0:fe5cb0a8fc5a | 94 | i2cport->write_bit(1, 15); |
dreamselec | 0:fe5cb0a8fc5a | 95 | wait(1.0); |
dreamselec | 0:fe5cb0a8fc5a | 96 | } else if (i2cport->read_bit(10)){ |
dreamselec | 0:fe5cb0a8fc5a | 97 | char bufferIT[6]; |
dreamselec | 0:fe5cb0a8fc5a | 98 | float fBufferIT; |
dreamselec | 0:fe5cb0a8fc5a | 99 | char *ITQ = "I"; |
dreamselec | 0:fe5cb0a8fc5a | 100 | |
dreamselec | 0:fe5cb0a8fc5a | 101 | LCDFL(); |
dreamselec | 0:fe5cb0a8fc5a | 102 | PRINT_LCD("Reading stdin"); |
dreamselec | 0:fe5cb0a8fc5a | 103 | LCDSL(); |
dreamselec | 0:fe5cb0a8fc5a | 104 | PRINT_LCD("\'I\' for I-Time"); |
dreamselec | 0:fe5cb0a8fc5a | 105 | pc.printf("Reading stdin\r\n"); |
dreamselec | 0:fe5cb0a8fc5a | 106 | pc.scanf("%5s", bufferIT); |
dreamselec | 0:fe5cb0a8fc5a | 107 | |
dreamselec | 0:fe5cb0a8fc5a | 108 | if (bufferIT[0] == *ITQ){ |
dreamselec | 0:fe5cb0a8fc5a | 109 | pc.printf("I-Time:%d\r\n", rgb_sensor.readIntegrationTime()); |
dreamselec | 0:fe5cb0a8fc5a | 110 | }else{ |
dreamselec | 0:fe5cb0a8fc5a | 111 | CLEAR_LEDS(); |
dreamselec | 0:fe5cb0a8fc5a | 112 | |
dreamselec | 0:fe5cb0a8fc5a | 113 | fBufferIT = atof(bufferIT); |
dreamselec | 0:fe5cb0a8fc5a | 114 | rgb_sensor.setIntegrationTime(fBufferIT); |
dreamselec | 0:fe5cb0a8fc5a | 115 | |
dreamselec | 0:fe5cb0a8fc5a | 116 | pc.printf("Integration Time set to %fms\r\n", fBufferIT); |
dreamselec | 0:fe5cb0a8fc5a | 117 | lcd->printf("I-Time:%d\r\n", fBufferIT); |
dreamselec | 0:fe5cb0a8fc5a | 118 | pc.printf("R,G,B,A,IT\r\n"); |
dreamselec | 0:fe5cb0a8fc5a | 119 | |
dreamselec | 0:fe5cb0a8fc5a | 120 | i2cport->write_bit(1, 14); |
dreamselec | 0:fe5cb0a8fc5a | 121 | } |
dreamselec | 0:fe5cb0a8fc5a | 122 | |
dreamselec | 0:fe5cb0a8fc5a | 123 | } |
dreamselec | 0:fe5cb0a8fc5a | 124 | //pc.printf("R:%d, G:%d, B:%d, A:%d. Integration Time:%d\r\n", rgb_results[0], rgb_results[1], rgb_results[2], rgb_results[3], rgb_sensor.readIntegrationTime()); |
dreamselec | 0:fe5cb0a8fc5a | 125 | pc.printf("%d,%d,%d,%d,%d\r\n", rgb_results[0], rgb_results[1], rgb_results[2], rgb_results[3], rgb_sensor.readIntegrationTime()); |
dreamselec | 0:fe5cb0a8fc5a | 126 | wait(2.0); |
dreamselec | 0:fe5cb0a8fc5a | 127 | } |
dreamselec | 0:fe5cb0a8fc5a | 128 | } |