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:
Wed Nov 05 12:03:40 2014 +0000
Revision:
4:f3be545b3826
Parent:
3:97668a4cd69d
Child:
5:644bca33c1ca
Added read/write to file

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