Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed-rtos mbed
main.cpp@17:6a0bb0ad5bb4, 2014-11-24 (annotated)
- Committer:
- IonSystems
- Date:
- Mon Nov 24 14:00:46 2014 +0000
- Revision:
- 17:6a0bb0ad5bb4
- Parent:
- 16:498359d078dc
- Child:
- 18:12e2c82a5a6c
fixed some bugs, great comment i know
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| IonSystems | 12:814a8fdbb6f7 | 1 | bool operationMode = true; //the MBED starts in operation mode. |
| IonSystems | 0:8d54ffcf256e | 2 | #include "mbed.h" |
| IonSystems | 15:0c5f20e15b6a | 3 | #include "Language.h" |
| IonSystems | 15:0c5f20e15b6a | 4 | DigitalIn languageButton(p30); |
| IonSystems | 15:0c5f20e15b6a | 5 | Language currentLanguage = ENGLISH; |
| IonSystems | 6:e64796f1f384 | 6 | #include <sstream> |
| IonSystems | 12:814a8fdbb6f7 | 7 | #include <string> |
| IonSystems | 15:0c5f20e15b6a | 8 | |
| IonSystems | 17:6a0bb0ad5bb4 | 9 | |
| IonSystems | 6:e64796f1f384 | 10 | #include "mbedStorage.h" |
| IonSystems | 6:e64796f1f384 | 11 | #include "rgbLED.h" |
| IonSystems | 6:e64796f1f384 | 12 | #include "mbedLCD.h" |
| IonSystems | 7:6ba00694f9cd | 13 | #include "stateMachine.h" |
| IonSystems | 8:b7771391adc9 | 14 | #include "FPGAcomms.h" |
| IonSystems | 10:8c0696b99692 | 15 | #ifndef serialComm |
| IonSystems | 10:8c0696b99692 | 16 | #define serialComm |
| IonSystems | 15:0c5f20e15b6a | 17 | |
| IonSystems | 10:8c0696b99692 | 18 | #include "serialCommunication.h" |
| IonSystems | 10:8c0696b99692 | 19 | #endif |
| IonSystems | 10:8c0696b99692 | 20 | #include "testFunctions.h" |
| IonSystems | 5:644bca33c1ca | 21 | |
| IonSystems | 0:8d54ffcf256e | 22 | |
| IonSystems | 11:06f6e82b40a8 | 23 | |
| IonSystems | 11:06f6e82b40a8 | 24 | |
| IonSystems | 7:6ba00694f9cd | 25 | StateMachine stateMachine; |
| IonSystems | 7:6ba00694f9cd | 26 | |
| IonSystems | 6:e64796f1f384 | 27 | //Boolean values to easily enable and disable certain features for individual testing |
| IonSystems | 6:e64796f1f384 | 28 | bool colourSensor = true; |
| IonSystems | 6:e64796f1f384 | 29 | bool cardReader = true; |
| IonSystems | 6:e64796f1f384 | 30 | bool sorter = true; |
| IonSystems | 6:e64796f1f384 | 31 | bool dispensor = true; |
| IonSystems | 10:8c0696b99692 | 32 | bool testFunctions = true; |
| IonSystems | 10:8c0696b99692 | 33 | bool serialComms = true; |
| IonSystems | 6:e64796f1f384 | 34 | |
| IonSystems | 6:e64796f1f384 | 35 | bool colourDataAcquired = false; |
| IonSystems | 6:e64796f1f384 | 36 | bool chipDetected = false; |
| IonSystems | 12:814a8fdbb6f7 | 37 | |
| IonSystems | 3:97668a4cd69d | 38 | |
| IonSystems | 17:6a0bb0ad5bb4 | 39 | int mode = 3; |
| IonSystems | 13:0661d658d9d1 | 40 | int maintenanceModePointer = 0; |
| IonSystems | 14:31ba3e56c788 | 41 | Colour lastColour = NONE; |
| IonSystems | 17:6a0bb0ad5bb4 | 42 | Colour colourStore = NONE; |
| IonSystems | 10:8c0696b99692 | 43 | |
| IonSystems | 10:8c0696b99692 | 44 | void checkSortDispenseButtons(){ |
| IonSystems | 11:06f6e82b40a8 | 45 | |
| IonSystems | 11:06f6e82b40a8 | 46 | if(par_port->read_bit(11)){ |
| IonSystems | 12:814a8fdbb6f7 | 47 | mode++; |
| IonSystems | 13:0661d658d9d1 | 48 | if(mode >= 7) mode = 0; |
| IonSystems | 11:06f6e82b40a8 | 49 | } |
| IonSystems | 11:06f6e82b40a8 | 50 | |
| IonSystems | 11:06f6e82b40a8 | 51 | |
| IonSystems | 13:0661d658d9d1 | 52 | if(mode == 0){ //Dispense Mode |
| IonSystems | 11:06f6e82b40a8 | 53 | printLCD("Dispense Mode"); |
| IonSystems | 10:8c0696b99692 | 54 | if(par_port->read_bit(8)){ |
| IonSystems | 10:8c0696b99692 | 55 | log("Red Button pressed"); |
| IonSystems | 10:8c0696b99692 | 56 | stateMachine = DISPENSE_RED; |
| IonSystems | 10:8c0696b99692 | 57 | processState(stateMachine); |
| IonSystems | 10:8c0696b99692 | 58 | }else if(par_port->read_bit(9)){ |
| IonSystems | 10:8c0696b99692 | 59 | log("Green Button pressed"); |
| IonSystems | 10:8c0696b99692 | 60 | stateMachine = DISPENSE_GREEN; |
| IonSystems | 10:8c0696b99692 | 61 | processState(stateMachine); |
| IonSystems | 10:8c0696b99692 | 62 | }else if(par_port->read_bit(10)){ |
| IonSystems | 10:8c0696b99692 | 63 | log("Blue Button pressed"); |
| IonSystems | 10:8c0696b99692 | 64 | stateMachine = DISPENSE_BLUE; |
| IonSystems | 10:8c0696b99692 | 65 | processState(stateMachine); |
| IonSystems | 10:8c0696b99692 | 66 | } |
| IonSystems | 13:0661d658d9d1 | 67 | }else if(mode == 1){ //Sort Mode |
| IonSystems | 11:06f6e82b40a8 | 68 | printLCD("Sort Mode"); |
| IonSystems | 10:8c0696b99692 | 69 | if(par_port->read_bit(8)){ |
| IonSystems | 10:8c0696b99692 | 70 | log("Red Button pressed"); |
| IonSystems | 10:8c0696b99692 | 71 | stateMachine = SORT_RED; |
| IonSystems | 10:8c0696b99692 | 72 | processState(stateMachine); |
| IonSystems | 10:8c0696b99692 | 73 | }else if(par_port->read_bit(9)){ |
| IonSystems | 13:0661d658d9d1 | 74 | log("Green Button pressed"); |
| IonSystems | 10:8c0696b99692 | 75 | stateMachine = SORT_GREEN; |
| IonSystems | 10:8c0696b99692 | 76 | processState(stateMachine); |
| IonSystems | 10:8c0696b99692 | 77 | }else if(par_port->read_bit(10)){ |
| IonSystems | 10:8c0696b99692 | 78 | log("Blue Button pressed"); |
| IonSystems | 10:8c0696b99692 | 79 | stateMachine = SORT_BLUE; |
| IonSystems | 10:8c0696b99692 | 80 | processState(stateMachine); |
| IonSystems | 10:8c0696b99692 | 81 | } |
| IonSystems | 11:06f6e82b40a8 | 82 | |
| IonSystems | 13:0661d658d9d1 | 83 | }else if(mode == 2){ //Bin/Recycle Mode |
| IonSystems | 11:06f6e82b40a8 | 84 | printLCD("Bin/Recycle Mode"); |
| IonSystems | 11:06f6e82b40a8 | 85 | if(par_port->read_bit(8)){ |
| IonSystems | 11:06f6e82b40a8 | 86 | log("Bin button pressed"); |
| IonSystems | 11:06f6e82b40a8 | 87 | stateMachine = SORT_BIN; |
| IonSystems | 11:06f6e82b40a8 | 88 | processState(stateMachine); |
| IonSystems | 11:06f6e82b40a8 | 89 | }else if(par_port->read_bit(9)){ |
| IonSystems | 11:06f6e82b40a8 | 90 | log("Recycle Button"); |
| IonSystems | 11:06f6e82b40a8 | 91 | stateMachine = SORT_RECYCLE; |
| IonSystems | 11:06f6e82b40a8 | 92 | processState(stateMachine); |
| IonSystems | 11:06f6e82b40a8 | 93 | } |
| IonSystems | 14:31ba3e56c788 | 94 | }else if(mode == 3){ |
| IonSystems | 14:31ba3e56c788 | 95 | printLCD("Colour Sensor Mode"); //Colour Sensing Mode |
| IonSystems | 11:06f6e82b40a8 | 96 | if(colourSensor){ |
| IonSystems | 17:6a0bb0ad5bb4 | 97 | |
| IonSystems | 17:6a0bb0ad5bb4 | 98 | |
| IonSystems | 17:6a0bb0ad5bb4 | 99 | Colour colour = readColourSensor(); |
| IonSystems | 17:6a0bb0ad5bb4 | 100 | lastColour = colourStore; |
| IonSystems | 17:6a0bb0ad5bb4 | 101 | colourStore = colour; |
| IonSystems | 15:0c5f20e15b6a | 102 | /* Tries to prevent a colour mis-read. When a chip is read as BIN or NONE, |
| IonSystems | 15:0c5f20e15b6a | 103 | the chip may be falling down the tube as the reading was taken. The chip may |
| IonSystems | 15:0c5f20e15b6a | 104 | also be out of position on the slider. Ignoring the next reading is trying to |
| IonSystems | 15:0c5f20e15b6a | 105 | prevent a mis-read being valid. |
| IonSystems | 15:0c5f20e15b6a | 106 | */ |
| IonSystems | 17:6a0bb0ad5bb4 | 107 | if(lastColour == NONE){ |
| IonSystems | 17:6a0bb0ad5bb4 | 108 | |
| IonSystems | 14:31ba3e56c788 | 109 | return; |
| IonSystems | 14:31ba3e56c788 | 110 | } |
| IonSystems | 17:6a0bb0ad5bb4 | 111 | |
| IonSystems | 14:31ba3e56c788 | 112 | if(sorter) sort(colour); |
| IonSystems | 14:31ba3e56c788 | 113 | //Put (sorter && colour != BIN) in if statement. |
| IonSystems | 11:06f6e82b40a8 | 114 | } |
| IonSystems | 14:31ba3e56c788 | 115 | //if |
| IonSystems | 13:0661d658d9d1 | 116 | }else if(mode == 4){ //Card Reader Mode |
| IonSystems | 12:814a8fdbb6f7 | 117 | if(cardReader && cardDetect){ |
| IonSystems | 12:814a8fdbb6f7 | 118 | cardAcquisition(); |
| IonSystems | 12:814a8fdbb6f7 | 119 | |
| IonSystems | 12:814a8fdbb6f7 | 120 | } |
| IonSystems | 13:0661d658d9d1 | 121 | }else if(mode == 5){ //Storage Mode |
| IonSystems | 12:814a8fdbb6f7 | 122 | stringstream ss; |
| IonSystems | 12:814a8fdbb6f7 | 123 | ss << "Stored Values " << "R:" << redAmount << "G:" << greenAmount << "B:" << blueAmount; |
| IonSystems | 12:814a8fdbb6f7 | 124 | string tmp = ss.str(); |
| IonSystems | 12:814a8fdbb6f7 | 125 | printLCD(tmp.c_str()); |
| IonSystems | 12:814a8fdbb6f7 | 126 | wait(0.1); |
| IonSystems | 12:814a8fdbb6f7 | 127 | if(par_port->read_bit(8)){ |
| IonSystems | 12:814a8fdbb6f7 | 128 | log("Dispense all button pressed"); |
| IonSystems | 12:814a8fdbb6f7 | 129 | stateMachine = DISPENSE_ALL; |
| IonSystems | 12:814a8fdbb6f7 | 130 | processState(stateMachine); |
| IonSystems | 12:814a8fdbb6f7 | 131 | }else if(par_port->read_bit(9)){ |
| IonSystems | 12:814a8fdbb6f7 | 132 | log("Clear stored numbers button pressed"); |
| IonSystems | 12:814a8fdbb6f7 | 133 | writeFile(0,0,0); |
| IonSystems | 12:814a8fdbb6f7 | 134 | redAmount = 0;greenAmount = 0;blueAmount = 0; |
| IonSystems | 12:814a8fdbb6f7 | 135 | printLCD("Cleared values"); |
| IonSystems | 12:814a8fdbb6f7 | 136 | |
| IonSystems | 12:814a8fdbb6f7 | 137 | }else if(par_port->read_bit(10)){ |
| IonSystems | 16:498359d078dc | 138 | log("Increment all stored values by 2."); |
| IonSystems | 16:498359d078dc | 139 | redAmount += 2;greenAmount += 2;blueAmount += 2; |
| IonSystems | 16:498359d078dc | 140 | writeFile(redAmount,greenAmount,blueAmount); |
| IonSystems | 16:498359d078dc | 141 | |
| IonSystems | 13:0661d658d9d1 | 142 | printLCD("set values to 2"); |
| IonSystems | 12:814a8fdbb6f7 | 143 | |
| IonSystems | 12:814a8fdbb6f7 | 144 | } |
| IonSystems | 17:6a0bb0ad5bb4 | 145 | }else if(mode == 6){ |
| IonSystems | 17:6a0bb0ad5bb4 | 146 | printLCD("Main. Mode: " + maintenanceModeSelect); |
| IonSystems | 17:6a0bb0ad5bb4 | 147 | wait(0.1); //Maintenance Mode |
| IonSystems | 17:6a0bb0ad5bb4 | 148 | //printLCD("Maintenance Mode"); |
| IonSystems | 13:0661d658d9d1 | 149 | if(par_port->read_bit(8)){ //Next maintenance function |
| IonSystems | 13:0661d658d9d1 | 150 | maintenanceModePointer++; |
| IonSystems | 13:0661d658d9d1 | 151 | if(maintenanceModePointer > 12) maintenanceModePointer = 0; |
| IonSystems | 13:0661d658d9d1 | 152 | }else if(par_port->read_bit(9)){ //Previous maintenance function |
| IonSystems | 13:0661d658d9d1 | 153 | maintenanceModePointer--; |
| IonSystems | 13:0661d658d9d1 | 154 | if(maintenanceModePointer < 0) maintenanceModePointer = 12; |
| IonSystems | 13:0661d658d9d1 | 155 | }else if(par_port->read_bit(10)){ //Process Instruction |
| IonSystems | 13:0661d658d9d1 | 156 | switch(maintenanceModePointer){ |
| IonSystems | 13:0661d658d9d1 | 157 | case 0://RB_LEFT: |
| IonSystems | 14:31ba3e56c788 | 158 | printLCD("Moving Red/Blue Slider left."); |
| IonSystems | 13:0661d658d9d1 | 159 | maintain(RB_LEFT); |
| IonSystems | 13:0661d658d9d1 | 160 | break; |
| IonSystems | 13:0661d658d9d1 | 161 | |
| IonSystems | 13:0661d658d9d1 | 162 | case 1://RB_CENTRE: |
| IonSystems | 14:31ba3e56c788 | 163 | printLCD("Centering Red/Blue Slider."); |
| IonSystems | 13:0661d658d9d1 | 164 | maintain(RB_CENTRE); |
| IonSystems | 13:0661d658d9d1 | 165 | break; |
| IonSystems | 13:0661d658d9d1 | 166 | |
| IonSystems | 13:0661d658d9d1 | 167 | case 2://RB_RIGHT: |
| IonSystems | 14:31ba3e56c788 | 168 | printLCD("Moving Red/Blue Slider right."); |
| IonSystems | 13:0661d658d9d1 | 169 | maintain(RB_RIGHT); |
| IonSystems | 13:0661d658d9d1 | 170 | break; |
| IonSystems | 13:0661d658d9d1 | 171 | |
| IonSystems | 13:0661d658d9d1 | 172 | case 3://GO_UP: |
| IonSystems | 14:31ba3e56c788 | 173 | printLCD("Moving Green Slider up."); |
| IonSystems | 13:0661d658d9d1 | 174 | maintain(GO_UP); |
| IonSystems | 13:0661d658d9d1 | 175 | break; |
| IonSystems | 13:0661d658d9d1 | 176 | |
| IonSystems | 13:0661d658d9d1 | 177 | case 4://GO_CENTRE: |
| IonSystems | 14:31ba3e56c788 | 178 | printLCD("Centering Green Slider."); |
| IonSystems | 13:0661d658d9d1 | 179 | maintain(GO_CENTRE); |
| IonSystems | 13:0661d658d9d1 | 180 | break; |
| IonSystems | 13:0661d658d9d1 | 181 | |
| IonSystems | 13:0661d658d9d1 | 182 | case 5://GO_DOWN: |
| IonSystems | 14:31ba3e56c788 | 183 | printLCD("Moving Green Slider down."); |
| IonSystems | 13:0661d658d9d1 | 184 | maintain(GO_DOWN); |
| IonSystems | 13:0661d658d9d1 | 185 | break; |
| IonSystems | 13:0661d658d9d1 | 186 | |
| IonSystems | 13:0661d658d9d1 | 187 | case 6://BR_LEFT: |
| IonSystems | 14:31ba3e56c788 | 188 | printLCD("Moving Bin Slider left."); |
| IonSystems | 13:0661d658d9d1 | 189 | maintain(BR_LEFT); |
| IonSystems | 13:0661d658d9d1 | 190 | break; |
| IonSystems | 13:0661d658d9d1 | 191 | |
| IonSystems | 13:0661d658d9d1 | 192 | case 7://BR_RIGHT: |
| IonSystems | 14:31ba3e56c788 | 193 | printLCD("Moving Bin Slider right."); |
| IonSystems | 13:0661d658d9d1 | 194 | maintain(BR_RIGHT); |
| IonSystems | 13:0661d658d9d1 | 195 | break; |
| IonSystems | 13:0661d658d9d1 | 196 | |
| IonSystems | 13:0661d658d9d1 | 197 | case 8://R_PUSH: |
| IonSystems | 14:31ba3e56c788 | 198 | printLCD("Pushing red dispensor"); |
| IonSystems | 13:0661d658d9d1 | 199 | maintain(R_PUSH); |
| IonSystems | 13:0661d658d9d1 | 200 | break; |
| IonSystems | 13:0661d658d9d1 | 201 | |
| IonSystems | 13:0661d658d9d1 | 202 | case 9://R_HOME: |
| IonSystems | 14:31ba3e56c788 | 203 | printLCD("Homing red dispensor"); |
| IonSystems | 14:31ba3e56c788 | 204 | maintain(R_HOME); |
| IonSystems | 13:0661d658d9d1 | 205 | break; |
| IonSystems | 13:0661d658d9d1 | 206 | |
| IonSystems | 13:0661d658d9d1 | 207 | case 10://GB_LEFT: |
| IonSystems | 14:31ba3e56c788 | 208 | printLCD("Pushing green dispensor"); |
| IonSystems | 13:0661d658d9d1 | 209 | maintain(GB_LEFT); |
| IonSystems | 13:0661d658d9d1 | 210 | break; |
| IonSystems | 13:0661d658d9d1 | 211 | |
| IonSystems | 13:0661d658d9d1 | 212 | case 11://GB_CENTRE: |
| IonSystems | 14:31ba3e56c788 | 213 | printLCD("Centre green/blue dispensor"); |
| IonSystems | 13:0661d658d9d1 | 214 | maintain(GB_CENTRE); |
| IonSystems | 13:0661d658d9d1 | 215 | break; |
| IonSystems | 13:0661d658d9d1 | 216 | |
| IonSystems | 13:0661d658d9d1 | 217 | case 12://GB_RIGHT: |
| IonSystems | 14:31ba3e56c788 | 218 | printLCD("Pushing blue dispensor"); |
| IonSystems | 14:31ba3e56c788 | 219 | maintain(GB_RIGHT ); |
| IonSystems | 13:0661d658d9d1 | 220 | break; |
| IonSystems | 13:0661d658d9d1 | 221 | |
| IonSystems | 13:0661d658d9d1 | 222 | |
| IonSystems | 13:0661d658d9d1 | 223 | |
| IonSystems | 14:31ba3e56c788 | 224 | } printLCD("Ready for next command"); |
| IonSystems | 14:31ba3e56c788 | 225 | wait(1); |
| IonSystems | 13:0661d658d9d1 | 226 | } |
| IonSystems | 12:814a8fdbb6f7 | 227 | } |
| IonSystems | 9:8d78eb55ad5e | 228 | } |
| IonSystems | 12:814a8fdbb6f7 | 229 | |
| IonSystems | 15:0c5f20e15b6a | 230 | void changeLanguage(){ |
| IonSystems | 17:6a0bb0ad5bb4 | 231 | currentLanguage = nextLanguage(currentLanguage); //Change the language on teh MBED. |
| IonSystems | 17:6a0bb0ad5bb4 | 232 | sendLanguageCharacter(); //Tell PC to change language |
| IonSystems | 17:6a0bb0ad5bb4 | 233 | ////Change Language //Tell Arduino to change language. |
| IonSystems | 15:0c5f20e15b6a | 234 | printLCD("setting language to english"); |
| IonSystems | 17:6a0bb0ad5bb4 | 235 | wait(1); |
| IonSystems | 15:0c5f20e15b6a | 236 | } |
| IonSystems | 6:e64796f1f384 | 237 | |
| IonSystems | 6:e64796f1f384 | 238 | int main() { |
| IonSystems | 12:814a8fdbb6f7 | 239 | |
| IonSystems | 7:6ba00694f9cd | 240 | stateMachine = INITIALISING; |
| IonSystems | 8:b7771391adc9 | 241 | processState(stateMachine); |
| IonSystems | 12:814a8fdbb6f7 | 242 | readChipNumbers(); |
| IonSystems | 12:814a8fdbb6f7 | 243 | printStoredChipValues(); |
| IonSystems | 14:31ba3e56c788 | 244 | //displayOperationMode(); |
| IonSystems | 8:b7771391adc9 | 245 | while(true){ |
| IonSystems | 10:8c0696b99692 | 246 | if(serialComms){ |
| IonSystems | 10:8c0696b99692 | 247 | checkSerial(); |
| IonSystems | 12:814a8fdbb6f7 | 248 | }if(testFunctions) checkSortDispenseButtons(); |
| IonSystems | 17:6a0bb0ad5bb4 | 249 | //if(languageButton) changeLanguage(); |
| IonSystems | 15:0c5f20e15b6a | 250 | |
| IonSystems | 15:0c5f20e15b6a | 251 | |
| IonSystems | 15:0c5f20e15b6a | 252 | |
| IonSystems | 15:0c5f20e15b6a | 253 | |
| IonSystems | 11:06f6e82b40a8 | 254 | wait(0.05); |
| IonSystems | 12:814a8fdbb6f7 | 255 | |
| IonSystems | 8:b7771391adc9 | 256 | } |
| IonSystems | 10:8c0696b99692 | 257 | /*char c; |
| IonSystems | 10:8c0696b99692 | 258 | if(c = readCharacter()){ |
| IonSystems | 10:8c0696b99692 | 259 | processMessage(c); |
| IonSystems | 10:8c0696b99692 | 260 | }*/ |
| IonSystems | 11:06f6e82b40a8 | 261 | |
| IonSystems | 11:06f6e82b40a8 | 262 | |
| IonSystems | 10:8c0696b99692 | 263 | } |
| IonSystems | 9:8d78eb55ad5e | 264 | |
| IonSystems | 9:8d78eb55ad5e | 265 | |
| IonSystems | 9:8d78eb55ad5e | 266 | |
| IonSystems | 17:6a0bb0ad5bb4 | 267 | |
| IonSystems | 8:b7771391adc9 | 268 | |
| IonSystems | 17:6a0bb0ad5bb4 | 269 | |
| IonSystems | 3:97668a4cd69d | 270 | |
| IonSystems | 10:8c0696b99692 | 271 | |
| IonSystems | 10:8c0696b99692 | 272 |
