Factory Monitor

Dependencies:   mbed

factory sense HQ

Committer:
Nick123
Date:
Sat Aug 15 11:37:09 2015 +0000
Revision:
2:cff64a27ae9d
Parent:
1:b52588ac9aae
Child:
3:cff56a809a37
i think this is right for the class set up

Who changed what in which revision?

UserRevisionLine numberNew 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 int sensorState1 = 0; // current state of the sensor
melmon 0:a521da10b15a 7 int lastSensorState1 = 0; // previous state of the sensor
Nick123 1:b52588ac9aae 8 int sensorState2 = 0;
Nick123 1:b52588ac9aae 9 int lastSensorState2 = 0;
melmon 0:a521da10b15a 10
melmon 0:a521da10b15a 11 // initialize serial communication:
melmon 0:a521da10b15a 12 Serial pc(USBTX, USBRX);
melmon 0:a521da10b15a 13
Nick123 2:cff64a27ae9d 14 class PinCheck {
Nick123 2:cff64a27ae9d 15 public:
Nick123 2:cff64a27ae9d 16 PinCheck(PinName pin) : _pin(pin) {
Nick123 2:cff64a27ae9d 17
Nick123 2:cff64a27ae9d 18 }
Nick123 2:cff64a27ae9d 19
Nick123 2:cff64a27ae9d 20
Nick123 2:cff64a27ae9d 21 void Go_and_check() {
Nick123 2:cff64a27ae9d 22 sensorState(_pin) = sensorPin(_pin); // read the sensor input pin:
melmon 0:a521da10b15a 23
Nick123 2:cff64a27ae9d 24 if (sensorState(_pin) != lastSensorState(_pin) // compare the sensorState to its previous state
melmon 0:a521da10b15a 25 {
melmon 0:a521da10b15a 26 // if the state has changed, check to see if it is a change from high to low or low to high
Nick123 2:cff64a27ae9d 27 if (sensorState(_pin) == 1){ // if the current state is HIGH then increment the counter
Nick123 2:cff64a27ae9d 28 return n;
melmon 0:a521da10b15a 29 }
melmon 0:a521da10b15a 30
Nick123 1:b52588ac9aae 31 else { // if the current state is LOW then ignore
Nick123 2:cff64a27ae9d 32 return n;
melmon 0:a521da10b15a 33 }
melmon 0:a521da10b15a 34 }
Nick123 2:cff64a27ae9d 35 lastSensorState(_pin) = sensorState(_pin);
Nick123 1:b52588ac9aae 36 }
Nick123 2:cff64a27ae9d 37 private:
Nick123 2:cff64a27ae9d 38 DigitalIn _pin;
Nick123 2:cff64a27ae9d 39 };
Nick123 2:cff64a27ae9d 40
Nick123 1:b52588ac9aae 41
Nick123 1:b52588ac9aae 42 int main() {
Nick123 1:b52588ac9aae 43 // variables:
Nick123 1:b52588ac9aae 44 int outputs = 0; // counter for the number of products passing through
Nick123 1:b52588ac9aae 45 int inputs = 0; //counter for the number of products entering station
Nick123 1:b52588ac9aae 46 int rejects = 0; //counter for products ejected from lines between stations
Nick123 1:b52588ac9aae 47
Nick123 1:b52588ac9aae 48 while(1)
Nick123 1:b52588ac9aae 49 {
Nick123 2:cff64a27ae9d 50 outputs += sensorPin1.Go_and_check();
Nick123 2:cff64a27ae9d 51 inputs += sensorPin2.Go_and_check();
Nick123 1:b52588ac9aae 52 rejects = previous_outputs - inputs;
Nick123 1:b52588ac9aae 53
melmon 0:a521da10b15a 54 }
melmon 0:a521da10b15a 55 }