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;
}
}
asked Apr 1, 2017 at 9:46
1 Answer 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
}
}
Explore related questions
See similar questions with these tags.
lang-cpp