1

So, trying to configure this code so that when I push the button, the person counter will go to zero and just as it goes, the counter will increase when the LDR will get tripped and again when the button is pressed, the counter will go to 0.

#include <LiquidCrystal.h>

int laser=8;
int pushbutton=12;
int flag=0; 
int myCounter = 0; 
LiquidCrystal lcd(7,6,5,4,3); 

void setup() 
{ 
    pinMode(laser,OUTPUT); pinMode(pushbutton,INPUT); 
    lcd.begin(16, 2); 
    lcd.print("Person Counter:"); lcd.setCursor(0,1); 
    lcd.print(0); 
} 

void loop() 
{
    lcd.setCursor(0, 1);
    digitalWrite(laser,HIGH); 

    if(analogRead(A0) < 600) //A0= LDR
    {
        flag = 1;
    } 

    if(analogRead(A0) > 600 && flag == 1) 
    {
        myCounter++; 
        delay(200); 
        lcd.print(myCounter); 
        delay(20); 
        flag=0;
    } 
}
1

Try out this code. I made some modification and also comment out that things.

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3);

int laser = 8;
int pushbutton = 12;
int flag = 0; 
int myCounter = 0; 

void setup() 
{ 
  pinMode(laser,OUTPUT);
  pinMode(pushbutton,INPUT_PULLUP); // Provide pull-up

  lcd.begin(16, 2); 
  lcd.print("Person Counter:");
  lcd.setCursor(0,1); 
  lcd.print(0); 
} 

void loop() 
{
  lcd.setCursor(0, 1);
  digitalWrite(laser,HIGH); 

  if(analogRead(A0) < 600) //A0= LDR
  {
    flag = 1;
  } 

  if(analogRead(A0) > 600 && flag == 1) 
  {
    myCounter++; 
    delay(200); 
    lcd.print(myCounter); 
    delay(20); 
    flag = 0;
  }

  if(digitalRead(pushbutton) == LOW) // When button pressed
  {
    myCounter = 0; // Set counter to 0
  }
}
1
  • Glad to help you. – Help2Engg Apr 18 '17 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.