IR Object counter with I2C 0.91 zoll OLED/ display

Hi everyone, I'm new to the arduino field and that's why I need your help to create an object counter with IR sensor and 0.91 I2C display.

This is my code, I have no idea if it is well written. But it is definitely misspelled because it doesn't work for me, I can compile and load it but it doesn't do what it should do, it just shows me on the display '' turn counter ''.
Could anyone help me? For experts it is definitely a game and you can write it in 5 minutes.
I'm sorry for my English, it's very bad.


#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 
#define SCREEN_HEIGHT 64 

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int x = 0;
int input = A0;
int state = 0;

void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);  
  display.clearDisplay();

  display.setTextSize(1,3);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("  Turn counter ");
  display.display(); 
  display.setCursor(0, 1);
  display.print(x);
  display.print("     =turn  ");
  delay(100);
 
}

void loop()
{
  int counter = digitalRead(A0);
  if (state == 0)
  {
    switch (counter) {

      case 1 : state = 1; display.setCursor (0, 1); x = x + 1; display.print(x); break;
      case 0 : state = 0; break;

    }
  }

  if (counter == LOW) {
    state = 0;
  }
}

Have you got a pullup or pulldown resistor in place to keep the input at a known state at all time ?

I have a MH-Sensor-Series Flying-Fish.
do i need a resistor for this IR sensor?

Because I'm new here, I can't unfortunately attach pictures with the sensor and the display.

Here is a video about an IR distance sensor:

Are you using that sensor? Are you using the digital output or the analog output?

It's not this sensor, I have one with VCC GND and OUT. I have two big LEDs.I use the analog output (A0) but in my code it says digital output. I also tried the digital output and it still didn't work.

Are you saying that your Flying Fish has three pins, not four?

If you use the digital output, the adjustable pot on the Flying Fish board selects the value at which the digital output switches from LOW to HIGH. Have you turned the pot to select the sensitivity of your sensor? There is a light on the Flying Fish board that lights up when the digital output goes HIGH. Connect the Flying Fish board to Ground and +5V and see if you can get the light to come on by putting an object near the sensor and turning the pot.

Are those on the end of the Flying Fish board? I would expect that one is an IR LED and the other is an IR sensor.

I made a video. I solved it a bit, but now my problem is that it doesn't count my objects, so it has to be something from the code.

Try this simplified code. Is the output pin of your sensor connected to the A0 pin of your Arduino?

#include <Wire.h>
// #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int Count = 0;
const int InputPin = A0;
int LastInputState = LOW;

void setup()
{
  Serial.begin(115200);

  pinMode(InputPin, INPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  // Address 0x3D for 128x64
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1, 3);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("  Turn counter ");
  display.display();
  display.setCursor(0, 1);
  display.print(Count);
  display.print("     =turn  ");
  delay(100);
}

void loop()
{
  int inputState = digitalRead(InputPin);
  
  if (inputState == HIGH && LastInputState == LOW)
  {
    Count++;
    display.setCursor (0, 1);
    display.print(Count);
  }

  LastInputState = inputState;
}

Yes,the output pin of my sensor is connected to the A0 pin of my arduino.
This code doesn't work, it still stays at 0....

I did not see any problems in your sketch. I made the simplified version just in case there was a problem in your sketch and I just didn't see it. If neither sketch will count, it sounds to me like a hardware problem. Maybe the output pin of the Flying Fish is not working or the A0 pin is not working.

I did the test with another sensor and set it to A1, I had the same problem. I will try another arduino. What I don't understand is that it has to read digitall (int counter = digitalRead(A0);) but it is connected to analog (A0). Is it normal?

I tried with int counter = analogRead(A0); but it still didn't work.

I can't tell from here if the output of your Flying Fish is analog or digital. Some have both. Connecting an analog output to a digital pin could explain why you are not seeing a signal.

If you run the File->Examples->03.Analog->AnalogInOutSerial sketch do you get numbers on Serial Monitor that change when you put something in front of your sensor?

After loading this example, the display still shows me the text from the object counter and nothing happens.
The description on amazon says that it has digital output

Did you open Serial Monitor and set the baud rate to match the example sketch? You can't expect an example sketch to know anything about your LCD display.

I tried now and it doesn't work. So my sensor is digital. I tried with digitalinputpullup (examples-02.digital) and it works. I get 1 and when I operate the sensor I get 0.

Now I put the D2 output on the sensor.
SCL on the display is on A5 and SDA on A4. Is my code correct now? Should it be mentioned somewhere in the code? SDA = A4
SCL = A5

#include <Wire.h>
// #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int Count = 0;
//const int InputPin = A0;
const byte D2 = 2;
const int InputPin = 2;
int LastInputState = LOW;

void setup()
{
  Serial.begin(115200);

  pinMode(InputPin, INPUT);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  // Address 0x3D for 128x64
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(2000);
  display.clearDisplay();

  display.setTextSize(1, 3);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("  Turn counter ");
  display.setCursor(40, 20);
  display.print(Count);
  display.print("     =turn  ");
  display.display();
  delay(100);
}

void loop()
{
  int inputState = digitalRead(InputPin);
  
  if (inputState == HIGH && LastInputState == LOW)
  {
    Count++;
    display.setCursor (40, 20);
    display.print(Count);
  }

  LastInputState = inputState;
}

Not necessary. The Wire library knows which pins are the I2c/Wire/TWI pins. If you are defining a bunch of pin names you
might want to add a comment for A4 and A5 to remind whoever reads the sketch that those pins are in use.

Okay ... Then I don't realize what it can be ... I'm a beginner and as I said I don't know very well, but there doesn't seem to be anything wrong with my code, but I guess it's something we omit ...