Factory Monitor
Dependencies: mbed
factory sense HQ
main.cpp@1:b52588ac9aae, 2015-08-12 (annotated)
- Committer:
- Nick123
- Date:
- Wed Aug 12 14:15:29 2015 +0000
- Revision:
- 1:b52588ac9aae
- Parent:
- 0:a521da10b15a
- Child:
- 2:cff64a27ae9d
not sure how to do the (n) bit
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 | DigitalIn sensorPin3(p7); |
Nick123 | 1:b52588ac9aae | 6 | //variables |
melmon | 0:a521da10b15a | 7 | int sensorState1 = 0; // current state of the sensor |
melmon | 0:a521da10b15a | 8 | int lastSensorState1 = 0; // previous state of the sensor |
Nick123 | 1:b52588ac9aae | 9 | int sensorState2 = 0; |
Nick123 | 1:b52588ac9aae | 10 | int lastSensorState2 = 0; |
Nick123 | 1:b52588ac9aae | 11 | int sensorState3 = 0; |
Nick123 | 1:b52588ac9aae | 12 | int lastSensorState3 = 0; |
melmon | 0:a521da10b15a | 13 | |
melmon | 0:a521da10b15a | 14 | // initialize serial communication: |
melmon | 0:a521da10b15a | 15 | Serial pc(USBTX, USBRX); |
melmon | 0:a521da10b15a | 16 | |
Nick123 | 1:b52588ac9aae | 17 | void Go_and_check(sensorPin(n)) { |
Nick123 | 1:b52588ac9aae | 18 | sensorState(n) = sensorPin(n); // read the sensor input pin: |
melmon | 0:a521da10b15a | 19 | |
Nick123 | 1:b52588ac9aae | 20 | if (sensorState(n) != lastSensorState(n) // compare the sensorState to its previous state |
melmon | 0:a521da10b15a | 21 | { |
melmon | 0:a521da10b15a | 22 | // if the state has changed, check to see if it is a change from high to low or low to high |
Nick123 | 1:b52588ac9aae | 23 | if (sensorState(n) == 1){ // if the current state is HIGH then increment the counter |
Nick123 | 1:b52588ac9aae | 24 | return 1; |
melmon | 0:a521da10b15a | 25 | } |
melmon | 0:a521da10b15a | 26 | |
Nick123 | 1:b52588ac9aae | 27 | else { // if the current state is LOW then ignore |
Nick123 | 1:b52588ac9aae | 28 | return 0; |
melmon | 0:a521da10b15a | 29 | } |
melmon | 0:a521da10b15a | 30 | } |
Nick123 | 1:b52588ac9aae | 31 | lastSensorState(n) = sensorState(n); |
Nick123 | 1:b52588ac9aae | 32 | } |
Nick123 | 1:b52588ac9aae | 33 | |
Nick123 | 1:b52588ac9aae | 34 | int main() { |
Nick123 | 1:b52588ac9aae | 35 | // variables: |
Nick123 | 1:b52588ac9aae | 36 | int outputs = 0; // counter for the number of products passing through |
Nick123 | 1:b52588ac9aae | 37 | int inputs = 0; //counter for the number of products entering station |
Nick123 | 1:b52588ac9aae | 38 | int rejects = 0; //counter for products ejected from lines between stations |
Nick123 | 1:b52588ac9aae | 39 | |
Nick123 | 1:b52588ac9aae | 40 | while(1) |
Nick123 | 1:b52588ac9aae | 41 | { |
Nick123 | 1:b52588ac9aae | 42 | outputs += Go_and_check(sensorPin1); |
Nick123 | 1:b52588ac9aae | 43 | inputs += Go_and_check(sensorPin2); |
Nick123 | 1:b52588ac9aae | 44 | rejects = previous_outputs - inputs; |
Nick123 | 1:b52588ac9aae | 45 | |
melmon | 0:a521da10b15a | 46 | } |
melmon | 0:a521da10b15a | 47 | } |
Nick123 | 1:b52588ac9aae | 48 |