Hi,
This is my first program and first post so go easy on me! I am trying to count pills through a tube using two Photodiodes/IR LED. I am testing this program with two push buttons before I move to the true sensors. What will happen is pills will slide (gravity) down a tube past the pair of sensors, the sensors will be close enough together to be both covered with just 1 pill. I am not sure how fast the pills will past, probably some fast some slow but I think I can add timing/conditioning to the program later. At the moment I am trying just to get the theory right. The below does not compile because of the second IF conditions, where it looks for the direction indicator (sensor going LOW) Can someone help to confirm my theory and clues to how I would code this.
Later on I need to count maybe up to 30 tubes of passing pills, would be interested to hear schemes on how this would be possible....or not as the case maybe.
Thanks for any help
Simon
/*
Reads a digital input on pin 2 and pin 3,
determine direction and increment counter,
print counter to the serial monitor
Code copyright Simon Miller
*/
// Assign digital pins:
const int gateFront1 = 2;
const int gateBack1 = 3;
const int gate1Led = 13;
// Assign variables
int gateFront1state = 0;
int gateBack1state = 0;
int lastgateFront1 = 0;
int lastgateBack1 = 0;
boolean trigger = false;
unsigned long timeChange;
int pillOut = 0;
int pillIn = 0;
int pillCount = 0;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(gateFront1, INPUT);
pinMode(gateBack1, INPUT);
pinMode(gate1Led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
pillCount = pillOut - pillIn;
// read the pushbutton input pin:
gateFront1state = digitalRead(gateFront1);
gateBack1state = digitalRead(gateBack1);
// check for both sensors being triggered
if (gateFront1 == HIGH && gateBack1 == HIGH) {
trigger = true;
timeChange = millis();
digitalWrite(gate1Led, HIGH);
}
else {
digitalWrite(gate1Led, LOW);
trigger = false;
}
if (gateFront1 == LOW && trigger = true)
{
pillIn ++;
}
else (gateBack1 == LOW && trigger = true)
{
pillOut ++;
}
// print results to serial
// Serial.print("number of pills in: ");
// Serial.println(pillIn);
// Serial.print("number of pills out: ");
// Serial.println(pillOut);
// Serial.print("delta pills: ");
// Serial.println(pillCount);
}