FACING PROBLEM IN COUNTER DISPLAY USING LCD-I2C WITH PUSH BUTTON

Can any one help me?

I want to make counter display using 16-2 LCD-I2C display and push button with Arduino UNO.

COUNT UP:- 1-4 (on each press of upswitch count will increase 1, up to 4).

COUNT DOWN:- 4-1(on each press of Downswitch 1 count will decrease up to 1).

I made one program but counting occurred above my prescribed limit 4.

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

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int inc =2;
int dec =3;
int initialsatate1;
int finalstate1;
int initialsatate2;
int finalstate2;
int count=1;
void setup()
{
  
  lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("counter:");
                        // Turn on the blacklight and print a message.
  lcd.backlight();
}

void loop()
{
  
  //increment part
 
 finalstate1 = digitalRead(inc);
 if (finalstate1!=initialsatate1)
 {
  delay(10);
  
  finalstate1 = digitalRead(inc);
  {
    if (finalstate1==HIGH)
  {
 
  count++;
 
  lcd.clear();
   lcd.print("counter:");
   lcd.setCursor(0,1);
   lcd.print(count);
  }
 
  initialsatate1=finalstate1;
 }}

 // decrements
 
 finalstate2 = digitalRead(dec);
 if (finalstate2!=initialsatate2)
 {
  delay(10);
  finalstate2 = digitalRead(dec);
  {
    if (finalstate2==HIGH&&count>=2)
  {
   count--;
   lcd.clear();
   lcd.print("counter:");
   lcd.setCursor(0,1);
   lcd.print(count);
  }
 
  initialsatate2=finalstate2;
} }}

what are the modifications i have to do get desired results.

You increment the count but where do you check that it has reached the upper limit ?

I want to set upper limit 4.but it cross 4..
How can set the upper limit.?

Method 1 : after you increment count test its value. If is greater than your limit then set it to the limit

Method 2 : before you increment count test its value and if it is at your limit then don’t increment it

Method 3 : if count is at your limit then don’t read the increment input

Sir
i am a beginner in programming and i am not well expert .so please give some program example.

You already have examples of testing values in the code you posted.

    if (finalstate1==HIGH)
  {
 
  count++;
etc, etc

Did you write the code or copy it from somewhere ?
Do you understand what it does ?

sir ,
where i can insert the limit ,

after lot of search i found this program from google.

i used the below logic but its not work

if (finalstate1==HIGH&&count<=4)
  {count++;

You need to post all the code to allow us to see why it doesn't work.

:slight_smile: now its working

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

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
int inc =2;
int dec =3;
int initialsatate1;
int finalstate1;
int initialsatate2;
int finalstate2;
int count=0;
void setup()
{
  
  lcd.begin(16,2);
 lcd.setCursor(0,0);
 lcd.print("counter:");
                        // Turn on the blacklight and print a message.
  lcd.backlight();
}

void loop()
{
  
  //increment part
 
 finalstate1 = digitalRead(inc);
 if (finalstate1!=initialsatate1)
 {
  delay(10);
  
  finalstate1 = digitalRead(inc);
  {
    if (finalstate1==HIGH&&count<4)
  {
 
  count++;
 
  lcd.clear();
   lcd.print("counter:");
   lcd.setCursor(0,1);
   lcd.print(count);
  }
 
  initialsatate1=finalstate1;
 }}

 // decrements
 
 finalstate2 = digitalRead(dec);
 if (finalstate2!=initialsatate2)
 {
  delay(10);
  finalstate2 = digitalRead(dec);
  {
    if (finalstate2==HIGH&&count>1)
  {
   count--;
   lcd.clear();
   lcd.print("counter:");
   lcd.setCursor(0,1);
   lcd.print(count);
  }
 
  initialsatate2=finalstate2;
} }}

Just as a matter of interest, how are the inputs wired ?