Always in last screen

Hi guys

I’m building a sketch with 6 panels on an LCD Keypad Shield and each page will have a different counter. Navigation is ok with the left and right buttons switching the panels from 1 to 6. However when try to increment ou decrement the counter with the up or down buttons in the panels 2, 3, 4 and 5, is automaticaly opening panel 6. Can you help me undersant what is hapenning?

Thanks in advance

#include <Math.h>
#include <Wire.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


//Counter to change positions of pages

int pagecounter=1 ;       //To move beetwen pages



//Alarmes
int countA = 24;
int countB = 6;
int countC = 10;
int countD = 14;
int countE = 2;



// 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

// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 

 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result

 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
}


        

void setup() {
  lcd.begin(16,2); 

}


void loop() {
  

lcd_key = read_LCD_buttons();  // read the buttons

 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   
   case btnRIGHT:
     {
    pagecounter=pagecounter+1;
     if(pagecounter>6) {
     pagecounter = 6;
     }
     delay(200);
     break;
    }
     
     
     case btnLEFT:
     {
     pagecounter=pagecounter-1;
     if(pagecounter<1) {
     pagecounter = 1;
     }
     delay(200);
     break;
     }
   
 



  // Setpoint alarm


   
 //BUTTON UP 
   case btnUP:
     {
     if(pagecounter=2){
    countA=countA+1;
     }
     or if(pagecounter=3){
    countB=countB+1;
     }
     if(pagecounter=4){
    countC=countC+1;
     }
     if(pagecounter=5){
    countD=countD+1;
     }
     if(pagecounter=6){
    countE=countE+1;
     }
      delay(200);
 break;
     }





 //BUTTON DOWN 
   case btnDOWN:
     {
     if(pagecounter=2){
    countA=countA-1;
     }
     if(pagecounter=3){
    countB=countB-1;
     }
     if(pagecounter=4){
    countC=countC-1;
     }
     if(pagecounter=5){
    countD=countD-1;
     }
     if(pagecounter=6){
    countE=countE-1;
     }
      delay(200);
 break;
     }
     
 }




//LCD


  switch (pagecounter) {
   
    case 1:{     //Design of home page 1
      lcd.setCursor(1,0);
      lcd.print("This is home ");
    }
    break;

    case 2: { //Design of page 2 
     lcd.setCursor(5,0);
     lcd.print("This is 2");
      lcd.setCursor(3,1);
      lcd.print(countA);
      lcd.print("    ");
     }
    break;

    case 3: {   //Design of page 3 
     lcd.setCursor(5,0);
     lcd.print("This is 3");
     lcd.setCursor(3,1);
     lcd.print(countB);
     lcd.print("    ");
    }
    break;


    case 4: { //Design of page 4 
     lcd.setCursor(5,0);
     lcd.print("This is 4");
     lcd.setCursor(3,1);
     lcd.print(countC);
     lcd.print("    ");
     }
    break;

case 5: { //Design of page 5 
     lcd.setCursor(5,0);
     lcd.print("This is 5");
     lcd.setCursor(3,1);
     lcd.print(countD);
     lcd.print("    ");
     }
    break;

case 6: { //Design of page 6 
     lcd.setCursor(5,0);
     lcd.print("This is 6");
     lcd.setCursor(3,1);
     lcd.print(countE);
     lcd.print("    ");
     }
    break;

    
  }


}

Not:

        if (pagecounter = 6)

But:

        if (pagecounter == 6)

You can simplify the sketch quite a bit by using an array for the counts instead of 5 separate variables:

// #include <Math.h>
// #include <Wire.h>
#include <LiquidCrystal.h>


LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


//Counter to change positions of pages
int pagecounter = 0 ;     //To move beetwen pages


// Alarmes for pagecounter 0 through 5
int counts[6] = {0, 24, 6, 10, 14, 2};


// 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


// read the buttons
int read_LCD_buttons()
{
  adc_key_in = analogRead(0);      // read the value from the sensor


  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result


  if (adc_key_in < 50)   return btnRIGHT;
  if (adc_key_in < 195)  return btnUP;
  if (adc_key_in < 380)  return btnDOWN;
  if (adc_key_in < 555)  return btnLEFT;
  if (adc_key_in < 790)  return btnSELECT;
  
  return 0;
}


void setup()
{
  lcd.begin(16, 2);


}




void loop()
{
  delay(200);  // debounce?
  lcd_key = read_LCD_buttons();  // read the buttons


  switch (lcd_key)   // depending on which button was pushed, we perform an action
  {
    case btnRIGHT:
      {
        pagecounter++;
        if (pagecounter > 5)
        {
          pagecounter = 5;
        }


        break;
      }




    case btnLEFT:
      {
        pagecounter--;
        if (pagecounter < 0)
        {
          pagecounter = 0;
        }
        break;
      }




    // Setpoint alarm


    //BUTTON UP
    case btnUP:
      counts[pagecounter]++;
      break;




    //BUTTON DOWN
    case btnDOWN:
      counts[pagecounter]--;
      delay(200);
      break;
  }




  //LCD
  if (pagecounter == 0)
  {
    //Design of home page 1
    lcd.setCursor(1, 0);
    lcd.print("This is home ");
  }
  else
  {
    lcd.setCursor(5, 0);
    lcd.print("This is ");
    lcd.print(pagecounter + 1);
    lcd.setCursor(3, 1);
    lcd.print(counts[pagecounter]);
    lcd.print("    ");
  }
}

Problem solved.

Very greatful guys.