#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
const int Lsensor = 7; //Sensor Pin
const int Rsensor = 6; //Sensor Pin
const int ButtonPin = 13;
const unsigned long debounceDelay = 50; // Debounce time for the button (in milliseconds)
unsigned long lastDebounceTime = 0; // Variable to store the last button press time
int Sense1;
int Sense2;
unsigned long NowTime;
unsigned long PastTime = 0;
unsigned long Period = 400;
int initial1 = LOW;
int initial2 = LOW;
int count = 0; // Counter variable
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
pinMode(Lsensor, INPUT);
pinMode(Rsensor, INPUT);
pinMode(ButtonPin, INPUT_PULLUP);
}
void loop() {
Sense1 = digitalRead(Lsensor);
Sense2 = digitalRead(Rsensor);
NowTime = millis();
//Serial.println(digitalRead(Lsensor));
if (initial1 != Sense1 && (Period <= NowTime - PastTime)) {
// Serial.print("initial");
// Serial.println(initial1);
// Serial.print("sense");
// Serial.println(Sense1);
if (Sense1 == HIGH) {
count++;
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Count: ");
lcd.print(count);
lcd.print("ft");
initial1 = HIGH;
}
if (Sense1 == LOW) {
initial1 = LOW;
}
PastTime = NowTime;
}
if (initial2 != Sense2 && (Period <= NowTime - PastTime)) {
// Serial.print("initial");
// Serial.println(initial2);
// Serial.print("sense");
// Serial.println(Sense2);
if (Sense2 == HIGH) {
count--;
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Count: ");
lcd.print(count);
lcd.print("ft");
initial2 = HIGH;
}
if (Sense2 == LOW) {
initial2 = LOW;
}
PastTime = NowTime;
}
if (digitalRead(ButtonPin) == LOW && (NowTime - lastDebounceTime >= debounceDelay)) {
count = 0;
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Count: ");
lcd.print(count);
lcd.print("ft");
lastDebounceTime = NowTime;
}
}