![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
FPGAcomms.h@22:8f11d1c178ab, 2014-11-25 (annotated)
- Committer:
- IonSystems
- Date:
- Tue Nov 25 22:32:26 2014 +0000
- Revision:
- 22:8f11d1c178ab
- Parent:
- 20:6de191ac7ff3
- Child:
- 23:f9e7e64784be
almost working sort, working dispense
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
IonSystems | 6:e64796f1f384 | 1 | #include "mbed.h" |
IonSystems | 11:06f6e82b40a8 | 2 | //#include "Colour.h" |
IonSystems | 11:06f6e82b40a8 | 3 | #include "colourProcessing.h" |
IonSystems | 20:6de191ac7ff3 | 4 | int recycleAmount = 0; |
IonSystems | 6:e64796f1f384 | 5 | //Setup pins to FPGA |
IonSystems | 6:e64796f1f384 | 6 | DigitalOut startSort(p5); //A positive edge tells the FPGA to start sorting. |
IonSystems | 10:8c0696b99692 | 7 | DigitalIn sortComplete(p16); |
IonSystems | 6:e64796f1f384 | 8 | /* |
IonSystems | 6:e64796f1f384 | 9 | The command codes for the three sort select bits(ColourBit1 - ColourBit2) |
IonSystems | 17:6a0bb0ad5bb4 | 10 | See datasheet for furter information |
IonSystems | 6:e64796f1f384 | 11 | 000 Sort red chip |
IonSystems | 6:e64796f1f384 | 12 | 001 Sort green chip |
IonSystems | 6:e64796f1f384 | 13 | 010 Sort blue chip |
IonSystems | 6:e64796f1f384 | 14 | 011 Bin the chip |
IonSystems | 6:e64796f1f384 | 15 | 100 Recycle the chip |
IonSystems | 6:e64796f1f384 | 16 | 101 Nothing |
IonSystems | 6:e64796f1f384 | 17 | 110 Nothing |
IonSystems | 6:e64796f1f384 | 18 | 111 Nothing |
IonSystems | 6:e64796f1f384 | 19 | */ |
IonSystems | 6:e64796f1f384 | 20 | |
IonSystems | 6:e64796f1f384 | 21 | DigitalOut colourBit1(p6); //The 3 bits below are select bits for the sorter. |
IonSystems | 6:e64796f1f384 | 22 | DigitalOut colourBit2(p7); |
IonSystems | 6:e64796f1f384 | 23 | DigitalOut colourBit3(p8); |
IonSystems | 9:8d78eb55ad5e | 24 | DigitalOut startDispense(p18); //A positive edge tells the FPGA to start dispensing. p9 |
IonSystems | 6:e64796f1f384 | 25 | /* |
IonSystems | 6:e64796f1f384 | 26 | 00 Dispense red chip |
IonSystems | 6:e64796f1f384 | 27 | 01 Dispense green chip |
IonSystems | 6:e64796f1f384 | 28 | 10 Dispense blue chip |
IonSystems | 6:e64796f1f384 | 29 | 11 Nothing |
IonSystems | 6:e64796f1f384 | 30 | */ |
IonSystems | 6:e64796f1f384 | 31 | |
IonSystems | 9:8d78eb55ad5e | 32 | DigitalOut dispenseBit1(p19); //The 2 bits below are select bits for the dispenser.p10 |
IonSystems | 9:8d78eb55ad5e | 33 | DigitalOut dispenseBit2(p20); //p11 |
IonSystems | 8:b7771391adc9 | 34 | |
IonSystems | 12:814a8fdbb6f7 | 35 | DigitalIn dispenseComplete(p11); //FPGA sets to 1 once complete until another signal is recieved. |
IonSystems | 10:8c0696b99692 | 36 | DigitalOut select(p15); //0 for control, 1 for maintenance. |
IonSystems | 10:8c0696b99692 | 37 | |
IonSystems | 12:814a8fdbb6f7 | 38 | |
IonSystems | 19:78d4b78fa736 | 39 | void setMaintenanceSelect(bool bit6, bool bit5, bool bit4, bool bit3, bool bit2, bool bit1) |
IonSystems | 19:78d4b78fa736 | 40 | { |
IonSystems | 12:814a8fdbb6f7 | 41 | startSort = bit1; |
IonSystems | 12:814a8fdbb6f7 | 42 | colourBit1 = bit2; |
IonSystems | 12:814a8fdbb6f7 | 43 | colourBit2 = bit3; |
IonSystems | 12:814a8fdbb6f7 | 44 | colourBit3 = bit4; |
IonSystems | 12:814a8fdbb6f7 | 45 | startDispense = bit5; |
IonSystems | 12:814a8fdbb6f7 | 46 | dispenseBit1 = bit6; |
IonSystems | 19:78d4b78fa736 | 47 | } |
IonSystems | 19:78d4b78fa736 | 48 | |
IonSystems | 19:78d4b78fa736 | 49 | void setMaintenanceStart(bool value) |
IonSystems | 19:78d4b78fa736 | 50 | { |
IonSystems | 12:814a8fdbb6f7 | 51 | dispenseBit2 = value; |
IonSystems | 19:78d4b78fa736 | 52 | } |
IonSystems | 19:78d4b78fa736 | 53 | void waitForMaintenanceComplete() |
IonSystems | 19:78d4b78fa736 | 54 | { |
IonSystems | 19:78d4b78fa736 | 55 | while(!sortComplete) { |
IonSystems | 14:31ba3e56c788 | 56 | printLCD("Waiting for REC"); |
IonSystems | 12:814a8fdbb6f7 | 57 | wait(0.1); |
IonSystems | 19:78d4b78fa736 | 58 | } |
IonSystems | 19:78d4b78fa736 | 59 | } |
IonSystems | 19:78d4b78fa736 | 60 | |
IonSystems | 19:78d4b78fa736 | 61 | void setDispenseSelect(bool dBit1, bool dBit2) |
IonSystems | 19:78d4b78fa736 | 62 | { |
IonSystems | 19:78d4b78fa736 | 63 | dispenseBit1 = dBit1; |
IonSystems | 19:78d4b78fa736 | 64 | dispenseBit2 = dBit2; |
IonSystems | 12:814a8fdbb6f7 | 65 | } |
IonSystems | 19:78d4b78fa736 | 66 | |
IonSystems | 19:78d4b78fa736 | 67 | void setSortSelect(bool sBit1, bool sBit2, bool sBit3) |
IonSystems | 19:78d4b78fa736 | 68 | { |
IonSystems | 19:78d4b78fa736 | 69 | colourBit1 = sBit1; |
IonSystems | 19:78d4b78fa736 | 70 | colourBit2 = sBit2; |
IonSystems | 19:78d4b78fa736 | 71 | colourBit3 = sBit3; |
IonSystems | 19:78d4b78fa736 | 72 | } |
IonSystems | 19:78d4b78fa736 | 73 | |
IonSystems | 19:78d4b78fa736 | 74 | void maintain(StateMachine maintainState) |
IonSystems | 19:78d4b78fa736 | 75 | { |
IonSystems | 12:814a8fdbb6f7 | 76 | select = 1; //setting FPGA to maintenance mode. |
IonSystems | 19:78d4b78fa736 | 77 | setMaintenanceSelect(true,true,true,true,true,true); |
IonSystems | 19:78d4b78fa736 | 78 | switch(maintainState) { |
IonSystems | 12:814a8fdbb6f7 | 79 | case RB_LEFT: |
IonSystems | 12:814a8fdbb6f7 | 80 | setMaintenanceSelect(false,false,false,true,false,true); |
IonSystems | 19:78d4b78fa736 | 81 | break; |
IonSystems | 12:814a8fdbb6f7 | 82 | case RB_CENTRE: |
IonSystems | 12:814a8fdbb6f7 | 83 | setMaintenanceSelect(false,false,false,true,true,false); |
IonSystems | 19:78d4b78fa736 | 84 | break; |
IonSystems | 12:814a8fdbb6f7 | 85 | case RB_RIGHT: |
IonSystems | 12:814a8fdbb6f7 | 86 | setMaintenanceSelect(false,false,false,true,true,true); |
IonSystems | 19:78d4b78fa736 | 87 | break; |
IonSystems | 12:814a8fdbb6f7 | 88 | case GO_UP: |
IonSystems | 12:814a8fdbb6f7 | 89 | setMaintenanceSelect(false,false,true,false,false,false); |
IonSystems | 19:78d4b78fa736 | 90 | break; |
IonSystems | 12:814a8fdbb6f7 | 91 | case GO_CENTRE: |
IonSystems | 12:814a8fdbb6f7 | 92 | setMaintenanceSelect(false,false,true,false,false,true); |
IonSystems | 19:78d4b78fa736 | 93 | break; |
IonSystems | 12:814a8fdbb6f7 | 94 | case GO_DOWN: |
IonSystems | 12:814a8fdbb6f7 | 95 | setMaintenanceSelect(false,false,true,false,true,false); |
IonSystems | 19:78d4b78fa736 | 96 | break; |
IonSystems | 12:814a8fdbb6f7 | 97 | case BR_LEFT: |
IonSystems | 12:814a8fdbb6f7 | 98 | setMaintenanceSelect(false,false,true,false,true,true); |
IonSystems | 19:78d4b78fa736 | 99 | break; |
IonSystems | 12:814a8fdbb6f7 | 100 | case BR_RIGHT: |
IonSystems | 12:814a8fdbb6f7 | 101 | setMaintenanceSelect(false,false,true,true,false,false); |
IonSystems | 19:78d4b78fa736 | 102 | break; |
IonSystems | 12:814a8fdbb6f7 | 103 | case R_PUSH: |
IonSystems | 12:814a8fdbb6f7 | 104 | setMaintenanceSelect(false,false,true,true,false,true); |
IonSystems | 19:78d4b78fa736 | 105 | break; |
IonSystems | 22:8f11d1c178ab | 106 | case R_HOME: |
IonSystems | 22:8f11d1c178ab | 107 | setMaintenanceSelect(false,false,true,true,true,false); |
IonSystems | 22:8f11d1c178ab | 108 | break; |
IonSystems | 22:8f11d1c178ab | 109 | case GB_LEFT: |
IonSystems | 22:8f11d1c178ab | 110 | setMaintenanceSelect(false,false,true,true,true,true); |
IonSystems | 22:8f11d1c178ab | 111 | break; |
IonSystems | 22:8f11d1c178ab | 112 | case GB_CENTRE: |
IonSystems | 22:8f11d1c178ab | 113 | setMaintenanceSelect(false,true,false,false,false,false); |
IonSystems | 22:8f11d1c178ab | 114 | break; |
IonSystems | 22:8f11d1c178ab | 115 | case GB_RIGHT: |
IonSystems | 22:8f11d1c178ab | 116 | setMaintenanceSelect(false,true,false,false,false,true); |
IonSystems | 22:8f11d1c178ab | 117 | break; |
IonSystems | 22:8f11d1c178ab | 118 | case maintenanceEND: |
IonSystems | 22:8f11d1c178ab | 119 | setMaintenanceSelect(false,true,false,false,true,false); |
IonSystems | 22:8f11d1c178ab | 120 | break; |
IonSystems | 14:31ba3e56c788 | 121 | default: |
IonSystems | 14:31ba3e56c788 | 122 | log("Incorrect maintenance command recieved"); |
IonSystems | 14:31ba3e56c788 | 123 | printLCD("Incorrect maintenance command recieved"); |
IonSystems | 19:78d4b78fa736 | 124 | break; |
IonSystems | 19:78d4b78fa736 | 125 | } |
IonSystems | 12:814a8fdbb6f7 | 126 | setMaintenanceStart(true);//Bit 7 |
IonSystems | 12:814a8fdbb6f7 | 127 | waitForMaintenanceComplete(); |
IonSystems | 22:8f11d1c178ab | 128 | |
IonSystems | 12:814a8fdbb6f7 | 129 | setMaintenanceStart(false); |
IonSystems | 19:78d4b78fa736 | 130 | } |
IonSystems | 19:78d4b78fa736 | 131 | |
IonSystems | 19:78d4b78fa736 | 132 | void waitForDispenseComplete() |
IonSystems | 19:78d4b78fa736 | 133 | { |
IonSystems | 19:78d4b78fa736 | 134 | //wait(1); //temporary delay |
IonSystems | 22:8f11d1c178ab | 135 | while(!dispenseComplete) { |
IonSystems | 19:78d4b78fa736 | 136 | log("Waiting for dispense complete"); |
IonSystems | 19:78d4b78fa736 | 137 | printLCD("Waiting for dispense complete"); |
IonSystems | 12:814a8fdbb6f7 | 138 | } |
IonSystems | 22:8f11d1c178ab | 139 | //wait(0.6); |
IonSystems | 19:78d4b78fa736 | 140 | log("Complete"); |
IonSystems | 19:78d4b78fa736 | 141 | } |
IonSystems | 19:78d4b78fa736 | 142 | |
IonSystems | 19:78d4b78fa736 | 143 | void dispense(Colour colour) |
IonSystems | 19:78d4b78fa736 | 144 | { |
IonSystems | 12:814a8fdbb6f7 | 145 | |
IonSystems | 19:78d4b78fa736 | 146 | bool validDispense = false; |
IonSystems | 19:78d4b78fa736 | 147 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 19:78d4b78fa736 | 148 | setDispenseSelect(true,true); |
IonSystems | 19:78d4b78fa736 | 149 | /* A dispense operation can only take place if there are more than zero chips |
IonSystems | 19:78d4b78fa736 | 150 | */ |
IonSystems | 19:78d4b78fa736 | 151 | switch(colour) { |
IonSystems | 19:78d4b78fa736 | 152 | case RED: |
IonSystems | 19:78d4b78fa736 | 153 | if(redAmount > 0 || operationMode == false) { |
IonSystems | 19:78d4b78fa736 | 154 | log("RED"); |
IonSystems | 19:78d4b78fa736 | 155 | log("Setting dispense bits to 00"); |
IonSystems | 19:78d4b78fa736 | 156 | setDispenseSelect(false,false); |
IonSystems | 19:78d4b78fa736 | 157 | redAmount--; |
IonSystems | 19:78d4b78fa736 | 158 | validDispense = true; |
IonSystems | 19:78d4b78fa736 | 159 | } |
IonSystems | 10:8c0696b99692 | 160 | break; |
IonSystems | 19:78d4b78fa736 | 161 | case GREEN: |
IonSystems | 19:78d4b78fa736 | 162 | if(greenAmount > 0 || operationMode == false) { |
IonSystems | 19:78d4b78fa736 | 163 | log("GREEN"); |
IonSystems | 19:78d4b78fa736 | 164 | log("Setting dispense bits to 10"); |
IonSystems | 19:78d4b78fa736 | 165 | setDispenseSelect(true,false); |
IonSystems | 19:78d4b78fa736 | 166 | greenAmount--; |
IonSystems | 19:78d4b78fa736 | 167 | validDispense = true; |
IonSystems | 19:78d4b78fa736 | 168 | } |
IonSystems | 10:8c0696b99692 | 169 | break; |
IonSystems | 19:78d4b78fa736 | 170 | case BLUE: |
IonSystems | 19:78d4b78fa736 | 171 | if(blueAmount > 0 || operationMode == false) { |
IonSystems | 10:8c0696b99692 | 172 | log("BLUE"); |
IonSystems | 10:8c0696b99692 | 173 | log("Setting dispense bits to 01"); |
IonSystems | 19:78d4b78fa736 | 174 | setDispenseSelect(false,true); |
IonSystems | 12:814a8fdbb6f7 | 175 | blueAmount--; |
IonSystems | 12:814a8fdbb6f7 | 176 | validDispense = true; |
IonSystems | 19:78d4b78fa736 | 177 | } |
IonSystems | 10:8c0696b99692 | 178 | break; |
IonSystems | 19:78d4b78fa736 | 179 | |
IonSystems | 19:78d4b78fa736 | 180 | } |
IonSystems | 19:78d4b78fa736 | 181 | writeFile(redAmount,greenAmount,blueAmount); |
IonSystems | 19:78d4b78fa736 | 182 | |
IonSystems | 19:78d4b78fa736 | 183 | if(validDispense || operationMode == false) { |
IonSystems | 10:8c0696b99692 | 184 | log("Setting start dispense to true"); |
IonSystems | 10:8c0696b99692 | 185 | startDispense = true; //set the startDispense line high. |
IonSystems | 19:78d4b78fa736 | 186 | |
IonSystems | 19:78d4b78fa736 | 187 | waitForDispenseComplete(); |
IonSystems | 22:8f11d1c178ab | 188 | |
IonSystems | 10:8c0696b99692 | 189 | log("Setting start dispense to false"); |
IonSystems | 10:8c0696b99692 | 190 | startDispense = false; |
IonSystems | 22:8f11d1c178ab | 191 | |
IonSystems | 19:78d4b78fa736 | 192 | log("Resetting dispense bits to 11"); |
IonSystems | 19:78d4b78fa736 | 193 | setDispenseSelect(true,true); |
IonSystems | 19:78d4b78fa736 | 194 | |
IonSystems | 19:78d4b78fa736 | 195 | |
IonSystems | 19:78d4b78fa736 | 196 | |
IonSystems | 10:8c0696b99692 | 197 | printLCD("DISPENSE COMPLETE"); |
IonSystems | 19:78d4b78fa736 | 198 | |
IonSystems | 10:8c0696b99692 | 199 | } |
IonSystems | 19:78d4b78fa736 | 200 | } |
IonSystems | 19:78d4b78fa736 | 201 | void dispenseOrder(int r, int g, int b) |
IonSystems | 19:78d4b78fa736 | 202 | { |
IonSystems | 19:78d4b78fa736 | 203 | for(int i = r; r >= 0; i--) { |
IonSystems | 14:31ba3e56c788 | 204 | dispense(RED); |
IonSystems | 19:78d4b78fa736 | 205 | } |
IonSystems | 19:78d4b78fa736 | 206 | for(int i = g; i >= 0; i--) { |
IonSystems | 15:0c5f20e15b6a | 207 | dispense(GREEN); |
IonSystems | 19:78d4b78fa736 | 208 | } |
IonSystems | 19:78d4b78fa736 | 209 | for(int i = g; i >= 0; i--) { |
IonSystems | 15:0c5f20e15b6a | 210 | dispense(BLUE); |
IonSystems | 17:6a0bb0ad5bb4 | 211 | } |
IonSystems | 19:78d4b78fa736 | 212 | } |
IonSystems | 19:78d4b78fa736 | 213 | |
IonSystems | 20:6de191ac7ff3 | 214 | void waitForSortComplete() |
IonSystems | 20:6de191ac7ff3 | 215 | { |
IonSystems | 20:6de191ac7ff3 | 216 | while(!sortComplete) { |
IonSystems | 22:8f11d1c178ab | 217 | |
IonSystems | 20:6de191ac7ff3 | 218 | log("Waiting for sort complete"); |
IonSystems | 20:6de191ac7ff3 | 219 | printLCD("Waiting for sort complete"); |
IonSystems | 20:6de191ac7ff3 | 220 | } |
IonSystems | 20:6de191ac7ff3 | 221 | log("sortComplete signal recieved from FPGA"); |
IonSystems | 22:8f11d1c178ab | 222 | |
IonSystems | 20:6de191ac7ff3 | 223 | |
IonSystems | 20:6de191ac7ff3 | 224 | } |
IonSystems | 20:6de191ac7ff3 | 225 | |
IonSystems | 20:6de191ac7ff3 | 226 | void lift() |
IonSystems | 20:6de191ac7ff3 | 227 | { |
IonSystems | 20:6de191ac7ff3 | 228 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 20:6de191ac7ff3 | 229 | setSortSelect(true,true,true); |
IonSystems | 20:6de191ac7ff3 | 230 | setSortSelect(true,false,true); |
IonSystems | 20:6de191ac7ff3 | 231 | log("Setting start sort to true, FPGA should begin sorting now"); |
IonSystems | 20:6de191ac7ff3 | 232 | startSort = true; //set the startDispense line high. |
IonSystems | 20:6de191ac7ff3 | 233 | |
IonSystems | 20:6de191ac7ff3 | 234 | |
IonSystems | 20:6de191ac7ff3 | 235 | //wait(1); //temporary delay |
IonSystems | 20:6de191ac7ff3 | 236 | waitForSortComplete(); |
IonSystems | 22:8f11d1c178ab | 237 | |
IonSystems | 20:6de191ac7ff3 | 238 | log("Setting start sort to false"); |
IonSystems | 20:6de191ac7ff3 | 239 | startSort = false; |
IonSystems | 20:6de191ac7ff3 | 240 | |
IonSystems | 20:6de191ac7ff3 | 241 | log("Resetting sort bits to 111"); |
IonSystems | 20:6de191ac7ff3 | 242 | setSortSelect(true,true,true); |
IonSystems | 20:6de191ac7ff3 | 243 | printLCD("LIFT COMPLETE"); |
IonSystems | 20:6de191ac7ff3 | 244 | recycleAmount = 0; |
IonSystems | 20:6de191ac7ff3 | 245 | } |
IonSystems | 20:6de191ac7ff3 | 246 | |
IonSystems | 20:6de191ac7ff3 | 247 | void checkRecycleTube(){ |
IonSystems | 22:8f11d1c178ab | 248 | if(recycleAmount >= 5){ |
IonSystems | 22:8f11d1c178ab | 249 | //lift(); |
IonSystems | 22:8f11d1c178ab | 250 | recycleAmount = 0; |
IonSystems | 20:6de191ac7ff3 | 251 | } |
IonSystems | 20:6de191ac7ff3 | 252 | } |
IonSystems | 20:6de191ac7ff3 | 253 | |
IonSystems | 19:78d4b78fa736 | 254 | void recycle() |
IonSystems | 19:78d4b78fa736 | 255 | { |
IonSystems | 19:78d4b78fa736 | 256 | |
IonSystems | 19:78d4b78fa736 | 257 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 19:78d4b78fa736 | 258 | setSortSelect(true,true,true); |
IonSystems | 19:78d4b78fa736 | 259 | |
IonSystems | 22:8f11d1c178ab | 260 | log("Sort BLUE"); |
IonSystems | 19:78d4b78fa736 | 261 | log("Setting sort bits to 100"); |
IonSystems | 19:78d4b78fa736 | 262 | setSortSelect(false,false,true); |
IonSystems | 22:8f11d1c178ab | 263 | recycleAmount++; |
IonSystems | 22:8f11d1c178ab | 264 | |
IonSystems | 19:78d4b78fa736 | 265 | |
IonSystems | 19:78d4b78fa736 | 266 | log("Setting start sort to true"); |
IonSystems | 19:78d4b78fa736 | 267 | startSort = true; //set the startDispense line high. |
IonSystems | 19:78d4b78fa736 | 268 | |
IonSystems | 22:8f11d1c178ab | 269 | |
IonSystems | 20:6de191ac7ff3 | 270 | waitForSortComplete(); |
IonSystems | 22:8f11d1c178ab | 271 | |
IonSystems | 19:78d4b78fa736 | 272 | log("Setting start sort to false"); |
IonSystems | 19:78d4b78fa736 | 273 | startSort = false; |
IonSystems | 19:78d4b78fa736 | 274 | |
IonSystems | 19:78d4b78fa736 | 275 | log("Resetting dispense bits to 11"); |
IonSystems | 19:78d4b78fa736 | 276 | setSortSelect(true,true,true); |
IonSystems | 22:8f11d1c178ab | 277 | printLCD("SORT COMPLETE"); |
IonSystems | 22:8f11d1c178ab | 278 | |
IonSystems | 20:6de191ac7ff3 | 279 | checkRecycleTube(); |
IonSystems | 19:78d4b78fa736 | 280 | } |
IonSystems | 19:78d4b78fa736 | 281 | |
IonSystems | 19:78d4b78fa736 | 282 | |
IonSystems | 19:78d4b78fa736 | 283 | |
IonSystems | 19:78d4b78fa736 | 284 | void sort(Colour colour) |
IonSystems | 19:78d4b78fa736 | 285 | { |
IonSystems | 19:78d4b78fa736 | 286 | if(colour == NONE) { |
IonSystems | 19:78d4b78fa736 | 287 | return; |
IonSystems | 19:78d4b78fa736 | 288 | } else { |
IonSystems | 19:78d4b78fa736 | 289 | |
IonSystems | 10:8c0696b99692 | 290 | select = 0; //Setting to operation mode just in case it has not been set. |
IonSystems | 19:78d4b78fa736 | 291 | setSortSelect(true,true,true); |
IonSystems | 19:78d4b78fa736 | 292 | switch(colour) { |
IonSystems | 10:8c0696b99692 | 293 | case RED: |
IonSystems | 19:78d4b78fa736 | 294 | if(redAmount >= tubeSize) { |
IonSystems | 17:6a0bb0ad5bb4 | 295 | recycle(); |
IonSystems | 17:6a0bb0ad5bb4 | 296 | return; |
IonSystems | 19:78d4b78fa736 | 297 | } |
IonSystems | 10:8c0696b99692 | 298 | log("Sort RED"); |
IonSystems | 10:8c0696b99692 | 299 | log("Setting sort bits to 000"); |
IonSystems | 19:78d4b78fa736 | 300 | setSortSelect(false,false,false); |
IonSystems | 12:814a8fdbb6f7 | 301 | redAmount++; |
IonSystems | 19:78d4b78fa736 | 302 | break; |
IonSystems | 10:8c0696b99692 | 303 | case GREEN: |
IonSystems | 19:78d4b78fa736 | 304 | if(greenAmount >= tubeSize) { |
IonSystems | 17:6a0bb0ad5bb4 | 305 | recycle(); |
IonSystems | 17:6a0bb0ad5bb4 | 306 | return; |
IonSystems | 19:78d4b78fa736 | 307 | } |
IonSystems | 10:8c0696b99692 | 308 | log("Sort GREEN"); |
IonSystems | 10:8c0696b99692 | 309 | log("Setting sort bits to 001"); |
IonSystems | 19:78d4b78fa736 | 310 | setSortSelect(true,false,false); |
IonSystems | 12:814a8fdbb6f7 | 311 | greenAmount++; |
IonSystems | 19:78d4b78fa736 | 312 | break; |
IonSystems | 10:8c0696b99692 | 313 | case BLUE: |
IonSystems | 19:78d4b78fa736 | 314 | if(blueAmount >= tubeSize) { |
IonSystems | 17:6a0bb0ad5bb4 | 315 | recycle(); |
IonSystems | 17:6a0bb0ad5bb4 | 316 | return; |
IonSystems | 19:78d4b78fa736 | 317 | } |
IonSystems | 10:8c0696b99692 | 318 | log("Sort BLUE"); |
IonSystems | 10:8c0696b99692 | 319 | log("Setting sort bits to 010"); |
IonSystems | 19:78d4b78fa736 | 320 | setSortSelect(false,true,false); |
IonSystems | 12:814a8fdbb6f7 | 321 | blueAmount++; |
IonSystems | 19:78d4b78fa736 | 322 | break; |
IonSystems | 11:06f6e82b40a8 | 323 | case BIN: |
IonSystems | 11:06f6e82b40a8 | 324 | log("Sort BIN"); |
IonSystems | 11:06f6e82b40a8 | 325 | log("Setting sort bits to 011"); |
IonSystems | 19:78d4b78fa736 | 326 | setSortSelect(true,true,false); |
IonSystems | 19:78d4b78fa736 | 327 | break; |
IonSystems | 10:8c0696b99692 | 328 | } |
IonSystems | 19:78d4b78fa736 | 329 | |
IonSystems | 19:78d4b78fa736 | 330 | |
IonSystems | 10:8c0696b99692 | 331 | log("Setting start sort to true"); |
IonSystems | 10:8c0696b99692 | 332 | startSort = true; //set the startDispense line high. |
IonSystems | 19:78d4b78fa736 | 333 | |
IonSystems | 19:78d4b78fa736 | 334 | |
IonSystems | 19:78d4b78fa736 | 335 | waitForSortComplete(); |
IonSystems | 22:8f11d1c178ab | 336 | |
IonSystems | 10:8c0696b99692 | 337 | log("Setting start sort to false"); |
IonSystems | 19:78d4b78fa736 | 338 | startSort = false; |
IonSystems | 19:78d4b78fa736 | 339 | |
IonSystems | 19:78d4b78fa736 | 340 | log("Resetting dispense bits to 11"); |
IonSystems | 19:78d4b78fa736 | 341 | setSortSelect(true,true,true); |
IonSystems | 10:8c0696b99692 | 342 | printLCD("SORT COMPLETE"); |
IonSystems | 12:814a8fdbb6f7 | 343 | writeFile(redAmount,greenAmount,blueAmount); |
IonSystems | 19:78d4b78fa736 | 344 | } |
IonSystems | 12:814a8fdbb6f7 | 345 | } |
IonSystems | 12:814a8fdbb6f7 | 346 | |
IonSystems | 19:78d4b78fa736 | 347 | |
IonSystems | 19:78d4b78fa736 | 348 | |
IonSystems | 19:78d4b78fa736 | 349 | void dispenseAll() |
IonSystems | 19:78d4b78fa736 | 350 | { |
IonSystems | 12:814a8fdbb6f7 | 351 | int red = redAmount; |
IonSystems | 12:814a8fdbb6f7 | 352 | int green = greenAmount; |
IonSystems | 12:814a8fdbb6f7 | 353 | int blue = blueAmount; |
IonSystems | 19:78d4b78fa736 | 354 | for(int r = 0; r < red; r++) { |
IonSystems | 12:814a8fdbb6f7 | 355 | dispense(RED); |
IonSystems | 22:8f11d1c178ab | 356 | |
IonSystems | 19:78d4b78fa736 | 357 | } |
IonSystems | 19:78d4b78fa736 | 358 | redAmount = 0; |
IonSystems | 19:78d4b78fa736 | 359 | |
IonSystems | 19:78d4b78fa736 | 360 | for(int g = 0; g < green; g++) { |
IonSystems | 12:814a8fdbb6f7 | 361 | dispense(GREEN); |
IonSystems | 22:8f11d1c178ab | 362 | |
IonSystems | 19:78d4b78fa736 | 363 | } |
IonSystems | 19:78d4b78fa736 | 364 | greenAmount = 0; |
IonSystems | 19:78d4b78fa736 | 365 | |
IonSystems | 19:78d4b78fa736 | 366 | for(int b = 0; b < blue; b++) { |
IonSystems | 12:814a8fdbb6f7 | 367 | dispense(BLUE); |
IonSystems | 22:8f11d1c178ab | 368 | |
IonSystems | 19:78d4b78fa736 | 369 | } |
IonSystems | 19:78d4b78fa736 | 370 | blueAmount = 0; |
IonSystems | 12:814a8fdbb6f7 | 371 | writeFile(redAmount,greenAmount,blueAmount); |
IonSystems | 19:78d4b78fa736 | 372 | } |