run 2 photoelectric sensors simultaneously

Hello! I have 2 photoelectric sensors, and 1 lcd module display
i am trying to build a little conveyor , that can count passed objects and add counted objects into a display

for example :1 object passed 1 sensor = it's count as 1 on a display
same object passed next sensor= it's count +1 on a display

I got the code, it only can read from 1 sensor

If anyone can help me to add a 2nd sensor into the code and count objects i will really appreciate that

#include <Wire.h>

#include <LiquidCrystal_I2C.h>



#define counter_pin 7

LiquidCrystal_I2C lcd(0x3F,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display



bool item_detected = false;

int  item_counter = 0;



void setup()

{

  Serial.begin(9600);

  pinMode( counter_pin , INPUT);



  lcd.init();                      // initialize the lcd



  // Print a message to the LCD.

  lcd.backlight();

  lcd.setCursor(0,0);

  lcd.print("Loading...");

  lcd.setCursor(2,1);

  lcd.print("Ready... :)");

  delay(3000);

}





void loop()

{

  int val = digitalRead( counter_pin );

  if( (item_detected == false) && ( val == 0 )){

        item_detected = true;

        item_counter++;

        updateCounter();

  }

   

  else if( (item_detected == true) && ( val == 1 )){

        item_detected = false;

     

  }

}

void updateCounter(){



     lcd.clear();

     lcd.setCursor(0,0);

     lcd.print("Item count");

     lcd.setCursor(0,1);

     lcd.print(item_counter);

}

Hi,
Welcome to the forum.
Looking at your code, have you got the second sensor connected to another input?
You would basically copy your sensor detecting code you have now, again for the second input, but subtract from the item_counter variable.

You are on the right path detecting a change in the input rather than an input level.

Tom... :slight_smile:

Please explain why you want to use 2 sensors. One sensor is enough for counting objects as your current code does.

Of course you can duplicate and change the code for the first sensor for handling another sensor using counter_pin_2 and item_counter_2.