Monitoring two events at a time

I am working on a project in which i have four sensors installed at different locations, detecting objects and counting the number of objects that they have detected. The objects are moving with extremely high speed. The issue i am facing is when objects appear in front of any two sensors simultaneously at exactly the same time the sensor detects and count one object while misses the other one because the objects get passed the sensor till controller reaches the section of code of other sensor. Is there any way through which we can program arduino to monitor multiple events simultaneously and responding to each event separately?

What type of sensors ?
With what sort of frequency do the objects pass the sensors ?
How long are the objects in front of the sensors ?
How far apart are the sensors ?

Please post a program that you have written that shows the problem.

UKHeliBob:
What type of sensors ?
With what sort of frequency do the objects pass the sensors ?
How long are the objects in front of the sensors ?
How far apart are the sensors ?

Please post a program that you have written that shows the problem.

The sensors are Laser combined with ambient light sensor.
The frequency is 0.05
The objects are in front of sensor for 100 ms.
The sensors are about 30 cm apart from one another

The program is given under

#include <LiquidCrystal.h> //library for using LCD 
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //syntax (reset, enable, d4, d5, d6, d7) d:data 

int sensorTh=980; //threshold value of optical sensor

const int sensorPin1 = A1; //optical sensor pin of row 1
const int sensorPin2 = A2; //optical sensor pin of row 2
const int sensorPin3 = A3; //optical sensor pin of row 3
const int sensorPin4 = A4; //optical sensor pin of row 4

int sensorValue1 = 0;  // optical sensor value of row 1
int sensorValue2 = 0;  // optical sensor value of row 2
int sensorValue3 = 0;  //optical sensor value of row 3
int sensorValue4 = 0;  //optical sensor value of row 4

int counter1=0; //counter of row 1
int counter2=0; //counter of row 2
int counter3=0; //cpunter of row 3
int counter4=0; //counter of row 4

int state1=LOW; //state of detection of row 1 is initially low
int state2=LOW; //state of detection of row 2 is initially low
int state3=LOW; //state of detection of row 3 is initially low
int state4=LOW; //state of detection of row 4 is initially low

void setup()
{
  Serial.begin(9600); //Initiate serial communcation and baud rate
  lcd.begin(20,4);  // syntax lcd.begin (rows,columns)  
  lcd.clear(); // clears the lcd screen
  lcd.setCursor(0,0);
  lcd.print("ROW-1: 0");
  lcd.setCursor(0,1);
  lcd.print("ROW-2: 0");
  lcd.setCursor(0,2);
  lcd.print("ROW-3: 0");
  lcd.setCursor(0,3);
  lcd.print("ROW-4: 0");
}

void loop() 
{
  ////////////////////////////////////////////////////////////////////////////////////
  sensorValue1 = analogRead(sensorPin1); //read the value of optical sensor of row 1
  if ((state1==LOW)&&(sensorValue1<sensorTh)) //check if sensor is ready to detect seed
  { 
    counter1++; //increment once entering in the loop
    lcd.setCursor(0,0);
    lcd.print("ROW-1:    ");
    lcd.print(counter1);
    state1=HIGH; //specify a detection has been performed and now waiting for the seed to be cleared
  }
  else if ((state1==HIGH)&&(sensorValue1>sensorTh))
  {
    state1=LOW;
  }
/////////////////////////////////////////////////////////////////////////////////////////////
  sensorValue2 = analogRead(sensorPin2); //read the value of optical sensor of row 2
  if ((state2==LOW)&&(sensorValue2<sensorTh)) //check if sensor is ready to detect seed
  { 
    counter2++; //increment once entering in the loop
    lcd.setCursor(0,1);
    lcd.print("ROW-2:    ");
    lcd.print(counter2);
    state2=HIGH; //specify a detection has been performed and now waiting for the seed to be cleared
  }
  else if ((state2==HIGH)&&(sensorValue2>sensorTh))
  {
    state2=LOW;
  }  
//////////////////////////////////////////////////////////////////////////////////////////////////////
  sensorValue3 = analogRead(sensorPin3); //read the value of optical sensor of row 3  
  if ((state3==LOW)&&(sensorValue3<sensorTh)) //check if sensor is ready to detect seed
  { 
    counter3++; //increment once entering in the loop
    lcd.setCursor(0,2);
    lcd.print("ROW-3:    ");
    lcd.print(counter3);
    state3=HIGH; //specify a detection has been performed and now waiting for the seed to be cleared
  }
  else if ((state3==HIGH)&&(sensorValue3>sensorTh))
  {
    state3=LOW;
  }
 /////////////////////////////////////////////////////////////////////////////////////////////////
  sensorValue4 = analogRead(sensorPin4); //read the value of optical sensor of row 4
  if ((state4==LOW)&&(sensorValue4<sensorTh)) //check if sensor is ready to detect seed
  { 
    counter4++; //increment once entering in the loop
    lcd.setCursor(0,3);
    lcd.print("ROW-4:    ");
    lcd.print(counter4);
    state4=HIGH; //specify a detection has been performed and now waiting for the seed to be cleared
  }
  else if ((state4==HIGH)&&(sensorValue4>sensorTh))
  {
    state4=LOW;
  }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////

Hi,

The frequency is 0.05

Hz or Seconds?

Tom... :slight_smile:

TomGeorge:
Hi,Hz or Seconds?

Tom… :slight_smile:

The frequency is generally in Hz as the same case here.

usmanqadir91:
The objects are in front of sensor for 100 ms.

That's a long time.

Your code has a whole lot of stuff between the readings of the different sensors. Read all the sensors one after the other and then do whatever needs to be done with the data.

...R

Robin2:
That's a long time.

...R
So then why the seeds are missed?

I was editing Reply #5 while you were replying.

...R

Robin2:
That’s a long time.

Your code has a whole lot of stuff between the readings of the different sensors. Read all the sensors one after the other and then do whatever needs to be done with the data.

…R

I rechecked for the time for which the object is in front of sensor and it ranges from 30 ms to 50 ms

usmanqadir91:
I rechecked for the time for which the object is in front of sensor and it ranges from 30 ms to 50 ms

Still plenty of time.

If it was 50 microsecs I could see a challenge.

I notice that you are using analogRead() (which takes about 100 µsecs IIRC). But if you just need to detect the presence or absence of something you could probably wire the optical detector to give a HIGH or LOW and use digitalRead().

...R

Maybe your seeds are too small to be detected reliably. A small seed might not cover the complete sensor and it might still see light.

Can you 100% reliably count one sensor?

Hi,

The sensors are Laser combined with ambient light sensor.

What sort of sensor?

Can you please post a copy of your sensor input circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

0.05Hz = period of 20Seconds between objects.

Tom... :slight_smile: