Factory Monitor
Dependencies: mbed
factory sense HQ
main.cpp@8:e417787cb2fa, 2015-09-03 (annotated)
- Committer:
- Nick123
- Date:
- Thu Sep 03 11:38:41 2015 +0000
- Revision:
- 8:e417787cb2fa
- Parent:
- 7:854af9ba3146
- Child:
- 9:3ee4f273d7d6
I don't know how to do the serial out in mbed format.... sorry this has taken so long :(
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
melmon | 0:a521da10b15a | 1 | #include "mbed.h" |
melmon | 0:a521da10b15a | 2 | |
melmon | 4:7a50e0818a47 | 3 | #define no_pins 2 |
Nick123 | 5:faa8843f3990 | 4 | #define Inputs 0 |
Nick123 | 5:faa8843f3990 | 5 | #define Outputs 1 |
melmon | 4:7a50e0818a47 | 6 | #define Rejects Values[2] |
melmon | 4:7a50e0818a47 | 7 | |
melmon | 4:7a50e0818a47 | 8 | |
melmon | 0:a521da10b15a | 9 | DigitalIn sensorPin1(p5); |
Nick123 | 1:b52588ac9aae | 10 | DigitalIn sensorPin2(p6); |
Nick123 | 1:b52588ac9aae | 11 | //variables |
melmon | 0:a521da10b15a | 12 | |
melmon | 0:a521da10b15a | 13 | // initialize serial communication: |
melmon | 0:a521da10b15a | 14 | Serial pc(USBTX, USBRX); |
melmon | 0:a521da10b15a | 15 | |
Nick123 | 2:cff64a27ae9d | 16 | class PinCheck { |
melmon | 3:cff56a809a37 | 17 | |
Nick123 | 2:cff64a27ae9d | 18 | public: |
Nick123 | 2:cff64a27ae9d | 19 | PinCheck(PinName pin) : _pin(pin) { |
melmon | 3:cff56a809a37 | 20 | |
Nick123 | 2:cff64a27ae9d | 21 | } |
Nick123 | 2:cff64a27ae9d | 22 | |
Nick123 | 2:cff64a27ae9d | 23 | |
melmon | 4:7a50e0818a47 | 24 | int Go_and_check(int a) { |
melmon | 3:cff56a809a37 | 25 | sensorState = _pin; // read the sensor input pin: |
melmon | 4:7a50e0818a47 | 26 | int zero = 0; |
melmon | 4:7a50e0818a47 | 27 | if (sensorState != lastSensorState[a]) // compare the sensorState to its previous state |
melmon | 0:a521da10b15a | 28 | { |
melmon | 0:a521da10b15a | 29 | // if the state has changed, check to see if it is a change from high to low or low to high |
melmon | 3:cff56a809a37 | 30 | if (sensorState == 1){ // if the current state is HIGH then increment the counter |
melmon | 7:854af9ba3146 | 31 | lastSensorState[a] = sensorState; |
melmon | 3:cff56a809a37 | 32 | return sensorState; |
melmon | 0:a521da10b15a | 33 | } |
melmon | 0:a521da10b15a | 34 | |
Nick123 | 5:faa8843f3990 | 35 | else { // if the current state is LOW then ignore |
Nick123 | 8:e417787cb2fa | 36 | lastSensorState[a] = sensorState; |
melmon | 4:7a50e0818a47 | 37 | return zero; |
melmon | 0:a521da10b15a | 38 | } |
melmon | 0:a521da10b15a | 39 | } |
melmon | 4:7a50e0818a47 | 40 | return zero; |
Nick123 | 1:b52588ac9aae | 41 | } |
melmon | 3:cff56a809a37 | 42 | |
Nick123 | 2:cff64a27ae9d | 43 | private: |
Nick123 | 2:cff64a27ae9d | 44 | DigitalIn _pin; |
melmon | 4:7a50e0818a47 | 45 | int sensorState; |
melmon | 4:7a50e0818a47 | 46 | int lastSensorState[no_pins]; |
melmon | 4:7a50e0818a47 | 47 | |
melmon | 3:cff56a809a37 | 48 | |
Nick123 | 2:cff64a27ae9d | 49 | }; |
Nick123 | 2:cff64a27ae9d | 50 | |
melmon | 4:7a50e0818a47 | 51 | PinCheck inputs(p6); |
melmon | 4:7a50e0818a47 | 52 | PinCheck outputs(p5); |
Nick123 | 1:b52588ac9aae | 53 | |
Nick123 | 1:b52588ac9aae | 54 | int main() { |
melmon | 4:7a50e0818a47 | 55 | |
Nick123 | 8:e417787cb2fa | 56 | int Values[3]; |
Nick123 | 5:faa8843f3990 | 57 | |
Nick123 | 1:b52588ac9aae | 58 | |
Nick123 | 1:b52588ac9aae | 59 | while(1) |
Nick123 | 1:b52588ac9aae | 60 | { |
melmon | 4:7a50e0818a47 | 61 | |
melmon | 4:7a50e0818a47 | 62 | for(int i = 0; i < no_pins; i ++){ |
melmon | 4:7a50e0818a47 | 63 | if (i == Inputs){ Values[i] += inputs.Go_and_check(i); } |
melmon | 4:7a50e0818a47 | 64 | if (i == Outputs){ Values[i] += outputs.Go_and_check(i); } |
melmon | 4:7a50e0818a47 | 65 | |
melmon | 4:7a50e0818a47 | 66 | }; |
Nick123 | 8:e417787cb2fa | 67 | Rejects = (Inputs - Outputs); |
Nick123 | 8:e417787cb2fa | 68 | pc.printf(Inputs, Outputs, Rejects \n); |
Nick123 | 8:e417787cb2fa | 69 | wait(0.2); |
melmon | 0:a521da10b15a | 70 | } |
Nick123 | 8:e417787cb2fa | 71 | } |