Arduino Code For Lcd Counter Display

#include <LiquidCrystal.h>  
 #define trigPin 2
 #define echoPin 4
 // initialize the library with the numbers of the interface pins
 LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
 int counter = 0;
 int currentState = 0;
 int previousState = 0;
 
 void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 lcd.begin(16, 2);
 lcd.setCursor(4, 0);
 lcd.print("counter");
 }
 
 void loop() {
 long duration, distance;
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = (duration/2) / 29.1;
 if (distance <= 10){  
 currentState = 1;
 }
 else {
 currentState = 0;
 }
 delay(200);
 if(currentState != previousState){
 if(currentState == 1){
 counter = counter + 1;
 lcd.setCursor(4,1);
 lcd.print(counter);
    }
  }
 }

Comments

Popular Posts