Up Down Counter

Hi There, I'm struggling with the up and down counter . the out puts are fine but if you keep pressing the button up or down you will lose the numbers off the screen and you have to keep pressing the buttons until you come back to the 0 or 4 which is no good for what im trying to do . i'm new to the IDE but not the developing hardware
Thank you

Matthew

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

int S1 = A3;
int S2 = A2;
int S3 = A1;
int S4 = A0;

int counter = 0;


// this constant won't change:

const int  Up_buttonPin   = 2;    // the pin that the pushbutton is attached to

const int  Down_buttonPin = 3;



// Variables will change:

int buttonPushCounter = 0;   // counter for the number of button presses

int up_buttonState = 0;         // current state of the up button

int up_lastButtonState = 0;     // previous state of the up button



int down_buttonState = 0;         // current state of the up button

int down_lastButtonState = 0;     // previous state of the up button

bool bPress = false;



byte LT[8] = 

{



  B00111,

  B01111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111

};

byte UB[8] =

{

  B11111,

  B11111,

  B11111,

  B00000,

  B00000,

  B00000,

  B00000,

  B00000

};

byte RT[8] =

{



  B11100,

  B11110,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111

};

byte LL[8] =

{





  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B01111,

  B00111

};

byte LB[8] =

{

  B00000,

  B00000,

  B00000,

  B00000,

  B00000,

  B11111,

  B11111,

  B11111

};

byte LR[8] =

{





  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11110,

  B11100

};

byte MB[8] =

{

  B11111,

  B11111,

  B11111,

  B00000,

  B00000,

  B00000,

  B11111,

  B11111

};

byte block[8] =

{

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111,

  B11111

};



void setup()

{



  

  lcd.init();                      // initialize the lcd 




  lcd.createChar(0,LT);

  lcd.createChar(1,UB);

  lcd.createChar(2,RT);

  lcd.createChar(3,LL);

  lcd.createChar(4,LB);

  lcd.createChar(5,LR);

  lcd.createChar(6,MB);

  lcd.createChar(7,block);


  

  // Print a message to the LCD.

  lcd.backlight();

   

  lcd.clear();



  displayNumber();

   

}



void printNumber(int val){

  

     int col=0;   
       
     

     if( val >= 4){

       printDigits(val ,col);     

       printDigits(val ,col);
       
       printDigits(val ,col);
       
       printDigits(val ,col);
       
     }

     else{

       printDigits(val,col);

     }

}



void loop()

{

   checkUp();

   checkDown();



   if( bPress){

       bPress = false;

       displayNumber();

   }

}

void displayNumber(){

    

    lcd.clear();

    lcd.setCursor(5,5);

    lcd.print("TEST"); 

    printNumber( buttonPushCounter);

 
  pinMode( Up_buttonPin , INPUT_PULLUP);

  pinMode( Down_buttonPin , INPUT_PULLUP);
}

void custom0(int x){ 
  
  digitalWrite(S1, LOW);
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  digitalWrite(S4, LOW);
  
  lcd.setCursor(x,0); 

  lcd.write(0);  

  lcd.write(1);  

  lcd.write(2);

  lcd.setCursor(x, 1); 

  lcd.write(3);  

  lcd.write(4);  

  lcd.write(5);
 
}



void custom1(int x){

  lcd.setCursor(x,0);

  lcd.write(1);

  lcd.write(2);

  lcd.print(" ");

  lcd.setCursor(x,1);

  lcd.write(4);

  lcd.write(7);

  lcd.write(4);
  pinMode(S1, OUTPUT);
  digitalWrite(S1, HIGH);
  digitalWrite(S2, LOW);
}



void custom2(int x){
digitalWrite(S1, LOW);
  lcd.setCursor(x,0);

  lcd.write(6);

  lcd.write(6);

  lcd.write(2);

  lcd.setCursor(x, 1);

  lcd.write(3);

  lcd.write(4);

  lcd.write(4);
  pinMode(S2, OUTPUT);
  digitalWrite(S2, HIGH);
  digitalWrite(S3, LOW);
}



