Variable not correct on lcd screen

Hi I’m new to this programming lark and I’m struggling my way through. I’m more looking for tips than a direct answer.
I have a variable on an LCD screen which i want to increment with a push button. my variable starts at 0 But when i press the button the LCD screen shows 1111 then goes up in what seems like random thousands.

If anybody has any tips it would be much appreciated.

What it does is displays 3 variables that make up a time.
Then i can select each hour minute or seconds individually and use the up and down buttons to change (increment or decrease) the time.
Also on the minutes and seconds i want it to get to 60 then go to 0. But i think i can cope with that bit myself.

I’m sure the code can be written a lot more efficiently than i have done it but i have only been learning for a 4 days.
Ive only posted part of the code with the problem but there is enough to load on an arduino to see what i mean,

    #include <LiquidCrystal.h>
    #include <Menu.h>
    #include <LCDMenu2.h>

    #define _LCD_rows       2
    #define _LCD_cols       16
  
    #define _BUTTON_up      6
    #define _BUTTON_down    4
    #define _BUTTON_enter   5
    #define _BUTTON_back    3
    
    #define _BUTTON_prestat 0
   
    #define _BUTTON_press_time  250
    
int restTimeH = 0;
int restTimeM = 0;
int restTimeS = 5;


int lapTimeH = 0;
int lapTimeM = 0;
int lapTimeS = 10;

int sets = 4;
///////////////////////////
int debouncetime = 80;
int tests = 1;
int hmtsel = 1;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void test()
{
 lcd.setCursor(4, 1);
 lcd.print(lapTimeH);
 printDigits(lapTimeM);
 printDigits(lapTimeS);
 lcd.print(" ");
                
while  (tests == 1)   
     {if (digitalRead(_BUTTON_up) == 1) {
          delay(debouncetime);
            if (digitalRead(_BUTTON_up) == 1){
             if (hmtsel = 1) {
                 lapTimeH++;
                 delay(100);
                 lcd.setCursor(4, 1);
                 lcd.print(lapTimeH++);
             }
              if (hmtsel = 2) {
                  lapTimeM++;
                  lcd.print(lapTimeM);
              }
               if (hmtsel = 3) {lapTimeS++;} 
                   lcd.print(lapTimeS);}
               }
                if (digitalRead(_BUTTON_down) == 1) {
                    delay(debouncetime);
                    if (digitalRead(_BUTTON_down) == 1){
                    }
                }

                 if (digitalRead(_BUTTON_enter) == 1) {
                     delay(debouncetime);
                     if (digitalRead(_BUTTON_enter) == 1){
                     }
                 }

                  if (digitalRead(_BUTTON_back) == 1) {
                      delay(debouncetime);
                      if (digitalRead(_BUTTON_back) == 1){
                      }
        }        }    
}
             
void printDigits(int digits) {
  lcd.print(":");
  if(digits < 10)
  lcd.print("0");
  lcd.print(digits);
}

    void setup()
    {
        lcd.begin(_LCD_rows,_LCD_cols);


    }

    void loop()
    {

 test();
    }

Many Thanks

if (hmtsel = 2) Comparisons use == not =

Hi

Thank you for your reply, i cant believe i missed that. But unfortunatly it has not solved my problem. On the first click is shows 1100 then 3100,5100,7100,9100,11100,13100 etc as i click the button. i only want it to go from 0 to 1 then 2 etc.

Thanks again for your help.

Your code would be clearer and easier to read if you used the IDE's auto-format tool (ctrl-T)

You might want to look up lcd.clear

lapTimeH++;
delay(100);
lcd.setCursor(4, 1);
lcd.print(lapTimeH++);
Why are you incrementing lapTimeH again?

Sorry that was just a test and i left it in there.
The second lapTimeH should not have ++.
But that still does not sort the problem out.

I have also tried lcd.clear(); and that clears my screen then displays the same incorrect numbers.

Thank you for the tip about the auto format tool.

The whole project is a timer that counts down and fires a relay.
At the moment i’m building the menu type of system so i can change the hour min and seconds with the lcd and buttons, then once it is set it counts down from the newly set time.

Here is the code auto formated, thank you

#include <LiquidCrystal.h>
#include <Menu.h>
#include <LCDMenu2.h>


#define _LCD_rows       2
#define _LCD_cols       16

#define _BUTTON_up      6
#define _BUTTON_down    4
#define _BUTTON_enter   5
#define _BUTTON_back    3

#define _BUTTON_prestat 0

#define _BUTTON_press_time  250




int restTimeH = 0;
int restTimeM = 0;
int restTimeS = 5;


int lapTimeH = 0;
int lapTimeM = 0;
int lapTimeS = 10;

int sets = 4;
///////////////////////////
int debouncetime = 80;
int tests = 1;
int hmtsel = 1;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Neuste Version, RW wird nicht mehr gebraucht 





void test()
{
  lcd.setCursor(4, 1);
  lcd.print(lapTimeH);
  printDigits(lapTimeM);
  printDigits(lapTimeS);
  lcd.print(" ");

  while  (tests == 1)   
  {
    if (digitalRead(_BUTTON_up) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_up) == 1){
        if (hmtsel == 1) {
          lapTimeH ++;
          delay(100);
          lcd.setCursor(4, 1);
          lcd.print(lapTimeH);
        }
        if (hmtsel == 2) {
          lapTimeM++;
          lcd.print(lapTimeM);
        }
        if (hmtsel == 3) {
          lapTimeS++;
        } 
        lcd.print(lapTimeS);
      }
    }
    if (digitalRead(_BUTTON_down) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_down) == 1){
      }
    }

    if (digitalRead(_BUTTON_enter) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_enter) == 1){
      }
    }

    if (digitalRead(_BUTTON_back) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_back) == 1){
      }
    }        
  }    
}

