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.
ColourSensor.cpp@0:23508e7103a4, 2019-03-18 (annotated)
- Committer:
- quickeman
- Date:
- Mon Mar 18 11:21:43 2019 +0000
- Revision:
- 0:23508e7103a4
Version 2 of OOP Colour Sensor program.; ; Altered so solenoid is ON by default and only turns OFF when dropping disk.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| quickeman | 0:23508e7103a4 | 1 | // ColourSensor.cpp | 
| quickeman | 0:23508e7103a4 | 2 | |
| quickeman | 0:23508e7103a4 | 3 | #include "ColourSensor_H.h" | 
| quickeman | 0:23508e7103a4 | 4 | #include "mbed.h" | 
| quickeman | 0:23508e7103a4 | 5 | |
| quickeman | 0:23508e7103a4 | 6 | ColourSensor::ColourSensor(PinName If, PinName Which, PinName Out): | 
| quickeman | 0:23508e7103a4 | 7 | inIf(If), inWhich(Which), solenoid(Out) { | 
| quickeman | 0:23508e7103a4 | 8 | // Class constructor for colour sensor & solenoid | 
| quickeman | 0:23508e7103a4 | 9 | // Sets initial conditions of operation | 
| quickeman | 0:23508e7103a4 | 10 | |
| quickeman | 0:23508e7103a4 | 11 | initialConditions(); | 
| quickeman | 0:23508e7103a4 | 12 | } | 
| quickeman | 0:23508e7103a4 | 13 | |
| quickeman | 0:23508e7103a4 | 14 | void ColourSensor::initialConditions() { | 
| quickeman | 0:23508e7103a4 | 15 | // set initial conditions & variables | 
| quickeman | 0:23508e7103a4 | 16 | //printf("Setting Initial Conditions\n\r"); | 
| quickeman | 0:23508e7103a4 | 17 | detectedIf = 0; | 
| quickeman | 0:23508e7103a4 | 18 | detectedIfOld = 0; | 
| quickeman | 0:23508e7103a4 | 19 | newDetection = 0; | 
| quickeman | 0:23508e7103a4 | 20 | diskHave = 0; | 
| quickeman | 0:23508e7103a4 | 21 | |
| quickeman | 0:23508e7103a4 | 22 | solenoidState = 0; | 
| quickeman | 0:23508e7103a4 | 23 | |
| quickeman | 0:23508e7103a4 | 24 | toggleA = 0; | 
| quickeman | 0:23508e7103a4 | 25 | toggleB = 0; | 
| quickeman | 0:23508e7103a4 | 26 | toggleConst = 1; | 
| quickeman | 0:23508e7103a4 | 27 | |
| quickeman | 0:23508e7103a4 | 28 | flagColour = 0; | 
| quickeman | 0:23508e7103a4 | 29 | |
| quickeman | 0:23508e7103a4 | 30 | solenoidSet(1); | 
| quickeman | 0:23508e7103a4 | 31 | } | 
| quickeman | 0:23508e7103a4 | 32 | |
| quickeman | 0:23508e7103a4 | 33 | void ColourSensor::solenoidOn() { | 
| quickeman | 0:23508e7103a4 | 34 | // Interrupt (software): Turn ON solenoid | 
| quickeman | 0:23508e7103a4 | 35 | //printf("Turning solenoid back ON\n\r"); | 
| quickeman | 0:23508e7103a4 | 36 | |
| quickeman | 0:23508e7103a4 | 37 | solenoidSet(1); | 
| quickeman | 0:23508e7103a4 | 38 | } | 
| quickeman | 0:23508e7103a4 | 39 | |
| quickeman | 0:23508e7103a4 | 40 | void ColourSensor::solenoidSet(bool state) { | 
| quickeman | 0:23508e7103a4 | 41 | // Turns solenoid on/off, sets solenoid's pull strength | 
| quickeman | 0:23508e7103a4 | 42 | // state: pass 1 or 0 for solenoid ON or OFF respectively | 
| quickeman | 0:23508e7103a4 | 43 | |
| quickeman | 0:23508e7103a4 | 44 | solenoid.write(state); | 
| quickeman | 0:23508e7103a4 | 45 | //printf("Solenoid has been set to %i\n\r", state); | 
| quickeman | 0:23508e7103a4 | 46 | |
| quickeman | 0:23508e7103a4 | 47 | solenoidState = state; | 
| quickeman | 0:23508e7103a4 | 48 | } | 
| quickeman | 0:23508e7103a4 | 49 | |
| quickeman | 0:23508e7103a4 | 50 | void ColourSensor::readIf() { | 
| quickeman | 0:23508e7103a4 | 51 | // Interrupt function (software): reads in colour detection state | 
| quickeman | 0:23508e7103a4 | 52 | |
| quickeman | 0:23508e7103a4 | 53 | // Update variables | 
| quickeman | 0:23508e7103a4 | 54 | detectedIfOld = detectedIf; | 
| quickeman | 0:23508e7103a4 | 55 | newDetection = 0; | 
| quickeman | 0:23508e7103a4 | 56 | |
| quickeman | 0:23508e7103a4 | 57 | //printf("Reading colour detection state\n\r"); | 
| quickeman | 0:23508e7103a4 | 58 | |
| quickeman | 0:23508e7103a4 | 59 | detectedIf = !inIf.read(); | 
| quickeman | 0:23508e7103a4 | 60 | |
| quickeman | 0:23508e7103a4 | 61 | if ((detectedIf && !detectedIfOld)) { | 
| quickeman | 0:23508e7103a4 | 62 | // if colour is newly detected | 
| quickeman | 0:23508e7103a4 | 63 | newDetection = 1; | 
| quickeman | 0:23508e7103a4 | 64 | toggleA = 1; | 
| quickeman | 0:23508e7103a4 | 65 | //printf("Colour newly detected\n\r"); | 
| quickeman | 0:23508e7103a4 | 66 | } | 
| quickeman | 0:23508e7103a4 | 67 | } | 
| quickeman | 0:23508e7103a4 | 68 | |
| quickeman | 0:23508e7103a4 | 69 | void ColourSensor::readWhich() { | 
| quickeman | 0:23508e7103a4 | 70 | //Interrupt function (software): reads in which colour is detected | 
| quickeman | 0:23508e7103a4 | 71 | |
| quickeman | 0:23508e7103a4 | 72 | detectedWhich = !inWhich.read(); | 
| quickeman | 0:23508e7103a4 | 73 | |
| quickeman | 0:23508e7103a4 | 74 | //printf("Colour detected; code: %d\n\r", detectedWhich); | 
| quickeman | 0:23508e7103a4 | 75 | |
| quickeman | 0:23508e7103a4 | 76 | toggleB = 1; | 
| quickeman | 0:23508e7103a4 | 77 | } | 
| quickeman | 0:23508e7103a4 | 78 | |
| quickeman | 0:23508e7103a4 | 79 | void ColourSensor::makeColourActive() { | 
| quickeman | 0:23508e7103a4 | 80 | // Interrupt function (software): reactivates colour processing | 
| quickeman | 0:23508e7103a4 | 81 | toggleConst = 1; | 
| quickeman | 0:23508e7103a4 | 82 | |
| quickeman | 0:23508e7103a4 | 83 | //printf("Colour processing Activated\n\r"); | 
| quickeman | 0:23508e7103a4 | 84 | } | 
| quickeman | 0:23508e7103a4 | 85 | |
| quickeman | 0:23508e7103a4 | 86 | void ColourSensor::process() { | 
| quickeman | 0:23508e7103a4 | 87 | // Processes change of state in colour detection | 
| quickeman | 0:23508e7103a4 | 88 | //printf("Colour process() called\n\r"); | 
| quickeman | 0:23508e7103a4 | 89 | |
| quickeman | 0:23508e7103a4 | 90 | if (!diskHave && newDetection) { | 
| quickeman | 0:23508e7103a4 | 91 | // If: No disk & colour newly detected | 
| quickeman | 0:23508e7103a4 | 92 | //printf("Colour detected; collecting disk\n\r"); | 
| quickeman | 0:23508e7103a4 | 93 | |
| quickeman | 0:23508e7103a4 | 94 | diskHave = 1; | 
| quickeman | 0:23508e7103a4 | 95 | diskColour = detectedWhich; | 
| quickeman | 0:23508e7103a4 | 96 | } | 
| quickeman | 0:23508e7103a4 | 97 | |
| quickeman | 0:23508e7103a4 | 98 | else if ((diskHave && newDetection) && (detectedWhich == diskColour)) { | 
| quickeman | 0:23508e7103a4 | 99 | // If: Have disk & colour newly detected & disk colour is same as detected colour, temporarily turn off solenoid | 
| quickeman | 0:23508e7103a4 | 100 | //printf("Correct colour detected; depositing disk\n\r"); | 
| quickeman | 0:23508e7103a4 | 101 | |
| quickeman | 0:23508e7103a4 | 102 | solenoidSet(0); | 
| quickeman | 0:23508e7103a4 | 103 | flagColour = 1; | 
| quickeman | 0:23508e7103a4 | 104 | diskHave = 0; | 
| quickeman | 0:23508e7103a4 | 105 | } | 
| quickeman | 0:23508e7103a4 | 106 | |
| quickeman | 0:23508e7103a4 | 107 | /*else if ((diskHave && newDetection) && (detectedWhich != diskColour)) { | 
| quickeman | 0:23508e7103a4 | 108 | // If: Have disk & colour newly detected & disk colour is NOT same as detected colour, update variables | 
| quickeman | 0:23508e7103a4 | 109 | //printf("Wrong colour detected; keeping disk\n\r"); | 
| quickeman | 0:23508e7103a4 | 110 | }*/ | 
| quickeman | 0:23508e7103a4 | 111 | |
| quickeman | 0:23508e7103a4 | 112 | else { | 
| quickeman | 0:23508e7103a4 | 113 | //printf("Cool (y)\n\r"); | 
| quickeman | 0:23508e7103a4 | 114 | } | 
| quickeman | 0:23508e7103a4 | 115 | |
| quickeman | 0:23508e7103a4 | 116 | // Temporarily disable colour processing | 
| quickeman | 0:23508e7103a4 | 117 | //printf("Disabling colour processing\n\r"); | 
| quickeman | 0:23508e7103a4 | 118 | toggleConst = 0; | 
| quickeman | 0:23508e7103a4 | 119 | } |