Please help me figure this out

Hello,
My name is Jordy and I am working on a counter. Not a normal counter, an airsoft ammo counter. Currently I have the following code, but it does not register if something passes the IR sensor:

I am using an I2c adaptor on my LCD screen. IR sensor is connected to the Digital pin 2 and 2 ICSP pins (right-under and Left-under)

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>
#include <Wire.h>
#define ir 13
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(2,INPUT);
  lcd.init();                   // initialiseer het LCD scherm
  lcd.backlight();              // zet de backlight aan

  lcd.clear();                  // wis het scherm
  lcd.setCursor(0, 0);          // zet de cursor op positie 1, regel 1
  lcd.print("AMMO COUNTER:");  // schrijf op scherm
}
int counter = 0;// VARIABLE TO COUNT THE NUMBER OF VISITOR

void loop() {
  lcd.clear();
 lcd.print("AMMO COUNT:");
 lcd.setCursor(0,1);
 lcd.print(counter);// DISPLAYING THE NUMBER OF VISITS.
 delay(50);
  if(digitalRead(ir)==1)// CHECKING INPUT
 {
while(digitalRead(ir)==1)// WAITING FOR INPUT TO END
{};

}
else
{

  }
  
   lcd.clear();// CASE WHEN NO INPUT IS RECEIVED.
 lcd.print("NO AMMO COUNT:");
 lcd.setCursor(0,2);
 lcd.print(counter);
 delay(50);
  }

OK. I will help.

Does it register your hand in front of the sensor?

Does it register the airsoft ammo in front of the sensor?

You aren't doing anything when something passes the IR sensor, except for waiting until it has passed. Nowhere are you even changing the value of counter.

It also appears you defined the input as pin 2, but are reading the ir input from pin 13.

IR sensor is connected to the Digital pin 2

Why are you including the IRremote and IRremoteInt header files?
Why are you including the ir_Lego_PF_BitStreamEncoder header file?

How do you think you are going to catch the airsoft ammo passing the IR sensor, when you bury your head in the sand so much?

Your final code will only have delay() like this:

// I am smart enough to never use delay();

Hello Guys, thanks for the replies. I am pretty new in this and I only have the knowledge of watching youtube vids about this.
What I want is an airsoft BB counter. I compared a view codes and thought this was close, apparently not? The sensor is showing a green light when my hand is in front, and nothing when it's not. What I want is that every time a BB gets past the sensor, my I2C lcd does a +1 on a counter. Help!

An IR sensor is probably a bad choice for detecting airsoft BB''s, because an infrared sensor detects heat. Works well with your hand, because your hand is warmer than the background temperature, but an airsoft BB will be room temperature.

david_2018:
An IR sensor is probably a bad choice for detecting airsoft BB''s, because an infrared sensor detects heat. Works well with your hand, because your hand is warmer than the background temperature, but an airsoft BB will be room temperature.

@David_2018. Please research IR sensors. There are many types and not all detect heat.

VIPheadhunter:
Hello Guys, thanks for the replies. I am pretty new in this and I only have the knowledge of watching youtube vids about this.
What I want is an airsoft BB counter. I compared a view codes and thought this was close, apparently not? The sensor is showing a green light when my hand is in front, and nothing when it’s not. What I want is that every time a BB gets past the sensor, my I2C lcd does a +1 on a counter. Help!

That’s good.

Only include header files when you know you need them.

Therefore you know you need

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

because you have an I2C LCD display.

Since you don’t know you need

#include <boarddefs.h>
#include <IRremote.h>
#include <IRremoteInt.h>
#include <ir_Lego_PF_BitStreamEncoder.h>

Let’s remove these.

Know to learn about loop().
loop() runs continuously. Therefore I’m sure you notice flickering on the LCD because you keep clearing the entire display and then redrawing it. Let’s only do that when the count is changed. Move all of you LCD writing stuff into a subroutine and only call it when the count is changed.

That cleans up your program, and also goes toward fixing the problem. The problem is that you can’t detect the split second the ammo goes past because the program is busy writing to the display. Which goes back to only writing to the display when you need to. Let’s try this and see if digitalRead() will be fast enough to detect the ammo. If not we will have to rewrite to use the interrupt which fires when the signal changes.

Hello @adwsystems

Thanks for heping me out! I removed the unnecessary libraries, but after that you lost me.
I may have a solution if you'd agree. Can you write the code or edit mine, for a compensation. Maybe a view bucks for the hard work and helping me out? Hope to hear from you soon!

Guys, thanks for all the help! I managed to do it with your help and this code:

#include <Wire.h>
#define ir 2
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(2,INPUT);
  lcd.init();                   // initialiseer het LCD scherm
  lcd.backlight();              // zet de backlight aan

  lcd.clear();                  // wis het scherm
  lcd.setCursor(0, 0);          // zet de cursor op positie 1, regel 1
  lcd.print("AMMO COUNTER:");  // schrijf op scherm
}
int counter = 150;// VARIABLE TO COUNT THE NUMBER OF VISITOR

void loop() {
  lcd.clear();
 lcd.print("AMMO COUNT:");
 lcd.setCursor(0,1);
 lcd.print(counter);// DISPLAYING THE NUMBER OF VISITS.
 delay(50);
  if(digitalRead(ir)==1)// CHECKING INPUT
 {
while(digitalRead(ir)==1)// WAITING FOR INPUT TO END
{};

}
else
{

  }
  
 counter--;// CASE WHEN NO INPUT IS RECEIVED.
 lcd.setCursor(0,2);
 lcd.print(counter);
 delay(50);
  }

I hope it is fast enough but this needs some testing first. If it is not, you suggested digitalread? How to managed that in this code?

if(digitalRead(ir)==1)// CHECKING INPUT
 {
while(digitalRead(ir)==1)// WAITING FOR INPUT TO END
{};

}

Why the "if"?

adwsystems:
@David_2018. Please research IR sensors. There are many types and not all detect heat.

Sorry, my mistake.

VIPheadhunter:
Hello @adwsystems

Thanks for heping me out! I removed the unnecessary libraries, but after that you lost me.
I may have a solution if you'd agree. Can you write the code or edit mine, for a compensation. Maybe a view bucks for the hard work and helping me out? Hope to hear from you soon!

If you get the basics down with a little guidance. We will be more than willing to help you with "alternate" digital read.

This code in loop

  lcd.clear();
 lcd.print("AMMO COUNT:");
 lcd.setCursor(0,1);
 lcd.print(counter);// DISPLAYING THE NUMBER OF VISITS.

and this code is setup()

  lcd.clear();                  // wis het scherm
  lcd.setCursor(0, 0);          // zet de cursor op positie 1, regel 1
  lcd.print("AMMO COUNTER:");  // schrijf op scherm

Should be combined in setup. There is no reason to keep clearing and reprinting the text AMMO COUNTER.

First up. Do you know what and how to create a subroutine? If not. Please research and try.

Then put this code in a subroutine

 lcd.setCursor(0,2);
 lcd.print(counter);

and place the function call in place of those two lines.

You will need to learn and understand how to create and call functions in order to move beyond what you have.

Once you have this working or have questions. Repost your code and we can take the next step.