#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define IR_PIN 15
#define ledpin 26
# define BTN_PIN 14
LiquidCrystal_I2C lcd(0x27,16 ,2);
int count =0;
bool detected= false;
bool pressed = false;
void showLCD(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Count: ");
lcd.print(count);
lcd.setCursor(0,1);
if(count>=10){
lcd.print("Count Reached");
}
else{
lcd.print("Counting....");
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(IR_PIN, INPUT);
pinMode(ledpin,OUTPUT);
pinMode(BTN_PIN, OUTPUT);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("IR Counter");
lcd.setCursor(0,1);
lcd.print("Ready");
delay(2000);
showLCD();
Serial.println("IR Counter Ready");
}
void loop() {
// put your main code here, to run repeatedly:
int ir=digitalRead(IR_PIN);
int btn= digitalRead(BTN_PIN);
if(ir== LOW && !detected){
detected=true;
count++;
digitalWrite(ledpin,HIGH);
delay(100);
digitalWrite(ledpin,LOW);
showLCD();
Serial.print("Count: ");
Serial.print(count);
}
if(ir==HIGH)detected= false;
if(btn== LOW && !pressed){
pressed= true;
count = 0;
showLCD ();
Serial.println("Reset!");
}
if(btn== HIGH) pressed= false;
delay(50);
}