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.
Revision 0:23508e7103a4, committed 2019-03-18
- Comitter:
- quickeman
- Date:
- Mon Mar 18 11:21:43 2019 +0000
- Commit message:
- Version 2 of OOP Colour Sensor program.; ; Altered so solenoid is ON by default and only turns OFF when dropping disk.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ColourSensor.cpp Mon Mar 18 11:21:43 2019 +0000
@@ -0,0 +1,119 @@
+// ColourSensor.cpp
+
+#include "ColourSensor_H.h"
+#include "mbed.h"
+
+ColourSensor::ColourSensor(PinName If, PinName Which, PinName Out):
+ inIf(If), inWhich(Which), solenoid(Out) {
+ // Class constructor for colour sensor & solenoid
+ // Sets initial conditions of operation
+
+ initialConditions();
+}
+
+void ColourSensor::initialConditions() {
+ // set initial conditions & variables
+ //printf("Setting Initial Conditions\n\r");
+ detectedIf = 0;
+ detectedIfOld = 0;
+ newDetection = 0;
+ diskHave = 0;
+
+ solenoidState = 0;
+
+ toggleA = 0;
+ toggleB = 0;
+ toggleConst = 1;
+
+ flagColour = 0;
+
+ solenoidSet(1);
+}
+
+void ColourSensor::solenoidOn() {
+ // Interrupt (software): Turn ON solenoid
+ //printf("Turning solenoid back ON\n\r");
+
+ solenoidSet(1);
+}
+
+void ColourSensor::solenoidSet(bool state) {
+ // Turns solenoid on/off, sets solenoid's pull strength
+ // state: pass 1 or 0 for solenoid ON or OFF respectively
+
+ solenoid.write(state);
+ //printf("Solenoid has been set to %i\n\r", state);
+
+ solenoidState = state;
+}
+
+void ColourSensor::readIf() {
+ // Interrupt function (software): reads in colour detection state
+
+ // Update variables
+ detectedIfOld = detectedIf;
+ newDetection = 0;
+
+ //printf("Reading colour detection state\n\r");
+
+ detectedIf = !inIf.read();
+
+ if ((detectedIf && !detectedIfOld)) {
+ // if colour is newly detected
+ newDetection = 1;
+ toggleA = 1;
+ //printf("Colour newly detected\n\r");
+ }
+}
+
+void ColourSensor::readWhich() {
+ //Interrupt function (software): reads in which colour is detected
+
+ detectedWhich = !inWhich.read();
+
+ //printf("Colour detected; code: %d\n\r", detectedWhich);
+
+ toggleB = 1;
+}
+
+void ColourSensor::makeColourActive() {
+ // Interrupt function (software): reactivates colour processing
+ toggleConst = 1;
+
+ //printf("Colour processing Activated\n\r");
+}
+
+void ColourSensor::process() {
+ // Processes change of state in colour detection
+ //printf("Colour process() called\n\r");
+
+ if (!diskHave && newDetection) {
+ // If: No disk & colour newly detected
+ //printf("Colour detected; collecting disk\n\r");
+
+ diskHave = 1;
+ diskColour = detectedWhich;
+ }
+
+ else if ((diskHave && newDetection) && (detectedWhich == diskColour)) {
+ // If: Have disk & colour newly detected & disk colour is same as detected colour, temporarily turn off solenoid
+ //printf("Correct colour detected; depositing disk\n\r");
+
+ solenoidSet(0);
+ flagColour = 1;
+ diskHave = 0;
+ }
+
+ /*else if ((diskHave && newDetection) && (detectedWhich != diskColour)) {
+ // If: Have disk & colour newly detected & disk colour is NOT same as detected colour, update variables
+ //printf("Wrong colour detected; keeping disk\n\r");
+ }*/
+
+ else {
+ //printf("Cool (y)\n\r");
+ }
+
+ // Temporarily disable colour processing
+ //printf("Disabling colour processing\n\r");
+ toggleConst = 0;
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ColourSensor_H.h Mon Mar 18 11:21:43 2019 +0000
@@ -0,0 +1,57 @@
+// ColourSensor_H.h
+
+#ifndef ColourSensor_H
+#define ColourSensor_H
+
+#include "mbed.h"
+
+// ColourSensor is used to detect the coloured disks and control the Solenoid
+/* Attributes:
+ - Pinouts of sensor inputs and solenoid output
+
+ Methods:
+ - solenoidSet
+ - readColSenIf
+ - readColSenWhich
+ - colourCheck
+ - makeColourActive
+*/
+
+class ColourSensor {
+ public:
+ void solenoidSet(bool state); // Setting solenoid state
+ void solenoidOn(); // Interrupt to turn ON solenoid
+ void readIf(); // Read 'IF' sensor
+ void readWhich(); // Read 'WHICH' sensor
+ void process(); // Colour processing
+ void initialConditions(); // Sets initial conditions
+
+ DigitalIn inIf;
+ DigitalIn inWhich;
+ DigitalOut solenoid;
+
+ int firstLoop;
+ bool toggleA;
+ bool toggleB;
+ bool toggleConst; // Can be used to toggle colour processing
+
+ bool flagColour;
+
+ ColourSensor(PinName pin1, PinName pin2, PinName pin3);
+
+ void makeColourActive();
+
+
+ private:
+ bool detectedIf; // Colour detected? Yes/No -> 1/0
+ bool detectedIfOld; // Comparison bool
+ bool newDetection; // Colour newly detected? (i.e. is now but wasn't before?) Yes/No -> 1/0
+ bool detectedWhich; // Colour detected: Red/Blue -> 1/0
+ bool diskHave; // Have disk? Yes/No -> 1/0
+ bool diskColour; // Colour of disk: Red/Blue -> 1/0
+
+ bool solenoidState; // State of solenoid: On/Off -> 1/0
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Mar 18 11:21:43 2019 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "ColourSensor_H.h"
+
+Ticker detectColour;
+Timeout readColour;
+Timeout reactivateColour;
+Timeout solenoidBackOn;
+
+int main() {
+ //printf("Program has started\n\r");
+
+ ColourSensor ColourSensor(PTC16, PTC13, PTE20);
+
+ // Set call rate of colour detection function
+ detectColour.attach(callback(&ColourSensor, &ColourSensor::readIf), 0.5);
+
+// int firstLoop = 1;
+
+ while (1) {
+ // main program loop
+ /*
+ if (firstLoop) {
+ printf("Main loop has started\n\r");
+ firstLoop--;
+ }
+ */
+
+ if (ColourSensor.toggleConst && ColourSensor.toggleA) {
+ // Call readColSenWhich after x seconds
+ //printf("Toggle A\n\r");
+
+ readColour.attach(callback(&ColourSensor, &ColourSensor::readWhich), 0.2);
+
+ ColourSensor.toggleA = 0;
+ }
+
+ if (ColourSensor.toggleConst && ColourSensor.toggleB) {
+ // Process colour sensor data
+ //printf("Toggle B\n\r");
+
+ ColourSensor.process();
+
+ if(ColourSensor.flagColour) {
+ // If dropped disk
+ solenoidBackOn.attach(callback(&ColourSensor, &ColourSensor::solenoidOn), 2.0);
+ }
+
+ reactivateColour.attach(callback(&ColourSensor, &ColourSensor::makeColourActive), 2.0);
+
+ ColourSensor.toggleB = 0;
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Mar 18 11:21:43 2019 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file