programming format or thinking error perhaps

hello the group :slight_smile:
i have built myself a motorized camera slider dolly which surprisingly works :roll_eyes: which i am pretty chuffed about
it has an arduino uno at the heart and a dual hbridge shield to run the stepper motor also wired onto the hbridge is an opto coupler and cable to control the camera shutter. it is all based on examples other people have already built on the web so i am no genius :slight_smile: . it had very simple sketch, move right a little take a photo . while it works it’s very basic. so i bought myself a sainsmart 16x2 lcd with the 6 buttons. had some troubles getting it going but google helped me out. the problem i have now is adding functionality i was thinking of using the buttons for a simple menu driven program. i thought of using the left and right buttons to decrement values and the up down buttons to move through the menu. here is the code i started with there is no movement or picture taking code yet i thought i would work on the input first.
now when i press the increment button i can see numbers increasing on the lcd however i don;t get 1,2,3,4 and when i press the decrement button it resets to zero and counts backwards. i have no idea if having the millimeters incrementing inside the switch statement is a good idea or not would debouncing the button help or do i need to do something else altogether ?

#include <LiquidCrystal.h>

/*******************************************************

This program will ask the user for number of milimeters to
move at one time and and number of photos to take then 
execute the program moving the required number of milimeters while taking  photos
********************************************************/

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
int millimeter =  0;
long photoNumber= 0;

// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT;  

 

 return btnNONE;  // when all others fail, return this...
}

void setup()
{
 lcd.begin(16, 2);              // start the library
 lcd.setCursor(0,0);
 lcd.print("Enter mm to move "); // the motor "steps" 25 times to move a millimeter
}
 
void loop()
{
 lcd.setCursor(0,0);            // move cursor to second line "1" and 9 spaces over
 //lcd.print(millis()/1000);      // display seconds elapsed since power-up


 lcd.setCursor(0,1);            // move to the begining of the second line
 lcd_key = read_LCD_buttons();  // read the buttons

 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
       millimeter ++ ;
     lcd.print("moving ") ;
     lcd.print(millimeter );
     lcd.print("mm ") ;
     break;
     }
   case btnLEFT:
     {
     millimeter -- ;  
     lcd.print("moving   ");
     lcd.print(millimeter) ;
     lcd.print("mm"); 
     break;
     }
   case btnUP:
     {
     lcd.print("UP    ");
     break;
     }
   case btnDOWN:
     {
     lcd.print("DOWN  ");
     break;
     }
   case btnSELECT:
     {
     lcd.print("SELECT");
     break;
     }
     case btnNONE:
     {
     lcd.print("");
     break;
     }
 }

}

Moderator edit: Code tags. {sigh}

Do you want the value to change when the switch IS pressed? Or, when it BECOMES pressed? As your code is currently written, the value will increment, or decrement, as rapidly as possible while the switch is pressed.

The state change detection example shows how to do things when the switch state changes to pressed or to released. It will take a little work to adapt it to your switch setup, but it is probably what you need to do.