The MBED firmware used on the Chipin sorter, developed over 12 weeks for a 3rd year university systems project. Chipin is a token sorter, it sorts tokens by colours and dispenses them to order through an online booking system and card reader. This program interfaces with an FPGA, PC and LCD screen to control the sorter. The sorter has an operation mode where it can process orders when a card is entered into the machine. There is also a maintenance mode where the device responds to maintenance instructions such as 'dispense all'. More information at http://www.ionsystems.uk/

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed

Committer:
IonSystems
Date:
Tue Nov 04 17:30:49 2014 +0000
Revision:
3:97668a4cd69d
Parent:
2:168850019d5a
Child:
4:f3be545b3826
Integrated card reader and colour sensor. Serial communication currently disabled.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IonSystems 0:8d54ffcf256e 1 #include "mbed.h"
IonSystems 0:8d54ffcf256e 2 #include "MCP23017.h"
IonSystems 0:8d54ffcf256e 3 #include "WattBob_TextLCD.h"
IonSystems 0:8d54ffcf256e 4 #include "TCS3472_I2C.h"
IonSystems 3:97668a4cd69d 5 #include <string>
IonSystems 0:8d54ffcf256e 6
IonSystems 3:97668a4cd69d 7 //Setup test LEDs
IonSystems 3:97668a4cd69d 8 DigitalOut testLED1(LED1);
IonSystems 3:97668a4cd69d 9 DigitalOut testLED2(LED2);
IonSystems 3:97668a4cd69d 10 DigitalOut testLED3(LED3);
IonSystems 3:97668a4cd69d 11 DigitalOut testLED4(LED4);
IonSystems 3:97668a4cd69d 12
IonSystems 3:97668a4cd69d 13 //Setup Buttons
IonSystems 3:97668a4cd69d 14 DigitalIn testButton1(P1_8);
IonSystems 0:8d54ffcf256e 15
IonSystems 0:8d54ffcf256e 16 //Boolean values to easily enable and disable certain features for individual testing
IonSystems 3:97668a4cd69d 17 /*bool colourSensor = true;
IonSystems 0:8d54ffcf256e 18 bool cardReader = true;
IonSystems 0:8d54ffcf256e 19 bool sorter = true;
IonSystems 0:8d54ffcf256e 20 bool dispensor = true;
IonSystems 3:97668a4cd69d 21 */
IonSystems 0:8d54ffcf256e 22 /*
IonSystems 0:8d54ffcf256e 23 There are two high level states representing the two main
IonSystems 0:8d54ffcf256e 24 functionalities of the MBED: Maintenance mode and Operation mode.
IonSystems 0:8d54ffcf256e 25
IonSystems 0:8d54ffcf256e 26 When operationMode is true, the MBED is in operation mode.
IonSystems 0:8d54ffcf256e 27 When operationMode is false, the MBED is in maintenance mode.
IonSystems 0:8d54ffcf256e 28 */
IonSystems 0:8d54ffcf256e 29
IonSystems 0:8d54ffcf256e 30 bool operationMode = true; //the MBED starts in operation mode.
IonSystems 0:8d54ffcf256e 31
IonSystems 0:8d54ffcf256e 32 #define BACK_LIGHT_ON(INTERFACE) INTERFACE->write_bit(1,BL_BIT)
IonSystems 0:8d54ffcf256e 33 #define BACK_LIGHT_OFF(INTERFACE) INTERFACE->write_bit(0,BL_BIT)
IonSystems 0:8d54ffcf256e 34
IonSystems 0:8d54ffcf256e 35 //Creating all the global variables.
IonSystems 0:8d54ffcf256e 36 MCP23017 *par_port;
IonSystems 0:8d54ffcf256e 37 WattBob_TextLCD *lcd;
IonSystems 0:8d54ffcf256e 38
IonSystems 0:8d54ffcf256e 39 Serial pc(USBTX, USBRX);
IonSystems 0:8d54ffcf256e 40
IonSystems 0:8d54ffcf256e 41 //Setup colour sensor variables
IonSystems 0:8d54ffcf256e 42 int rgb_readings[4];
IonSystems 0:8d54ffcf256e 43 double rMax = 9244;
IonSystems 0:8d54ffcf256e 44 double gMax = 3194;
IonSystems 0:8d54ffcf256e 45 double bMax = 3590;
IonSystems 3:97668a4cd69d 46 TCS3472_I2C rgb_sensor(p28,p27); //p28 =sda, p27=scl
IonSystems 3:97668a4cd69d 47 //thresholds are in the RGB format.
IonSystems 3:97668a4cd69d 48 int redLowerThresholds [3] = {79,23,41};
IonSystems 3:97668a4cd69d 49 int redUpperThresholds [3]= {92,28,49};
IonSystems 3:97668a4cd69d 50 int greenLowerThresholds [3] = {46,81,61};
IonSystems 3:97668a4cd69d 51 int greenUpperThresholds [3]= {57,100,75};
IonSystems 3:97668a4cd69d 52 int blueLowerThresholds [3]= {25,47,47};
IonSystems 3:97668a4cd69d 53 int blueUpperThresholds [3]= {28,52,52};
IonSystems 0:8d54ffcf256e 54
IonSystems 3:97668a4cd69d 55 //setup Card Reader pins
IonSystems 0:8d54ffcf256e 56 DigitalIn cardBit1(p21);
IonSystems 0:8d54ffcf256e 57 DigitalIn cardBit2(p22);
IonSystems 0:8d54ffcf256e 58 DigitalIn cardBit3(p23);
IonSystems 0:8d54ffcf256e 59 DigitalIn cardBit4(p24);
IonSystems 0:8d54ffcf256e 60 DigitalIn cardBit5(p25);
IonSystems 0:8d54ffcf256e 61 DigitalIn cardBit6(p26);
IonSystems 3:97668a4cd69d 62 DigitalIn cardDetect(p29);
IonSystems 3:97668a4cd69d 63 bool cardDataAcquired = false;
IonSystems 3:97668a4cd69d 64 bool colourDataAcquired = false;
IonSystems 3:97668a4cd69d 65 bool chipDetected = false;
IonSystems 0:8d54ffcf256e 66
IonSystems 0:8d54ffcf256e 67 //Setup pins to FPGA
IonSystems 0:8d54ffcf256e 68 DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting.
IonSystems 0:8d54ffcf256e 69 /* The command codes for the three sort select bits(ColourBit1 - ColourBit2)
IonSystems 0:8d54ffcf256e 70 000 Sort red chip
IonSystems 0:8d54ffcf256e 71 001 Sort green chip
IonSystems 0:8d54ffcf256e 72 010 Sort blue chip
IonSystems 0:8d54ffcf256e 73 011 Bin the chip
IonSystems 0:8d54ffcf256e 74 100 Recycle the chip
IonSystems 0:8d54ffcf256e 75 101 Nothing
IonSystems 0:8d54ffcf256e 76 110 Nothing
IonSystems 0:8d54ffcf256e 77 111 Nothing
IonSystems 0:8d54ffcf256e 78 */
IonSystems 0:8d54ffcf256e 79 DigitalOut ColourBit1(p6); //The 3 bits below are select bits for the sorter.
IonSystems 0:8d54ffcf256e 80 DigitalOut ColourBit2(p7);
IonSystems 0:8d54ffcf256e 81 DigitalOut ColourBit3(p8);
IonSystems 0:8d54ffcf256e 82 DigitalOut startDispense(p9); //A positive edge tells the FPGA to start dispensing.
IonSystems 0:8d54ffcf256e 83 /*
IonSystems 0:8d54ffcf256e 84 00 Dispense red chip
IonSystems 0:8d54ffcf256e 85 01 Dispense green chip
IonSystems 0:8d54ffcf256e 86 10 Dispense blue chip
IonSystems 0:8d54ffcf256e 87 11 Nothing
IonSystems 0:8d54ffcf256e 88 */
IonSystems 0:8d54ffcf256e 89 DigitalOut DispenseBit1(p10); //The 2 bits below are select bits for the dispenser.
IonSystems 0:8d54ffcf256e 90 DigitalOut DispenseBit2(p11);
IonSystems 0:8d54ffcf256e 91
IonSystems 3:97668a4cd69d 92 DigitalOut servoTest(p21); //for basic servo test fro deadline day 31/10/14
IonSystems 3:97668a4cd69d 93
IonSystems 3:97668a4cd69d 94 //Global card bits
IonSystems 3:97668a4cd69d 95 int cardValue1 = 0;
IonSystems 3:97668a4cd69d 96 int cardValue2 = 0;
IonSystems 3:97668a4cd69d 97 int cardValue3 = 0;
IonSystems 3:97668a4cd69d 98 int cardValue4 = 0;
IonSystems 3:97668a4cd69d 99 int cardValue5 = 0;
IonSystems 3:97668a4cd69d 100 int cardValue6 = 0;
IonSystems 0:8d54ffcf256e 101
IonSystems 0:8d54ffcf256e 102 //Functions go here
IonSystems 0:8d54ffcf256e 103 void sendCharacter(char ch){
IonSystems 0:8d54ffcf256e 104 pc.putc(ch);
IonSystems 0:8d54ffcf256e 105 }
IonSystems 0:8d54ffcf256e 106
IonSystems 3:97668a4cd69d 107
IonSystems 3:97668a4cd69d 108 void setLEDs(){
IonSystems 3:97668a4cd69d 109 testLED1 = cardDetect;
IonSystems 3:97668a4cd69d 110 testLED4 = true;
IonSystems 3:97668a4cd69d 111 }
IonSystems 3:97668a4cd69d 112
IonSystems 3:97668a4cd69d 113 /* A simple print function for use on the MBED LCD Screen.
IonSystems 3:97668a4cd69d 114 * Will format a char array into two lines and display the char array on the LCD.
IonSystems 3:97668a4cd69d 115 */
IonSystems 3:97668a4cd69d 116 void printLCD(const char * text){
IonSystems 3:97668a4cd69d 117 std::string txt_str = std::string(text); //Convert ther character array to a std::string,
IonSystems 3:97668a4cd69d 118 //to use in the string manipulation functions.
IonSystems 0:8d54ffcf256e 119 lcd->reset();
IonSystems 3:97668a4cd69d 120 if(txt_str.length() > 32){
IonSystems 3:97668a4cd69d 121 int length = txt_str.length();
IonSystems 3:97668a4cd69d 122 txt_str = txt_str.erase(32,length-32);
IonSystems 3:97668a4cd69d 123 lcd->locate(0,0); //Going to new line
IonSystems 3:97668a4cd69d 124 text = txt_str.c_str();
IonSystems 3:97668a4cd69d 125 }
IonSystems 3:97668a4cd69d 126 if(txt_str.length() > 16){
IonSystems 3:97668a4cd69d 127 string line1 = txt_str.substr(0,16);
IonSystems 3:97668a4cd69d 128 string line2 = txt_str.substr(16,16);
IonSystems 3:97668a4cd69d 129 lcd->locate(0,0); //Going to new line
IonSystems 3:97668a4cd69d 130 lcd->printf(line1.c_str());
IonSystems 3:97668a4cd69d 131 lcd->locate(1,0); //Going to new line
IonSystems 3:97668a4cd69d 132 lcd->printf(line2.c_str());
IonSystems 3:97668a4cd69d 133 lcd->locate(0,0); //Going to new line
IonSystems 3:97668a4cd69d 134 }
IonSystems 3:97668a4cd69d 135
IonSystems 0:8d54ffcf256e 136 }
IonSystems 3:97668a4cd69d 137
IonSystems 3:97668a4cd69d 138
IonSystems 3:97668a4cd69d 139 void readButtons(){
IonSystems 3:97668a4cd69d 140 chipDetected = par_port->read_bit(8);
IonSystems 3:97668a4cd69d 141 testLED1 = par_port->read_bit(8);
IonSystems 3:97668a4cd69d 142
IonSystems 3:97668a4cd69d 143 }
IonSystems 3:97668a4cd69d 144 /* Reads all 6 bits from the card IO pins and prints them to the MBED LCD.
IonSystems 3:97668a4cd69d 145 * Stores the values read to the global variables cardValue1 to cardValue6.
IonSystems 3:97668a4cd69d 146 */
IonSystems 3:97668a4cd69d 147 void cardAcquisition(){
IonSystems 3:97668a4cd69d 148 lcd->reset();
IonSystems 3:97668a4cd69d 149 lcd->locate(0,0); //Going to new line
IonSystems 3:97668a4cd69d 150 if(cardBit1) lcd->printf("p1=1,");
IonSystems 3:97668a4cd69d 151 else lcd->printf("p1=0,");
IonSystems 3:97668a4cd69d 152 cardValue1 = cardBit1;
IonSystems 3:97668a4cd69d 153
IonSystems 3:97668a4cd69d 154 if(cardBit2) lcd->printf("p2=1,");
IonSystems 3:97668a4cd69d 155 else lcd->printf("p2=0,");
IonSystems 3:97668a4cd69d 156 cardValue2 = cardBit2;
IonSystems 3:97668a4cd69d 157
IonSystems 3:97668a4cd69d 158 if(cardBit3) lcd->printf("p3=1");
IonSystems 3:97668a4cd69d 159 else lcd->printf("p3=0");
IonSystems 3:97668a4cd69d 160 cardValue3 = cardBit3;
IonSystems 3:97668a4cd69d 161
IonSystems 3:97668a4cd69d 162 lcd->locate(1,0); //Going to new line
IonSystems 3:97668a4cd69d 163
IonSystems 3:97668a4cd69d 164 if(cardBit4) lcd->printf("p4=1");
IonSystems 3:97668a4cd69d 165 else lcd->printf("p4=0,");
IonSystems 3:97668a4cd69d 166 cardValue4 = cardBit4;
IonSystems 3:97668a4cd69d 167
IonSystems 3:97668a4cd69d 168
IonSystems 3:97668a4cd69d 169 if(cardBit5) lcd->printf("p5=1,");
IonSystems 3:97668a4cd69d 170 else lcd->printf("p5=0,");
IonSystems 3:97668a4cd69d 171 cardValue5 = cardBit5;
IonSystems 3:97668a4cd69d 172
IonSystems 3:97668a4cd69d 173 if(cardBit6) lcd->printf("p6=1");
IonSystems 3:97668a4cd69d 174 else lcd->printf("p6=0");
IonSystems 3:97668a4cd69d 175 cardValue6 = cardBit6;
IonSystems 3:97668a4cd69d 176
IonSystems 3:97668a4cd69d 177 cardDataAcquired = true;
IonSystems 3:97668a4cd69d 178
IonSystems 3:97668a4cd69d 179 }
IonSystems 1:a8a01df48d1a 180 /* valueToChar(int value):
IonSystems 1:a8a01df48d1a 181 Expects an int value what it 0 or 1.
IonSystems 1:a8a01df48d1a 182 Returns the int value as a char.
IonSystems 1:a8a01df48d1a 183 0 will return '0'
IonSystems 1:a8a01df48d1a 184 1 will return '1'
IonSystems 1:a8a01df48d1a 185 Used to read the bits in the card reader.
IonSystems 1:a8a01df48d1a 186 */
IonSystems 0:8d54ffcf256e 187 char valueToChar(int value){
IonSystems 1:a8a01df48d1a 188 if(value == 0){
IonSystems 0:8d54ffcf256e 189 return '0';
IonSystems 1:a8a01df48d1a 190 }else if(value == 1){
IonSystems 1:a8a01df48d1a 191 return '1';
IonSystems 1:a8a01df48d1a 192 }else{
IonSystems 1:a8a01df48d1a 193 return NULL;
IonSystems 1:a8a01df48d1a 194 }
IonSystems 0:8d54ffcf256e 195 }
IonSystems 0:8d54ffcf256e 196 /* readBit(int bitNumber):
IonSystems 0:8d54ffcf256e 197 Takes a bit number between 1 and 6 inclusive.
IonSystems 0:8d54ffcf256e 198 Returns the value of the cardBit associated with the bit number as a char.
IonSystems 0:8d54ffcf256e 199 */
IonSystems 0:8d54ffcf256e 200 char readBit(int bitNumber){
IonSystems 0:8d54ffcf256e 201 int value = 0;
IonSystems 0:8d54ffcf256e 202 switch(bitNumber){
IonSystems 0:8d54ffcf256e 203 case 1:
IonSystems 0:8d54ffcf256e 204 value = cardBit1;
IonSystems 0:8d54ffcf256e 205 break;
IonSystems 0:8d54ffcf256e 206 case 2:
IonSystems 0:8d54ffcf256e 207 value = cardBit2;
IonSystems 0:8d54ffcf256e 208 break;
IonSystems 0:8d54ffcf256e 209 case 3:
IonSystems 0:8d54ffcf256e 210 value = cardBit3;
IonSystems 0:8d54ffcf256e 211 break;
IonSystems 0:8d54ffcf256e 212 case 4:
IonSystems 0:8d54ffcf256e 213 value = cardBit4;
IonSystems 0:8d54ffcf256e 214 break;
IonSystems 0:8d54ffcf256e 215 case 5:
IonSystems 0:8d54ffcf256e 216 value = cardBit5;
IonSystems 0:8d54ffcf256e 217 break;
IonSystems 0:8d54ffcf256e 218 case 6:
IonSystems 0:8d54ffcf256e 219 value = cardBit6;
IonSystems 0:8d54ffcf256e 220 break;
IonSystems 0:8d54ffcf256e 221 }
IonSystems 0:8d54ffcf256e 222 return valueToChar(value);
IonSystems 0:8d54ffcf256e 223 }
IonSystems 1:a8a01df48d1a 224 /* processMessage(char c):
IonSystems 1:a8a01df48d1a 225 Runs when the MBED recieves a character from the PC.
IonSystems 1:a8a01df48d1a 226 Carries out tasks based on the char value.
IonSystems 1:a8a01df48d1a 227 An acknowledge signal is always send back to the PC so that it knows the MBED
IonSystems 1:a8a01df48d1a 228 has recieved the command.
IonSystems 1:a8a01df48d1a 229 Has two case statements: one for maintenance mode and one for operation mode.
IonSystems 0:8d54ffcf256e 230 */
IonSystems 0:8d54ffcf256e 231
IonSystems 0:8d54ffcf256e 232 void processMessage(char c){
IonSystems 3:97668a4cd69d 233 switch(c){
IonSystems 0:8d54ffcf256e 234 case 'a' : //servo 1 clockwise
IonSystems 0:8d54ffcf256e 235 sendCharacter('a'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 236 printLCD("S1 clockwise");
IonSystems 0:8d54ffcf256e 237 break;
IonSystems 0:8d54ffcf256e 238 case 'b' : //servo 1 anticlockwise
IonSystems 0:8d54ffcf256e 239 sendCharacter('b'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 240 printLCD("S1 anticlockwise");
IonSystems 0:8d54ffcf256e 241 break;
IonSystems 0:8d54ffcf256e 242 case 'c':
IonSystems 0:8d54ffcf256e 243 sendCharacter('c'); //bounce back for acknowledgement
IonSystems 0:8d54ffcf256e 244 printLCD("S1 stop");
IonSystems 0:8d54ffcf256e 245 break;
IonSystems 0:8d54ffcf256e 246 case 'C':// Read card command
IonSystems 0:8d54ffcf256e 247 sendCharacter('R'); //Notify PC that command has been recieved
IonSystems 0:8d54ffcf256e 248 printLCD("Reading Card");
IonSystems 0:8d54ffcf256e 249 //read card here
IonSystems 0:8d54ffcf256e 250 char value = ' ';
IonSystems 0:8d54ffcf256e 251 for(int num = 1;num <= 6;num++){
IonSystems 0:8d54ffcf256e 252 value = readBit(num);
IonSystems 0:8d54ffcf256e 253 // sendCharacter('R');
IonSystems 0:8d54ffcf256e 254 sendCharacter(value);
IonSystems 0:8d54ffcf256e 255 printLCD("Bit read");
IonSystems 0:8d54ffcf256e 256 //wait(0.1);
IonSystems 0:8d54ffcf256e 257 }
IonSystems 0:8d54ffcf256e 258 sendCharacter('C'); //Tells PC that the card has been read
IonSystems 3:97668a4cd69d 259 printLCD("Card read done");
IonSystems 0:8d54ffcf256e 260 break;
IonSystems 0:8d54ffcf256e 261 case 'X':
IonSystems 0:8d54ffcf256e 262 sendCharacter('X'); //Notify the PC that this is an MBED
IonSystems 0:8d54ffcf256e 263 printLCD("Connected to PC");
IonSystems 0:8d54ffcf256e 264 break;
IonSystems 2:168850019d5a 265 case 'd': //Dispense a chip
IonSystems 3:97668a4cd69d 266 sendCharacter('D');
IonSystems 2:168850019d5a 267 printLCD("Dispensing chip");
IonSystems 2:168850019d5a 268
IonSystems 2:168850019d5a 269 //read from color sensor
IonSystems 2:168850019d5a 270 //convert readings to colour
IonSystems 2:168850019d5a 271 //convert colour to 3-bit colour code
IonSystems 2:168850019d5a 272 //set the FPGA colour inputs to the colour code
IonSystems 3:97668a4cd69d 273 startSort = true;
IonSystems 3:97668a4cd69d 274 break;
IonSystems 3:97668a4cd69d 275 case 't'://Test 180 servo
IonSystems 3:97668a4cd69d 276 sendCharacter('T');
IonSystems 3:97668a4cd69d 277 printLCD("Servo signal on");
IonSystems 3:97668a4cd69d 278 servoTest = true;
IonSystems 2:168850019d5a 279 break;
IonSystems 3:97668a4cd69d 280 case 'w'://Test 180 servo
IonSystems 3:97668a4cd69d 281 sendCharacter('W');
IonSystems 3:97668a4cd69d 282 printLCD("Servo signal off");
IonSystems 3:97668a4cd69d 283 servoTest = false;
IonSystems 3:97668a4cd69d 284 break;
IonSystems 3:97668a4cd69d 285
IonSystems 3:97668a4cd69d 286
IonSystems 3:97668a4cd69d 287 /* else if (operationMode){
IonSystems 0:8d54ffcf256e 288 switch(c){
IonSystems 1:a8a01df48d1a 289 case 'n':// Start sorting
IonSystems 1:a8a01df48d1a 290 sendCharacter('N'); //Confirm to PC that we are processing the instruction.
IonSystems 1:a8a01df48d1a 291 startSort = true; //Pulse the startSort control line to FPGA
IonSystems 1:a8a01df48d1a 292 wait(0.1);
IonSystems 1:a8a01df48d1a 293 startSort = false;
IonSystems 1:a8a01df48d1a 294 break;
IonSystems 1:a8a01df48d1a 295 case 'o': //Start dispensing
IonSystems 1:a8a01df48d1a 296 sendCharacter('O');
IonSystems 1:a8a01df48d1a 297 startDispense = true; //Pulse the startDispense control line to FPGA
IonSystems 1:a8a01df48d1a 298 wait(0.1);
IonSystems 1:a8a01df48d1a 299 startDispense = false;
IonSystems 0:8d54ffcf256e 300 break;
IonSystems 2:168850019d5a 301 case 'p': //
IonSystems 2:168850019d5a 302 sendCharacter('P');
IonSystems 2:168850019d5a 303 break;
IonSystems 0:8d54ffcf256e 304 }
IonSystems 3:97668a4cd69d 305 */ }
IonSystems 0:8d54ffcf256e 306 }
IonSystems 0:8d54ffcf256e 307
IonSystems 3:97668a4cd69d 308 void readColourSensor(){
IonSystems 0:8d54ffcf256e 309 rgb_sensor.getAllColors(rgb_readings);
IonSystems 0:8d54ffcf256e 310
IonSystems 0:8d54ffcf256e 311 double redd = (rgb_readings[1] /gMax) * 255;
IonSystems 0:8d54ffcf256e 312 double greend = (rgb_readings[2] /bMax) * 255;
IonSystems 0:8d54ffcf256e 313 double blued = (rgb_readings[0] /rMax) * 255;
IonSystems 0:8d54ffcf256e 314
IonSystems 0:8d54ffcf256e 315 int red = redd;
IonSystems 0:8d54ffcf256e 316 int green = greend;
IonSystems 0:8d54ffcf256e 317 int blue = blued;
IonSystems 3:97668a4cd69d 318 colourDataAcquired = true;
IonSystems 0:8d54ffcf256e 319 lcd->cls(); // clear display
IonSystems 0:8d54ffcf256e 320 lcd->locate(0,0);
IonSystems 0:8d54ffcf256e 321 lcd->printf("R:%d G:%d B:%d",red,green,blue);
IonSystems 0:8d54ffcf256e 322
IonSystems 0:8d54ffcf256e 323 lcd->locate(1,0);
IonSystems 0:8d54ffcf256e 324 if(red > 55){
IonSystems 0:8d54ffcf256e 325 lcd->printf("RED");
IonSystems 0:8d54ffcf256e 326 }
IonSystems 0:8d54ffcf256e 327 if(green > 55){
IonSystems 0:8d54ffcf256e 328 lcd->printf("GREEN");
IonSystems 0:8d54ffcf256e 329 }
IonSystems 0:8d54ffcf256e 330 if(red < 30 && green > 30 && blue > 30){
IonSystems 0:8d54ffcf256e 331 lcd->printf("BLUE");
IonSystems 0:8d54ffcf256e 332 }
IonSystems 0:8d54ffcf256e 333 }
IonSystems 3:97668a4cd69d 334
IonSystems 3:97668a4cd69d 335 void resetForNextCustomer(){
IonSystems 3:97668a4cd69d 336 colourDataAcquired = false;
IonSystems 3:97668a4cd69d 337 cardDataAcquired = false;
IonSystems 3:97668a4cd69d 338 printLCD("Ready for next customer");
IonSystems 3:97668a4cd69d 339 }
IonSystems 0:8d54ffcf256e 340
IonSystems 0:8d54ffcf256e 341 int main() {
IonSystems 0:8d54ffcf256e 342 //Setting up all the global variables
IonSystems 0:8d54ffcf256e 343 par_port = new MCP23017(p9, p10, 0x40);
IonSystems 0:8d54ffcf256e 344 par_port->config(0x0F00, 0x0F00, 0x0F00); // configure MCP23017 chip on WattBob
IonSystems 0:8d54ffcf256e 345 BACK_LIGHT_ON(par_port);
IonSystems 0:8d54ffcf256e 346 lcd = new WattBob_TextLCD(par_port);
IonSystems 0:8d54ffcf256e 347 rgb_sensor.enablePowerAndRGBC();
IonSystems 0:8d54ffcf256e 348 rgb_sensor.setIntegrationTime(100);
IonSystems 0:8d54ffcf256e 349 //Writing initial text on LCD
IonSystems 3:97668a4cd69d 350 // lcd->printf("Welcome to the");
IonSystems 3:97668a4cd69d 351 // lcd->locate(1,0); //Going to new line
IonSystems 3:97668a4cd69d 352 // lcd->printf("Chipin Sorter");
IonSystems 3:97668a4cd69d 353
IonSystems 3:97668a4cd69d 354
IonSystems 3:97668a4cd69d 355
IonSystems 3:97668a4cd69d 356 printLCD("Welcome to the Chipin Sorter");
IonSystems 3:97668a4cd69d 357 wait(1);
IonSystems 3:97668a4cd69d 358 lcd->reset(); //Clear LCD
IonSystems 0:8d54ffcf256e 359
IonSystems 0:8d54ffcf256e 360 while(1){
IonSystems 3:97668a4cd69d 361 if(par_port->read_bit(11)) resetForNextCustomer();
IonSystems 3:97668a4cd69d 362 // char c = pc.getc(); //wait for a serial character to be recieved.
IonSystems 3:97668a4cd69d 363 // processMessage(c); //Do something, based on the character recieved.
IonSystems 3:97668a4cd69d 364 if(cardDetect & !cardDataAcquired) cardAcquisition();
IonSystems 3:97668a4cd69d 365 setLEDs();
IonSystems 3:97668a4cd69d 366 readButtons();
IonSystems 3:97668a4cd69d 367
IonSystems 3:97668a4cd69d 368 if(chipDetected & !colourDataAcquired) readColourSensor();
IonSystems 0:8d54ffcf256e 369 }
IonSystems 0:8d54ffcf256e 370
IonSystems 0:8d54ffcf256e 371
IonSystems 3:97668a4cd69d 372
IonSystems 0:8d54ffcf256e 373 }