CONTADOR DE OBJETOS INFRAROJO E18

Disculpen estoy tratando de hacer un contador de objetos detectados con un sensor E18 conectado al arduino pero no lo logro hacer funcionar este es mi codigo...por favor ayuda

#include <LiquidCrystal.h>

int ledPin = 13; // choose pin for the LED
int inputPin = A4; // choose input pin (for Infrared sensor)
int val = 0; // variable for reading the pin status
int cantidad = 0;

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); //conexiones a pantalla

void setup() { //declaraciones de varibales
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare Infrared sensor as input
lcd.begin(16, 2);
lcd.setCursor(0,0);
lcd.print("cantidad:");
}

void loop() { //codigo de ejecucion
val = digitalRead(inputPin); // read input value

if (val == HIGH) { // check if the input is HIGH
lcd.print(cantidad);
delay(3000);
cantidad = cantidad +1 ;
digitalWrite(ledPin, HIGH); // turn LED ONN

} else {
delay(3000);
cantidad = cantidad +val ;
lcd.setCursor(1,1);

digitalWrite(ledPin, LOW); // turn LED Off

}

}

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

Utilice etiquetas de código, por favor.

a ver para comenzar imprimes antes de sumar cantidad, con lo que no se actualizara el valor hasta el siguiente objeto

para seguir en el else sumas val a cantidad que no tiene ninguna logica, val solo puede ser HIGH o LOW por que la has obternido del digitalread esa linea sobra, y el lcd setcursor tambien sobra, puedes hacer en el if high

además no tienes que inicializar val ni con 0 ni con uno si acaso con LOW o HIGH, pero tampoco por que se lee antes de usarse

#include <LiquidCrystal.h>

int ledPin = 13; // choose pin for the LED
int inputPin = A4; // choose input pin (for Infrared sensor)
int val; // variable for reading the pin status
int cantidad = 0;

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12); //conexiones a pantalla

void setup() { //declaraciones de varibales 
  pinMode(ledPin, OUTPUT); // declare LED as output
  pinMode(inputPin, INPUT); // declare Infrared sensor as input
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("cantidad:");
}

void loop() { //codigo de ejecucion 
  val = digitalRead(inputPin); // read input value
  
  if (val == HIGH) { // check if the input is HIGH
    cantidad = cantidad +1 ;
    lcd.setCursor(1,1);
    lcd.print(cantidad);
    delay(3000); 
    digitalWrite(ledPin, HIGH); // turn LED ON
  
} else {
    delay(3000);
    digitalWrite(ledPin, LOW); // turn LED Off
  
}
  
  
  
  }