Need help to write code for increment/ detriment counter.

Good Day to everyone.
i am writing a menu to be displayed on 16*2 lcd. menu have 10 index points. on pressing up/down button each time, items of menu should change accordingly. i have decided to write codes in chunks, for the first chunk i am trying to write code to check weather up/down buttons are functioning properly. for that i am getting the “count” be displayed on lcd. ( this is temporary, just to check if code for scrolling through menu is working properly or not, if it work correctly, i will use switch function with count as variable, to get the items if menu displayed.)
i have written following codes. UP button is working accurately. ( 10,9,8…to 1 an again roll over to 10)
but on pressing push button for down, output are random numbers between 1 to 10.
i am not able to find out the error. kindly help me.
my best regards.
thank you.

#include <LiquidCrystal.h>
LiquidCrystal lcd (5,4,3,2,1,0);
const byte Menu_Button = A0;
const byte Up_Button = A1;
const byte Select_Button = A2;
const byte Down_Button = A3;
byte Menu_Button_State;
byte Up_Button_State;
byte Select_Button_State;
byte Down_Button_State;
byte lastState = LOW;
byte count = 10;


void setup() {
  lcd.begin (16,2);
  lcd.clear ();
  lcd.print ("HOPE");
  delay(3000);
  lcd.clear ();
  pinMode ( Up_Button, INPUT );
  pinMode ( Menu_Button, INPUT );
  pinMode ( Down_Button, INPUT );
  pinMode ( Select_Button, INPUT );
  digitalWrite ( Select_Button, HIGH );
  digitalWrite ( Down_Button, HIGH );
  digitalWrite ( Menu_Button, HIGH );

}

void loop() {
  
Menu_Button_State = digitalRead ( Menu_Button );  
Up_Button_State = digitalRead ( Up_Button );
Select_Button_State = digitalRead ( Select_Button );
Down_Button_State = digitalRead ( Down_Button );

if ( Menu_Button_State == LOW ){
  if (Select_Button_State != LOW){
if ( Up_Button_State && Up_Button_State != lastState )
{
  if ( count > 0 ){
  count -= 1;}
  else{
  count = 10;
  }
}

if ( Down_Button_State && Down_Button_State != lastState )
{
  if  ( count < 10 ){
  count += 1;}
  else{
  count = 0;
  }
}
}
}
lastState = Up_Button_State ;
lcd.clear ();
lcd.setCursor (0,1);
lcd.print ( count );
}
lastState = Up_Button_State ;

For starters, you want a last state for the Up and Down buttons.

You may also need to debounce your buttons.

UP button is working accurately. ( 10,9,8..to 1 an again roll over to 10)

You have an interesting interpretation of up and down :slight_smile:

For what it is worth, it is called increment and decrement. Now that you know the correct term, you might have better luck finding your solution.