void custom3(int x){
digitalWrite(S2, LOW);
  lcd.setCursor(x,0);

  lcd.write(6);

  lcd.write(6);

  lcd.write(2);

  lcd.setCursor(x, 1);

  lcd.write(4);

  lcd.write(4);

  lcd.write(5); 
  pinMode(S3, OUTPUT);
  digitalWrite(S3, HIGH);
  digitalWrite(S4, LOW);
}



void custom4(int x){
  digitalWrite(S3, LOW);
  lcd.setCursor(x,0);

  lcd.write(3);

  lcd.write(4);

  lcd.write(7);

  lcd.setCursor(x, 1);

  lcd.print(" ");

  lcd.print(" ");

  lcd.write(7);
  pinMode(S4, OUTPUT);
  digitalWrite(S4, HIGH);

}

void printDigits(int digits, int x){

  // utility function for digital clock display: prints preceding colon and leading 0



  switch (digits) {

  case 0:  

    custom0(x);

    break;

  case 1:  

    custom1(x);

    break;

  case 2:  

    custom2(x);

    break;

  case 3:  

    custom3(x);

    break;

  case 4:  

    custom4(x);

     break;

  }



}



void checkUp()

{

  up_buttonState = digitalRead(Up_buttonPin);



  // compare the buttonState to its previous state

  if (up_buttonState != up_lastButtonState) {

    // if the state has changed, increment the counter

    if (up_buttonState == LOW) {

        bPress = true;

      // if the current state is HIGH then the button went from off to on:

      buttonPushCounter++;

      Serial.println("on");

      Serial.print("number of button pushes: ");

      Serial.println(buttonPushCounter);

    } else {

      // if the current state is LOW then the button went from on to off:

      Serial.println("on");

    }

    // Delay a little bit to avoid bouncing

    delay(50);

  }

  // save the current state as the last state, for next time through the loop

  up_lastButtonState = up_buttonState;

}

void checkDown()

{

  down_buttonState = digitalRead(Down_buttonPin);



  // compare the buttonState to its previous state

  if (down_buttonState != down_lastButtonState) {

    // if the state has changed, increment the counter

    if (down_buttonState == LOW) {

        bPress = true;

      // if the current state is HIGH then the button went from off to on:

      buttonPushCounter--;

     

      Serial.println("on");

      Serial.print("number of button pushes: ");

      Serial.println(buttonPushCounter);

    } else {

      // if the current state is LOW then the button went from on to off:

      Serial.println("on");

    }

    // Delay a little bit to avoid bouncing

    delay(50);

  }

  // save the current state as the last state, for next time through the loop

  down_lastButtonState = down_buttonState;

}

Hello matthew40

Post your sketch, well formated, with comments and in so called code tags "</>" and schematic to see how we can help.
Have a nice day and enjoy coding in C++.

OMG I guess half of the problems you have comes from all these empty lines that hide away most of your code

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

LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display

int led1 = A3;
int led2 = A2;
int led3 = A1;
int led4 = A0;

int counter = 0;

// this constant won't change:
const int  Up_buttonPin   = 2;    // the pin that the pushbutton is attached to
const int  Down_buttonPin = 3;

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int up_buttonState = 0;         // current state of the up button

int up_lastButtonState = 0;     // previous state of the up button

int down_buttonState = 0;         // current state of the up button
int down_lastButtonState = 0;     // previous state of the up button

bool bPress = false;

byte LT[8] = {
  B00111,
  B01111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};

byte UB[8] = {
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};

byte RT[8] = {
  B11100,
  B11110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};

byte LL[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B01111,
  B00111
};

byte LB[8] = {
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111,
  B11111
};

byte LR[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11110,
  B11100
};

byte MB[8] = {
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};

byte block[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};


