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;
}
}