Code for counter

hi, hope someone can help me, as I am a total newbie.
I am building a chainlink counter for my boat. I am using a proximity sensor and I would
like the arduino to tell me how many meters of chain has been rolled out through the windlass. So the display should state 5, 10, 15, 20, 25, 30, 35 or 40 m.
So when the counter hits 250 (equals 5m on the display) 500 (equals 10m on the display) 750 (equals 15m on the display) on so forth up to 2000hits (equals 40m on the display).
I am using an Arduino Mega and 1602 display with I2C. The black wire (trigger) from
the sensor is in pin A0 - plus and minus for the sensor is from a 12v. source. The display says "Kaede counter" and on the line below "0". However when I activate the sensor with a metal object, the indication light on the sensor itself lights up, BUT the counter does not increase the to 1.... clearly I have a mistake ( I believe in the code) and I have considered how to code the scale (meter of chain let out) I hope you can help - code below:

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

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

int x = 0;
int input = A0;
int state = 0;

void setup()
{
  
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("  Kaede counter ");
  lcd.setCursor(0,1);
  lcd.print(x);
  pinMode (A0,OUTPUT);
  
  

}
void loop()
{
  digitalWrite(A0, LOW);
  int counter = digitalRead(A0);
  if (state==0)
  {
    switch (counter) {
      
      case 1 : state=1; lcd.setCursor (0, 1); x = x + 1; lcd.print(x); break;
      case 0 : state=0; break;

    }}
 

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

Can you write a simple program to prove that not on,y does the sensor light up but you can get an indication of that in you program from reading the input?

Your logic is a bit confusing to me, so I would just try the simplest report you can make about what the sens or is saying.

Or try an example sketch you might find for it.

a7

This is not making much sense. Why are you setting your input pin as an output and writing to it before reading from it?!?

Be careful with that. A Mega can only take 3.3 V signals. You are doing some kind of level shifting, aren't you?

Does your logic work on the bench, with a pushbutton in lieu of the sensor? The code should detect the change in the state of a pin (i.e. its going from high to low or the other way around).

Hello
Post the datasheet of the sensor.
I think the sketch needs a state-change-detection function for the counter. Take a view into the examples of the IDE for some ideas.
Have a nice day and enjpy coding in C++.

Thanks - I will check the IDE for ideas/exambles

Hi, Yes i know. I haven't connected the 12v sensor to the Arduino - only the signal cable. I think this could cause the problem. I will be looking for exambles to get an idea... thanks

But the signal cable is coming out of the sensor, isn't it? So, normally a signal should variate between GND and Vcc, Vcc being 12 volts in your case.

Yes, I realized after my input, that I have to connect the signal cable, also to the GND of the Mega.... but thanks very much....