void setup() {
  pinMode( Up_buttonPin , INPUT_PULLUP);
  pinMode( Down_buttonPin , INPUT_PULLUP);

  lcd.init();                      // initialize the lcd
  lcd.createChar(0, LT);
  lcd.createChar(1, UB);
  lcd.createChar(2, RT);
  lcd.createChar(3, LL);
  lcd.createChar(4, LB);

  // Print a message to the LCD.
  lcd.backlight();
  lcd.clear();
  displayNumber();
}


void printNumber(int val) {
  int col = 5;

  if ( val >= 10) {
    printDigits(val / 10, col);
    printDigits(val % 10, col + 4);
  }
  else {
    printDigits(val, col);
  }
}


void loop() {
  checkUp();
  checkDown();

  if ( bPress) {
    bPress = false;
    displayNumber();
  }
}

void displayNumber() {
  lcd.clear();
  lcd.setCursor(10, 5);
  lcd.print("B.D.R:");

  printNumber( buttonPushCounter);
}


void custom0(int x) {
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  lcd.setCursor(x, 0);

  lcd.write(0);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
}


void custom1(int x) {
  lcd.setCursor(x, 0);
  lcd.write(1);
  lcd.write(2);
  lcd.print(" ");
  lcd.setCursor(x, 1);
  lcd.write(4);
  lcd.write(7);
  lcd.write(4);
  pinMode(led1, OUTPUT);
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
}


void custom2(int x) {
  digitalWrite(led1, LOW);
  lcd.setCursor(x, 0);

  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(4);

  pinMode(led2, OUTPUT);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, LOW);
}


void custom3(int x) {
  digitalWrite(led2, LOW);
  lcd.setCursor(x, 0);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(x, 1);

  lcd.write(4);
  lcd.write(4);
  lcd.write(5);

  pinMode(led3, OUTPUT);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, LOW);
}


void custom4(int x) {
  digitalWrite(led3, LOW);
  lcd.setCursor(x, 0);
  lcd.write(3);
  lcd.write(4);
  lcd.write(7);

  lcd.setCursor(x, 1);
  lcd.print(" ");
  lcd.print(" ");

  lcd.write(7);
  pinMode(led4, OUTPUT);
  digitalWrite(led4, HIGH);
}

void printDigits(int digits, int x) {
  // utility function for digital clock display: prints preceding colon and leading 0

  switch (digits) {

    case 0:
      custom0(x);
      break;

    case 1:
      custom1(x);
      break;

    case 2:
      custom2(x);
      break;

    case 3:
      custom3(x);
      break;

    case 4:
      custom4(x);
      break;
  }
}



void checkUp() {
  up_buttonState = digitalRead(Up_buttonPin);
  
  // compare the buttonState to its previous state
  if (up_buttonState != up_lastButtonState) {
    // if the state has changed, increment the counter
    if (up_buttonState == LOW) {
      bPress = true;
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }    
    delay(50); // Delay a little bit to avoid bouncing
  }
  // save the current state as the last state, for next time through the loop
  up_lastButtonState = up_buttonState;
}


void checkDown() {
  down_buttonState = digitalRead(Down_buttonPin);
  
  // compare the buttonState to its previous state
  if (down_buttonState != down_lastButtonState) {
    // if the state has changed, increment the counter
    if (down_buttonState == LOW) {
      bPress = true;

      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter--;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    delay(50); // Delay a little bit to avoid bouncing
  }
  // save the current state as the last state, for next time through the loop
  down_lastButtonState = down_buttonState;
}

Check if it goes too high, right there when you increment it, and don't let it.

Similarly, when you are decrementing, don't let it go below zero.

Also, no offense, but your code is quite naive and does not indicate that you are taking on projects commensurate with your skill level.

Slow your role and learn the basics of coding. This sketch coud be about 1/6 the size it is, and I am not talking about the double line feeds.

HTH

a7

See Looking to pay someone to correct existing code or rewrite it

@matthew40

This Topic has been locked as it is the identical or too similar your other topic.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.