Factory Monitor
Dependencies: mbed
factory sense HQ
main.cpp@3:cff56a809a37, 2015-08-15 (annotated)
- Committer:
- melmon
- Date:
- Sat Aug 15 12:18:07 2015 +0000
- Revision:
- 3:cff56a809a37
- Parent:
- 2:cff64a27ae9d
- Child:
- 4:7a50e0818a47
Changed int to bool, restructured Class;
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 | 0:a521da10b15a | 3 | DigitalIn sensorPin1(p5); |
Nick123 | 1:b52588ac9aae | 4 | DigitalIn sensorPin2(p6); |
Nick123 | 1:b52588ac9aae | 5 | //variables |
melmon | 0:a521da10b15a | 6 | |
melmon | 0:a521da10b15a | 7 | // initialize serial communication: |
melmon | 0:a521da10b15a | 8 | Serial pc(USBTX, USBRX); |
melmon | 0:a521da10b15a | 9 | |
Nick123 | 2:cff64a27ae9d | 10 | class PinCheck { |
melmon | 3:cff56a809a37 | 11 | |
Nick123 | 2:cff64a27ae9d | 12 | public: |
Nick123 | 2:cff64a27ae9d | 13 | PinCheck(PinName pin) : _pin(pin) { |
melmon | 3:cff56a809a37 | 14 | |
Nick123 | 2:cff64a27ae9d | 15 | } |
Nick123 | 2:cff64a27ae9d | 16 | |
Nick123 | 2:cff64a27ae9d | 17 | |
melmon | 3:cff56a809a37 | 18 | bool Go_and_check() { |
melmon | 3:cff56a809a37 | 19 | sensorState = _pin; // read the sensor input pin: |
melmon | 0:a521da10b15a | 20 | |
melmon | 3:cff56a809a37 | 21 | if (sensorState != lastSensorState) // compare the sensorState to its previous state |
melmon | 0:a521da10b15a | 22 | { |
melmon | 0:a521da10b15a | 23 | // if the state has changed, check to see if it is a change from high to low or low to high |
melmon | 3:cff56a809a37 | 24 | if (sensorState == 1){ // if the current state is HIGH then increment the counter |
melmon | 3:cff56a809a37 | 25 | return sensorState; |
melmon | 0:a521da10b15a | 26 | } |
melmon | 0:a521da10b15a | 27 | |
Nick123 | 1:b52588ac9aae | 28 | else { // if the current state is LOW then ignore |
melmon | 3:cff56a809a37 | 29 | return sensorState; |
melmon | 0:a521da10b15a | 30 | } |
melmon | 0:a521da10b15a | 31 | } |
melmon | 3:cff56a809a37 | 32 | lastSensorState = sensorState; |
Nick123 | 1:b52588ac9aae | 33 | } |
melmon | 3:cff56a809a37 | 34 | |
Nick123 | 2:cff64a27ae9d | 35 | private: |
Nick123 | 2:cff64a27ae9d | 36 | DigitalIn _pin; |
melmon | 3:cff56a809a37 | 37 | bool sensorState; |
melmon | 3:cff56a809a37 | 38 | bool lastSensorState; |
melmon | 3:cff56a809a37 | 39 | |
Nick123 | 2:cff64a27ae9d | 40 | }; |
Nick123 | 2:cff64a27ae9d | 41 | |
Nick123 | 1:b52588ac9aae | 42 | |
Nick123 | 1:b52588ac9aae | 43 | int main() { |
Nick123 | 1:b52588ac9aae | 44 | // variables: |
Nick123 | 1:b52588ac9aae | 45 | int outputs = 0; // counter for the number of products passing through |
Nick123 | 1:b52588ac9aae | 46 | int inputs = 0; //counter for the number of products entering station |
Nick123 | 1:b52588ac9aae | 47 | int rejects = 0; //counter for products ejected from lines between stations |
Nick123 | 1:b52588ac9aae | 48 | |
Nick123 | 1:b52588ac9aae | 49 | while(1) |
Nick123 | 1:b52588ac9aae | 50 | { |
Nick123 | 2:cff64a27ae9d | 51 | outputs += sensorPin1.Go_and_check(); |
Nick123 | 2:cff64a27ae9d | 52 | inputs += sensorPin2.Go_and_check(); |
Nick123 | 1:b52588ac9aae | 53 | rejects = previous_outputs - inputs; |
Nick123 | 1:b52588ac9aae | 54 | |
melmon | 0:a521da10b15a | 55 | } |
melmon | 0:a521da10b15a | 56 | } |