Lezer Counter with Auto reset

Hey Guys !

I have a code which counting the hits of a sensor and I need the program to reset the code once it count 100 hits :o your help will be appreciated , I have already tried digitalRead method but there is something wrong.

THE CODE //

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);
int ledPin = 13;
int pirPin = 10;
int pirState = LOW;
int val = 0;
int counter = 0;
int currentState = 0;
int previousState = 0;

void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(mk, OUTPUT);

lcd.init();
lcd.backlight();

lcd.setCursor(0,0);
lcd.print(“Auto Count:”);

lcd.setCursor(4, 1);
lcd.print(counter);
}
void loop()

{

val = digitalRead(pirPin);
if (val == HIGH)

{

digitalWrite(ledPin, HIGH);
if (pirState == LOW) {

currentState = 1;
pirState = HIGH;
delay(100);
}

}

else {

digitalWrite(ledPin, LOW);

if (pirState == HIGH){

currentState = 0;
pirState = LOW;

}

}
if(currentState != previousState){
if(currentState == 1){
counter ++;
lcd.setCursor(4,1);
lcd.print(counter);
delay(100);

}
}
}

I have already tried digitalRead method

What have you tried reading? Why did that seem like the right thing to do?

I need the program to reset the code

What does "reset the code" mean to you? It means nothing to me.

int counter = 0;

It would be far more useful to know WHAT you are counting. The variable name should reflect that.

If you mean you want to reset the counter variable once it gets to 100 why not just put something in that says if the counter is 100 then set counter to 0?

If "reset the code" means something else a bit more explanation is needed.

Steve

slipstick:
If you mean you want to reset the counter variable once it gets to 100 why not just put something in that says if the counter is 100 then set counter to 0?

If "reset the code" means something else a bit more explanation is needed.

Steve

Hi Steve

sorry for the confusion that I mad, yes what I meant is to rest the counter once it's reached the desired count.

Anas

So just this?

if(currentState != previousState){
if(currentState == 1){
counter ++;
lcd.setCursor(4,1);
lcd.print(counter);
if (counter==100) counter=0; // <<<<<<<<<<<<<<<<<<
delay(100);
counter++;
if (counter>99) counter=0; // <<<<<<<<<<<<<<<<<<
lcd.setCursor(4,1);
lcd.print(counter);

would be better.[/code]

ChrisTenone:

counter++;

if (counter>99) counter=0; // <<<<<<<<<<<<<<<<<<
lcd.setCursor(4,1);
lcd.print(counter);




would be better.[/code]

ardy_guy:
So just this?

if(currentState != previousState){

if(currentState == 1){
counter ++;
lcd.setCursor(4,1);
lcd.print(counter);
if (counter==100) counter=0; // <<<<<<<<<<<<<<<<<<
delay(100);

THANKS TO ALL :art:

ANAS25:
THANKS TO ALL :art:

ANAS25:
THANKS TO ALL :art:

Guys,

I still have a problem once the counting hits 99, counter will go to 0

if(currentState != previousState){
if(currentState == 1){
counter ++;
lcd.setCursor(4,1);
lcd.print(counter);
if (counter==11) counter=0;
delay(100);}

the problem is when its hit 99 the LCD shows 09, 19,29 up 99 then it count right?
:o

You need to overwrite the 9 that's there from 99, the 0 only covers the first 9 so it shows 09...

Probably easiest to print a leading space if counter is under 10, then the 1, 2, 3 etc from counter, so it actually puts the new 1, 2, 3 etc in the second position so the units always line up.