#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
const int sensor = 2; //Sensor Pin
int current; //Current reading of Sensor
int count = 0;
int initial = LOW; //Previous reading of Sensor
const int ButtonPin = 13;
int PastPress = HIGH;
int PresentPress;
int PotPin=A0;
int ReverseS = 4;
int ReverseState;
unsigned long NowTime = 0;
unsigned long PastTime = 0;
unsigned long Period = 100;
const int LED = 3;
void setup() {
Serial.begin(9600);
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
pinMode(sensor, INPUT);
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(ReverseS, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(PotPin, INPUT);
}
void loop() {
NowTime = millis();
if (Period <= NowTime - PastTime) {
PresentPress = digitalRead(ButtonPin);
if (PastPress == PresentPress) {
// digitalWrite(LED, digitalRead(ReverseS));
if( analogRead(PotPin)<=512){
digitalWrite(LED, HIGH);
ReverseState=HIGH;
}
else{
digitalWrite(LED, LOW);
ReverseState=LOW;
}
current = digitalRead(sensor);
if (initial != current) { //if sensor senses change
if (current == HIGH) { //if being currently sensed
initial = HIGH;
} else {
if (ReverseState == LOW) {
count = count + 1;
} else {
count = count - 1;
}
Serial.println(count); // print count to Serial Monitor
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Count: ");
lcd.print(count);
lcd.print("ft");
initial = current;
}
}
} else {
count = 0;
Serial.println(count); // print count to Serial Monitor
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print("Count: ");
lcd.print(count);
lcd.print("ft");
initial = current;
}
}
}