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;
 } 
}
SMFSW
3672 silver badges7 bronze badges
asked Apr 1, 2017 at 9:46

1 Answer 1

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
 }
}
answered Apr 1, 2017 at 11:15
1
  • Glad to help you. Commented Apr 18, 2017 at 6:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.