Thank You so much for help,
and i apologies for my incorrect English… ((
i have debounced all push buttons and modify the code as under.
in this code, requirements are counter should increment by one, only wen menu button is LOW and down button is pressed. and when menu button is LOW and up button is pressed counter should get decrement by one. well, right now by running this code, down button is working correctly, but up button is not responding at all, on reset, when i push menu and up button it counter shows “10”. but after using down button it stops responding at all. can you please point out my mistake in this code? and rectify it.

Thank You.
Best regards.!

#include <LiquidCrystal.h>
LiquidCrystal lcd (5,4,3,2,1,0);
const byte Menu_Button = A0;
const byte Up_Button = A1;
const byte Down_Button = A2;
byte Menu_Button_State;
byte Up_Button_State;
byte Down_Button_State;
byte Up_Last_State = LOW;
byte Down_Last_State;
byte count = 0;


void setup() {
  lcd.begin (16,2);
  lcd.clear ();
  lcd.print ("HOPE");
  delay(3000);
  lcd.clear ();
  pinMode ( Up_Button, INPUT );
  pinMode ( Down_Button, INPUT );
  pinMode ( Menu_Button, INPUT );
  digitalWrite ( Menu_Button, HIGH );

}

void loop() {
  
Menu_Button_State = digitalRead ( Menu_Button );  
Up_Button_State = digitalRead ( Up_Button );
Down_Button_State = digitalRead ( Down_Button );

if ( Menu_Button_State == LOW )
{
if ( Up_Button_State && Up_Button_State != Up_Last_State )
{
  if  ( count > 1 ){
  count -= 1;
  }
  else{
  count = 10;
  }
  Up_Last_State = Up_Button_State ;
}
}

if ( Menu_Button_State == LOW ){
if ( Down_Button_State && Down_Button_State != Down_Last_State )
{
  if  ( count < 10 ){
  count += 1;
  }
  else{
  count = 1;
  }
}
Down_Last_State = Down_Button_State;
}

lcd.clear ();
lcd.setCursor (0,1);
lcd.print ( count );
}

Can you provide a drawing of how your buttons are wired? Please describe what the state of each button should be when it is pressed or not pressed?

Have you confirmed that each button behaves as expected with a simple digitalRead() sketch?

Respected cattedog,

Thank you for your help. Yes, I have checked the buttons. They are working properly. Kindly find the photo of schematic of button wiring that I am using right now, attached with this. Is there any other way to write code for this requirement?

I would have suggested adding some debugging printouts but you are using pins 0 and 1 for the LCD. Can you change that and add the prints ?

Respected cattedog,

thank you for help and quick reply.
here is the, wiring diagram i am using for push buttons. and yes i can change pin for lcd, what should i do add debugging for lcd.?

Scan.jpg

what should i do add debugging for lcd.?

Insert a Serial.begin() into setup() then, at relevant parts of the program print a message indicating that the program has reached that point and print an identifier and variable value before each test of variables in your program to ensure that the tests of values that you are doing are reasonable and make sense.

You will be looking to see that the program flaw is as you expect and that the value of variables is as expected.

Yes, I have checked the buttons. They are working properly

I am no so sure of that. When I force the logic in your code, I can cycle the count for 1 to 10 and form 10 to 1 whichever button I make active.

#include <LiquidCrystal.h>
LiquidCrystal lcd (5,4,3,2,1,0);
const byte Menu_Button = A0;
const byte Up_Button = A1;
const byte Down_Button = A2;
byte Menu_Button_State;
byte Up_Button_State;
byte Down_Button_State;
byte Up_Last_State = LOW;
byte Down_Last_State;
byte count = 0;


void setup() {
  lcd.begin (16,2);
  lcd.clear ();
  lcd.print ("HOPE");
  delay(3000);
  lcd.clear ();
  pinMode ( Up_Button, INPUT );
  pinMode ( Down_Button, INPUT );
  pinMode ( Menu_Button, INPUT );
  digitalWrite ( Menu_Button, HIGH );

  Serial.begin(115200);
  Serial.println("starting...");

}

void loop() {
  
Menu_Button_State = LOW;//digitalRead ( Menu_Button );  
//Up_Button_State = LOW;//digitalRead ( Up_Button );
//Up_Button_State = HIGH;//digitalRead ( Up_Button );
Down_Button_State = HIGH;//digitalRead ( Down_Button );
//Down_Button_State = LOW;//digitalRead ( Down_Button );

if ( Menu_Button_State == LOW )
{
if ( Up_Button_State && Up_Button_State != Up_Last_State )
{
  if  ( count > 1 ){
  count -= 1;
  }
  else{
  count = 10;
  }
  //Up_Last_State = Up_Button_State ;
  Up_Last_State = LOW;//Up_Button_State ;
}
}

if ( Menu_Button_State == LOW ){
if ( Down_Button_State && Down_Button_State != Down_Last_State )
{
  if  ( count < 10 ){
  count += 1;
  }
  else{
  count = 1;
  }
}
//Down_Last_State = Down_Button_State;
Down_Last_State = LOW;//Down_Button_State;

}

lcd.clear ();
lcd.setCursor (0,1);
lcd.print ( count );
Serial.println(count);
delay(1000);
}