void printDigits(int digits) {
  lcd.print(":");
  if(digits < 10)
    lcd.print("0");
  lcd.print(digits);
}

void setup()
{
  lcd.begin(_LCD_rows,_LCD_cols);


}

void loop()
{

  test();
}

But that still does not sort the problem out

Time to add some serial debug

I tried some serial debuging and the serial was saying the variable was 1 with one click and 2 with another and 3 with another but on the lcd it was displaying what i have said previously.

Im very stumped.

Here is the code with the debugging, try it for yourself its well wierd. My Serial output increments by one each time (lovely) but on the lcd its displaying the previous numbers said in the before post.

#include <LiquidCrystal.h>
#include <Menu.h>
#include <LCDMenu2.h>


#define _LCD_rows       2
#define _LCD_cols       16

#define _BUTTON_up      6
#define _BUTTON_down    4
#define _BUTTON_enter   5
#define _BUTTON_back    3

#define _BUTTON_prestat 0

#define _BUTTON_press_time  250




int restTimeH = 0;
int restTimeM = 0;
int restTimeS = 5;


int lapTimeH = 0;
int lapTimeM = 0;
int lapTimeS = 10;

int sets = 4;
///////////////////////////
int debouncetime = 80;
int tests = 1;
int hmtsel = 1;
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Neuste Version, RW wird nicht mehr gebraucht 





void test()
{
  lcd.setCursor(4, 1);
  lcd.print(lapTimeH);
  printDigits(lapTimeM);
  printDigits(lapTimeS);
  lcd.print(" ");

  while  (tests == 1)   
  {
    if (digitalRead(_BUTTON_up) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_up) == 1){
        if (hmtsel == 1) {
          lapTimeH ++;
          delay(100);
          lcd.setCursor(4, 1);
          lcd.print(lapTimeH);
          Serial.print(lapTimeH);
        }
        if (hmtsel == 2) {
          lapTimeM++;
          lcd.print(lapTimeM);
        }
        if (hmtsel == 3) {
          lapTimeS++;
        } 
        lcd.print(lapTimeS);
      }
    }
    if (digitalRead(_BUTTON_down) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_down) == 1){
      }
    }

    if (digitalRead(_BUTTON_enter) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_enter) == 1){
      }
    }

    if (digitalRead(_BUTTON_back) == 1) {
      delay(debouncetime);
      if (digitalRead(_BUTTON_back) == 1){
      }
    }        
  }    
}

void printDigits(int digits) {
  lcd.print(":");
  if(digits < 10)
    lcd.print("0");
  lcd.print(digits);
}

void setup()
{
  lcd.begin(_LCD_rows,_LCD_cols);
  Serial.begin(9600);
  Serial.println("DEBUG:");
  Serial.println("================");


}

void loop()
{

  test();
}

try it for yourself its well wierd

How am I going to do that?

I was thinking everybody would have an arduino in here. A breadboard and maybe a spare LCD a button with a pulldown resistor.

It was only a thought. I just am lacking knowledge with the programming and hoped somebody could help me understand why this is doing this.

void setup()
{
	lcd.begin(_LCD_rows,_LCD_cols);

	Serial.begin(9600);

	Serial.println("DEBUG:");
	Serial.println("================");

	pinMode(_BUTTON_up, INPUT);
	pinMode(_BUTTON_down, INPUT);
	pinMode(_BUTTON_enter, INPUT);
	pinMode(_BUTTON_back, INPUT);
}

EDIT: (AWOL) Fixed typo. My eternal editor had autocorrect on and I failed to notice its contribution to my edit.

pineMode(_BUTTON_up, OUTPUT);
    pineMode(_BUTTON_down, OUTPUT);
    pineMode(_BUTTON_enter, OUTPUT);
    pineMode(_BUTTON_back, OUTPUT);
}

Now I'm really confused. Is there maybe an oakMode, and an ashMode?

EDIT: Still confused - why "OUTPUT"?

Simple. I'm in to much pain and trying to be useful but apparently to distracted to be so.

They were INPUT by default, but I think you may be on the right track - @OP what about pul-downs?

just tried these and

pineMode(_BUTTON_up, OUTPUT);
pineMode(_BUTTON_down, OUTPUT);
pineMode(_BUTTON_enter, OUTPUT);
pineMode(_BUTTON_back, OUTPUT);

Gives me error: 'pineMode' was not declared in this scope

and the pinMode stops the buttons from working.

All my buttons are in a pulldown configuration.

Thanks for helping

Haven't studied the code much, but something just stood out in this section :

      if (digitalRead(_BUTTON_up) == 1){
        if (hmtsel == 1) {
          lapTimeH ++;
          delay(100);
          lcd.setCursor(4, 1);
          lcd.print(lapTimeH);
          Serial.print(lapTimeH);
        }
        if (hmtsel == 2) {
          lapTimeM++;
          lcd.print(lapTimeM);
        }
        if (hmtsel == 3) {
          lapTimeS++;
        } 
        lcd.print(lapTimeS);
      }

Why is the lcd.print(lapTimeS); outside the if (hmtsel == 3) { loop, but the lcd.print(lapTimeM);and the lcd.print(lapTimeH); are inside the 'IF' loops ?

All Sorted

That is so obvious now you pointed that out. That is where the extra figures are coming from. That autoformat made it clear as day.

Something so simple, how dumb do i feel.

Thank you all for your help.

I have lost count of the number of times over the last 15 years that I have stared at a piece of code for hours and failed to see a really simple and obvious mistake.

An author is the last one to see his own errors. Years ago in the print industry we learnt (usually the hard way) never to proof read our own work.

Thanks OP for staying with it and working at it. Don't feel dumb.